Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions gson/src/main/java/com/google/gson/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,6 +61,18 @@
elements = 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 deep copy of this element and all its children.
*
Expand Down Expand Up @@ -123,10 +136,7 @@
* @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);
}

/**
Expand All @@ -134,10 +144,42 @@
*
* @param array the array whose elements need to be added to the array.
*/
public void addAll(JsonArray array) {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method JsonArray.addAll(..) could be confused with overloaded method
addAll
, since dispatch depends on static types.
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<? extends JsonElement> 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.
*
Expand Down
307 changes: 307 additions & 0 deletions gson/src/main/java/com/google/gson/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,313 @@ 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 = of(key1, value1, 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These of overloads make it a bit verbose, though I guess there is no better way at the moment and Guava and the JDK do it like this as well.

The other alternative would be to only have maybe of 1-3 and for the rest require the user to use a different approach, e.g. the above mentioned of(Map).

Would be good to wait for the opinion of one of the project members.

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<String, ? extends JsonElement>... entries) {
JsonObject object = new JsonObject();
for (Map.Entry<? extends String, ? extends JsonElement> 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<? extends Map.Entry<String, ? extends JsonElement>> entries) {
JsonObject object = new JsonObject();
for (Map.Entry<? extends String, ? extends JsonElement> 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<String, ? extends JsonElement> map) {
return ofEntries(map.entrySet());
}

/**
* Creates a deep copy of this element and all its children.
*
Expand Down
Loading