Package com.google.gson
Class JsonArray
java.lang.Object
com.google.gson.JsonElement
com.google.gson.JsonArray
- All Implemented Interfaces:
Iterable<JsonElement>
A class representing an array type in Json. An array is a list of
JsonElements 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.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(JsonElement element) Adds the specified element to self.voidAdds the specified boolean to self.voidAdds the specified character to self.voidAdds the specified number to self.voidAdds the specified string to self.voidAdds all the elements of the specified array to self.booleancontains(JsonElement element) Returns true if this array contains the specified element.deepCopy()Creates a deep copy of this element and all its childrenbooleanget(int i) Returns the ith element of the array.convenience method to get this array as aBigDecimalif it contains a single element.convenience method to get this array as aBigIntegerif it contains a single element.booleanconvenience method to get this array as a boolean if it contains a single element.byteconvenience method to get this element as a primitive byte value.charconvenience method to get the first character of this element as a string or the first character of this array's first element as a string.doubleconvenience method to get this array as a double if it contains a single element.floatconvenience 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.longconvenience method to get this array as a long if it contains a single element.convenience method to get this array as aNumberif it contains a single element.shortconvenience method to get this array as a primitive short if it contains a single element.convenience method to get this array as aStringif it contains a single element.inthashCode()booleanisEmpty()Returns true if the array is emptyiterator()Returns an iterator to navigate the elements of the array.remove(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.set(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, toStringMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
JsonArray
public JsonArray()Creates an empty JsonArray. -
JsonArray
public JsonArray(int capacity)
-
-
Method Details
-
deepCopy
Creates a deep copy of this element and all its children- Specified by:
deepCopyin classJsonElement- Since:
- 2.8.2
-
add
Adds the specified boolean to self.- Parameters:
bool- the boolean that needs to be added to the array.
-
add
Adds the specified character to self.- Parameters:
character- the character that needs to be added to the array.
-
add
Adds the specified number to self.- Parameters:
number- the number that needs to be added to the array.
-
add
Adds the specified string to self.- Parameters:
string- the string that needs to be added to the array.
-
add
Adds the specified element to self.- Parameters:
element- the element that needs to be added to the array.
-
addAll
Adds all the elements of the specified array to self.- Parameters:
array- the array whose elements need to be added to the array.
-
set
Replaces the element at the specified position in this array with the specified element. Element can be null.- 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
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
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
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
-
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
Returns the ith element of the array.- Parameters:
i- the index of the element that is being sought.- Returns:
- the element present at the ith index.
- Throws:
IndexOutOfBoundsException- if i is negative or greater than or equal to thesize()of the array.
-
getAsNumber
convenience method to get this array as aNumberif it contains a single element.- Overrides:
getAsNumberin classJsonElement- Returns:
- get this element as a number if it is single element array.
- Throws:
ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid Number.IllegalStateException- if the array has more than one element.
-
getAsString
convenience method to get this array as aStringif it contains a single element.- Overrides:
getAsStringin classJsonElement- Returns:
- get this element as a String if it is single element array.
- Throws:
ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid String.IllegalStateException- if the array has more than one element.
-
getAsDouble
public double getAsDouble()convenience method to get this array as a double if it contains a single element.- Overrides:
getAsDoublein classJsonElement- Returns:
- get this element as a double if it is single element array.
- Throws:
ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid double.IllegalStateException- if the array has more than one element.
-
getAsBigDecimal
convenience method to get this array as aBigDecimalif it contains a single element.- Overrides:
getAsBigDecimalin classJsonElement- Returns:
- get this element as a
BigDecimalif it is single element array. - Throws:
ClassCastException- if the element in the array is of not aJsonPrimitive.NumberFormatException- if the element at index 0 is not a validBigDecimal.IllegalStateException- if the array has more than one element.- Since:
- 1.2
-
getAsBigInteger
convenience method to get this array as aBigIntegerif it contains a single element.- Overrides:
getAsBigIntegerin classJsonElement- Returns:
- get this element as a
BigIntegerif it is single element array. - Throws:
ClassCastException- if the element in the array is of not aJsonPrimitive.NumberFormatException- if the element at index 0 is not a validBigInteger.IllegalStateException- if the array 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.- Overrides:
getAsFloatin classJsonElement- Returns:
- get this element as a float if it is single element array.
- Throws:
ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid float.IllegalStateException- if the array has more than one element.
-
getAsLong
public long getAsLong()convenience method to get this array as a long if it contains a single element.- Overrides:
getAsLongin classJsonElement- Returns:
- get this element as a long if it is single element array.
- Throws:
ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid long.IllegalStateException- if the array has more than one element.
-
getAsInt
public int getAsInt()convenience method to get this array as an integer if it contains a single element.- Overrides:
getAsIntin classJsonElement- Returns:
- get this element as an integer if it is single element array.
- Throws:
ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid integer.IllegalStateException- if the array has more than one element.
-
getAsByte
public byte getAsByte()Description copied from class:JsonElementconvenience method to get this element as a primitive byte value.- Overrides:
getAsBytein classJsonElement- Returns:
- get this element as a primitive byte value.
-
getAsCharacter
public char getAsCharacter()Description copied from class:JsonElementconvenience method to get the first character of this element as a string or the first character of this array's first element as a string.- Overrides:
getAsCharacterin classJsonElement- Returns:
- the first character of the string.
-
getAsShort
public short getAsShort()convenience method to get this array as a primitive short if it contains a single element.- Overrides:
getAsShortin classJsonElement- Returns:
- get this element as a primitive short if it is single element array.
- Throws:
ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid short.IllegalStateException- if the array has more than one element.
-
getAsBoolean
public boolean getAsBoolean()convenience method to get this array as a boolean if it contains a single element.- Overrides:
getAsBooleanin classJsonElement- Returns:
- get this element as a boolean if it is single element array.
- Throws:
ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid boolean.IllegalStateException- if the array has more than one element.
-
equals
-
hashCode
public int hashCode()
-