- java.lang.Object
-
- com.google.gson.JsonElement
-
- com.google.gson.JsonArray
-
- All Implemented Interfaces:
Iterable<JsonElement>
public final class JsonArray extends JsonElement implements Iterable<JsonElement>
A class representing an array type in JSON. An array is a list ofJsonElements each of which can be of a different type. This is an ordered list, meaning that the order in which elements are added is preserved. This class does not supportnullvalues. Ifnullis provided as element argument to any of the methods, it is converted to aJsonNull.JsonArrayonly implements theIterableinterface but not theListinterface. AListview of it can be obtained withasList().- Author:
- Inderjeet Singh, Joel Leitch
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(JsonElement element)Adds the specified element to self.voidadd(Boolean bool)Adds the specified boolean to self.voidadd(Character character)Adds the specified character to self.voidadd(Number number)Adds the specified number to self.voidadd(String string)Adds the specified string to self.voidaddAll(JsonArray array)Adds all the elements of the specified array to self.List<JsonElement>asList()Returns a mutableListview of thisJsonArray.booleancontains(JsonElement element)Returns true if this array contains the specified element.JsonArraydeepCopy()Creates a deep copy of this element and all its children.booleanequals(Object o)Returns whether the other object is equal to this.JsonElementget(int i)Returns the i-th element of the array.BigDecimalgetAsBigDecimal()Convenience method to get this array as aBigDecimalif it contains a single element.BigIntegergetAsBigInteger()Convenience method to get this array as aBigIntegerif it contains a single element.booleangetAsBoolean()Convenience method to get this array as a boolean if it contains a single element.bytegetAsByte()Convenience method to get this array as a primitive byte if it contains a single element.chargetAsCharacter()Deprecated.This method is misleading, as it does not get this element as a char but rather as a string's first character.doublegetAsDouble()Convenience method to get this array as a double if it contains a single element.floatgetAsFloat()Convenience method to get this array as a float if it contains a single element.intgetAsInt()Convenience method to get this array as an integer if it contains a single element.longgetAsLong()Convenience method to get this array as a long if it contains a single element.NumbergetAsNumber()Convenience method to get this array as aNumberif it contains a single element.shortgetAsShort()Convenience method to get this array as a primitive short if it contains a single element.StringgetAsString()Convenience method to get this array as aStringif it contains a single element.inthashCode()Returns the hash code of this array.booleanisEmpty()Returns true if the array is empty.Iterator<JsonElement>iterator()Returns an iterator to navigate the elements of the array.JsonElementremove(int index)Removes the element at the specified position in this array.booleanremove(JsonElement element)Removes the first occurrence of the specified element from this array, if it is present.JsonElementset(int index, JsonElement element)Replaces the element at the specified position in this array with the specified element.intsize()Returns the number of elements in the array.-
Methods inherited from class com.google.gson.JsonElement
getAsJsonArray, getAsJsonNull, getAsJsonObject, getAsJsonPrimitive, isJsonArray, isJsonNull, isJsonObject, isJsonPrimitive, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
JsonArray
public JsonArray()
Creates an empty JsonArray.
-
JsonArray
public JsonArray(int capacity)
Creates an empty JsonArray with the desired initial capacity.- Parameters:
capacity- initial capacity.- Throws:
IllegalArgumentException- if thecapacityis negative- Since:
- 2.8.1
-
-
Method Detail
-
deepCopy
public JsonArray deepCopy()
Creates a deep copy of this element and all its children.- Specified by:
deepCopyin classJsonElement- Since:
- 2.8.2
-
add
public void add(Boolean bool)
Adds the specified boolean to self.- Parameters:
bool- the boolean that needs to be added to the array.- Since:
- 2.4
-
add
public void add(Character character)
Adds the specified character to self.- Parameters:
character- the character that needs to be added to the array.- Since:
- 2.4
-
add
public void add(Number number)
Adds the specified number to self.- Parameters:
number- the number that needs to be added to the array.- Since:
- 2.4
-
add
public void add(String string)
Adds the specified string to self.- Parameters:
string- the string that needs to be added to the array.- Since:
- 2.4
-
add
public void add(JsonElement element)
Adds the specified element to self.- Parameters:
element- the element that needs to be added to the array.
-
addAll
public void addAll(JsonArray array)
Adds all the elements of the specified array to self.- Parameters:
array- the array whose elements need to be added to the array.
-
set
public JsonElement set(int index, JsonElement element)
Replaces the element at the specified position in this array with the specified element.- Parameters:
index- index of the element to replaceelement- element to be stored at the specified position- Returns:
- the element previously at the specified position
- Throws:
IndexOutOfBoundsException- if the specified index is outside the array bounds
-
remove
public boolean remove(JsonElement element)
Removes the first occurrence of the specified element from this array, if it is present. If the array does not contain the element, it is unchanged.- Parameters:
element- element to be removed from this array, if present- Returns:
- true if this array contained the specified element, false otherwise
- Since:
- 2.3
-
remove
public JsonElement remove(int index)
Removes the element at the specified position in this array. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the array.- Parameters:
index- index the index of the element to be removed- Returns:
- the element previously at the specified position
- Throws:
IndexOutOfBoundsException- if the specified index is outside the array bounds- Since:
- 2.3
-
contains
public boolean contains(JsonElement element)
Returns true if this array contains the specified element.- Parameters:
element- whose presence in this array is to be tested- Returns:
- true if this array contains the specified element.
- Since:
- 2.3
-
size
public int size()
Returns the number of elements in the array.- Returns:
- the number of elements in the array.
-
isEmpty
public boolean isEmpty()
Returns true if the array is empty.- Returns:
- true if the array is empty.
- Since:
- 2.8.7
-
iterator
public Iterator<JsonElement> iterator()
Returns an iterator to navigate the elements of the array. Since the array is an ordered list, the iterator navigates the elements in the order they were inserted.- Specified by:
iteratorin interfaceIterable<JsonElement>- Returns:
- an iterator to navigate the elements of the array.
-
get
public JsonElement get(int i)
Returns the i-th element of the array.- Parameters:
i- the index of the element that is being sought.- Returns:
- the element present at the i-th index.
- Throws:
IndexOutOfBoundsException- if i is negative or greater than or equal to thesize()of the array.
-
getAsNumber
public Number getAsNumber()
Convenience method to get this array as aNumberif it contains a single element. This method callsJsonElement.getAsNumber()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsNumberin classJsonElement- Returns:
- this element as a number if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsString
public String getAsString()
Convenience method to get this array as aStringif it contains a single element. This method callsJsonElement.getAsString()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsStringin classJsonElement- Returns:
- this element as a String if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsDouble
public double getAsDouble()
Convenience method to get this array as a double if it contains a single element. This method callsJsonElement.getAsDouble()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsDoublein classJsonElement- Returns:
- this element as a double if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsBigDecimal
public BigDecimal getAsBigDecimal()
Convenience method to get this array as aBigDecimalif it contains a single element. This method callsJsonElement.getAsBigDecimal()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBigDecimalin classJsonElement- Returns:
- this element as a
BigDecimalif it is single element array. - Throws:
IllegalStateException- if the array is empty or has more than one element.- Since:
- 1.2
-
getAsBigInteger
public BigInteger getAsBigInteger()
Convenience method to get this array as aBigIntegerif it contains a single element. This method callsJsonElement.getAsBigInteger()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBigIntegerin classJsonElement- Returns:
- this element as a
BigIntegerif it is single element array. - Throws:
IllegalStateException- if the array is empty or has more than one element.- Since:
- 1.2
-
getAsFloat
public float getAsFloat()
Convenience method to get this array as a float if it contains a single element. This method callsJsonElement.getAsFloat()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsFloatin classJsonElement- Returns:
- this element as a float if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsLong
public long getAsLong()
Convenience method to get this array as a long if it contains a single element. This method callsJsonElement.getAsLong()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsLongin classJsonElement- Returns:
- this element as a long if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsInt
public int getAsInt()
Convenience method to get this array as an integer if it contains a single element. This method callsJsonElement.getAsInt()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsIntin classJsonElement- Returns:
- this element as an integer if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsByte
public byte getAsByte()
Convenience method to get this array as a primitive byte if it contains a single element. This method callsJsonElement.getAsByte()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBytein classJsonElement- Returns:
- this element as a primitive byte if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsCharacter
@Deprecated public char getAsCharacter()
Deprecated.This method is misleading, as it does not get this element as a char but rather as a string's first character.Convenience method to get this array as a character if it contains a single element. This method callsJsonElement.getAsCharacter()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsCharacterin classJsonElement- Returns:
- this element as a primitive short if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsShort
public short getAsShort()
Convenience method to get this array as a primitive short if it contains a single element. This method callsJsonElement.getAsShort()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsShortin classJsonElement- Returns:
- this element as a primitive short if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
getAsBoolean
public boolean getAsBoolean()
Convenience method to get this array as a boolean if it contains a single element. This method callsJsonElement.getAsBoolean()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBooleanin classJsonElement- Returns:
- this element as a boolean if it is single element array.
- Throws:
IllegalStateException- if the array is empty or has more than one element.
-
asList
public List<JsonElement> asList()
Returns a mutableListview of thisJsonArray. Changes to theListare visible in thisJsonArrayand the other way around.The
Listdoes not permitnullelements. UnlikeJsonArray'snullhandling, aNullPointerExceptionis thrown when trying to addnull. UseJsonNullfor JSON null values.- Returns:
- mutable
Listview - Since:
- 2.10
-
equals
public boolean equals(Object o)
Returns whether the other object is equal to this. This method only considers the other object to be equal if it is an instance ofJsonArrayand has equal elements in the same order.
-
-