diff --git a/gson/src/main/java/com/google/gson/JsonArray.java b/gson/src/main/java/com/google/gson/JsonArray.java index 10b94c0dc1..c857e56c67 100644 --- a/gson/src/main/java/com/google/gson/JsonArray.java +++ b/gson/src/main/java/com/google/gson/JsonArray.java @@ -21,6 +21,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -40,12 +41,17 @@ * @author Joel Leitch */ public final class JsonArray extends JsonElement implements Iterable { + /** All elements in this list are non-null. */ private final ArrayList elements; + @SuppressWarnings({"deprecation", "NonApiType"}) // superclass constructor + private JsonArray(ArrayList elements) { + this.elements = elements; + } + /** Creates an empty JsonArray. */ - @SuppressWarnings("deprecation") // superclass constructor public JsonArray() { - elements = new ArrayList<>(); + this(new ArrayList<>()); } /** @@ -55,9 +61,35 @@ public JsonArray() { * @throws IllegalArgumentException if the {@code capacity} is negative * @since 2.8.1 */ - @SuppressWarnings("deprecation") // superclass constructor public JsonArray(int capacity) { - elements = new ArrayList<>(capacity); + this(new ArrayList<>(capacity)); + } + + /** + * Creates a JsonArray with the specified elements. + * + * @return a new JsonArray. + * @since $next-version$ + */ + public static JsonArray of(JsonElement... elements) { + JsonArray array = new JsonArray(elements.length); + array.addAll(elements); + return array; + } + + /** + * Creates a JsonArray with the specified elements. + * + * @return a new JsonArray. + * @since $next-version$ + */ + public static JsonArray of(Iterable elements) { + JsonArray array = + elements instanceof Collection + ? new JsonArray(((Collection) elements).size()) + : new JsonArray(); + array.addAll(elements); + return array; } /** @@ -123,10 +155,7 @@ public void add(String string) { * @param element the element that needs to be added to the array. */ public void add(JsonElement element) { - if (element == null) { - element = JsonNull.INSTANCE; - } - elements.add(element); + elements.add(element == null ? JsonNull.INSTANCE : element); } /** @@ -138,6 +167,38 @@ public void addAll(JsonArray array) { elements.addAll(array.elements); } + /** + * Adds all the elements to self. + * + * @param elements the elements to be added. + * @since $next-version$ + */ + public void addAll(JsonElement... elements) { + this.elements.ensureCapacity(elements.length + this.elements.size()); + for (JsonElement element : elements) { + add(element); + } + } + + /** + * Adds all the elements of the specified iterable to self. + * + * @param iterable the iterable whose elements need to be added to the array. + * @since $next-version$ + */ + public void addAll(Iterable iterable) { + if (iterable instanceof JsonArray) { + addAll((JsonArray) iterable); + return; + } else if (iterable instanceof Collection) { + elements.ensureCapacity(((Collection) iterable).size() + elements.size()); + } + + for (JsonElement element : iterable) { + add(element); + } + } + /** * Replaces the element at the specified position in this array with the specified element. * diff --git a/gson/src/main/java/com/google/gson/JsonObject.java b/gson/src/main/java/com/google/gson/JsonObject.java index 3bde741190..0861aef8d8 100644 --- a/gson/src/main/java/com/google/gson/JsonObject.java +++ b/gson/src/main/java/com/google/gson/JsonObject.java @@ -44,6 +44,316 @@ public final class JsonObject extends JsonElement { @SuppressWarnings("deprecation") // superclass constructor public JsonObject() {} + /** + * Creates a JsonObject with the specified member. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of(String key, JsonElement value) { + JsonObject object = new JsonObject(); + object.add(key, value); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of(String key1, JsonElement value1, String key2, JsonElement value2) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of( + String key1, + JsonElement value1, + String key2, + JsonElement value2, + String key3, + JsonElement value3) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + object.add(key3, value3); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of( + String key1, + JsonElement value1, + String key2, + JsonElement value2, + String key3, + JsonElement value3, + String key4, + JsonElement value4) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + object.add(key3, value3); + object.add(key4, value4); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of( + String key1, + JsonElement value1, + String key2, + JsonElement value2, + String key3, + JsonElement value3, + String key4, + JsonElement value4, + String key5, + JsonElement value5) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + object.add(key3, value3); + object.add(key4, value4); + object.add(key5, value5); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of( + String key1, + JsonElement value1, + String key2, + JsonElement value2, + String key3, + JsonElement value3, + String key4, + JsonElement value4, + String key5, + JsonElement value5, + String key6, + JsonElement value6) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + object.add(key3, value3); + object.add(key4, value4); + object.add(key5, value5); + object.add(key6, value6); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of( + String key1, + JsonElement value1, + String key2, + JsonElement value2, + String key3, + JsonElement value3, + String key4, + JsonElement value4, + String key5, + JsonElement value5, + String key6, + JsonElement value6, + String key7, + JsonElement value7) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + object.add(key3, value3); + object.add(key4, value4); + object.add(key5, value5); + object.add(key6, value6); + object.add(key7, value7); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of( + String key1, + JsonElement value1, + String key2, + JsonElement value2, + String key3, + JsonElement value3, + String key4, + JsonElement value4, + String key5, + JsonElement value5, + String key6, + JsonElement value6, + String key7, + JsonElement value7, + String key8, + JsonElement value8) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + object.add(key3, value3); + object.add(key4, value4); + object.add(key5, value5); + object.add(key6, value6); + object.add(key7, value7); + object.add(key8, value8); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of( + String key1, + JsonElement value1, + String key2, + JsonElement value2, + String key3, + JsonElement value3, + String key4, + JsonElement value4, + String key5, + JsonElement value5, + String key6, + JsonElement value6, + String key7, + JsonElement value7, + String key8, + JsonElement value8, + String key9, + JsonElement value9) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + object.add(key3, value3); + object.add(key4, value4); + object.add(key5, value5); + object.add(key6, value6); + object.add(key7, value7); + object.add(key8, value8); + object.add(key9, value9); + return object; + } + + /** + * Creates a JsonObject with the specified members. + * + * @return a new JsonObject. + * @since $next-version$ + */ + public static JsonObject of( + String key1, + JsonElement value1, + String key2, + JsonElement value2, + String key3, + JsonElement value3, + String key4, + JsonElement value4, + String key5, + JsonElement value5, + String key6, + JsonElement value6, + String key7, + JsonElement value7, + String key8, + JsonElement value8, + String key9, + JsonElement value9, + String key10, + JsonElement value10) { + JsonObject object = new JsonObject(); + object.add(key1, value1); + object.add(key2, value2); + object.add(key3, value3); + object.add(key4, value4); + object.add(key5, value5); + object.add(key6, value6); + object.add(key7, value7); + object.add(key8, value8); + object.add(key9, value9); + object.add(key10, value10); + return object; + } + + /** + * Creates a JsonObject with the specified entries. + * + * @return a new JsonObject with given entries. + * @since $next-version$ + */ + @SafeVarargs + public static JsonObject ofEntries(Map.Entry... entries) { + JsonObject object = new JsonObject(); + for (Map.Entry entry : entries) { + object.add(entry.getKey(), entry.getValue()); + } + return object; + } + + /** + * Creates a JsonObject with the specified entries. + * + * @return a new JsonObject with given entries. + * @since $next-version$ + */ + public static JsonObject ofEntries( + Iterable> entries) { + JsonObject object = new JsonObject(); + for (Map.Entry entry : entries) { + object.add(entry.getKey(), entry.getValue()); + } + return object; + } + + /** + * Creates a JsonObject from the specified map. All entries of the map will be shallow copied into + * the new JsonObject. + * + * @return a new JsonObject with all entries of the given map. + * @since $next-version$ + */ + public static JsonObject copyOf(Map map) { + return ofEntries(map.entrySet()); + } + /** * Creates a deep copy of this element and all its children. *