Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
77 changes: 69 additions & 8 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 All @@ -40,12 +41,17 @@
* @author Joel Leitch
*/
public final class JsonArray extends JsonElement implements Iterable<JsonElement> {
/** All elements in this list are non-null. */
private final ArrayList<JsonElement> elements;

@SuppressWarnings({"deprecation", "NonApiType"}) // superclass constructor
private JsonArray(ArrayList<JsonElement> elements) {

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
JsonElement.JsonElement
should be avoided because it has been deprecated.
this.elements = elements;
}

/** Creates an empty JsonArray. */
@SuppressWarnings("deprecation") // superclass constructor
public JsonArray() {
elements = new ArrayList<>();
this(new ArrayList<>());
}

/**
Expand All @@ -55,9 +61,35 @@
* @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<? extends JsonElement> elements) {
JsonArray array =
elements instanceof Collection
? new JsonArray(((Collection<?>) elements).size())
: new JsonArray();
array.addAll(elements);
return array;
}

/**
Expand Down Expand Up @@ -112,7 +144,7 @@
*
* @param string the string that needs to be added to the array.
* @since 2.4
*/

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.
public void add(String string) {
elements.add(string == null ? JsonNull.INSTANCE : new JsonPrimitive(string));
}
Expand All @@ -123,10 +155,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 @@ -138,6 +167,38 @@
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
Loading