Package com.google.gson
Class JsonArray
- java.lang.Object
-
- com.google.gson.JsonElement
-
- com.google.gson.JsonArray
-
- All Implemented Interfaces:
java.lang.Iterable<JsonElement>
public final class JsonArray extends JsonElement implements java.lang.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.
-
-
Constructor Summary
Constructors Constructor Description JsonArray()Creates an empty JsonArray.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(JsonElement element)Adds the specified element to self.voidaddAll(JsonArray array)Adds all the elements of the specified array to self.booleanequals(java.lang.Object o)JsonElementget(int i)Returns the ith element of the array.java.math.BigDecimalgetAsBigDecimal()convenience method to get this array as aBigDecimalif it contains a single element.java.math.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 element as a primitive byte value.chargetAsCharacter()convenience method to get this element as a primitive character value.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.java.lang.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.java.lang.StringgetAsString()convenience method to get this array as aStringif it contains a single element.inthashCode()java.util.Iterator<JsonElement>iterator()Returns an iterator to navigate the elemetns of the array.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
-
-
-
-
Method Detail
-
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.
-
size
public int size()
Returns the number of elements in the array.- Returns:
- the number of elements in the array.
-
iterator
public java.util.Iterator<JsonElement> iterator()
Returns an iterator to navigate the elemetns of the array. Since the array is an ordered list, the iterator navigates the elements in the order they were inserted.- Specified by:
iteratorin interfacejava.lang.Iterable<JsonElement>- Returns:
- an iterator to navigate the elements of the array.
-
get
public JsonElement get(int i)
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:
java.lang.IndexOutOfBoundsException- if i is negative or greater than or equal to thesize()of the array.
-
getAsNumber
public java.lang.Number 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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid Number.java.lang.IllegalStateException- if the array has more than one element.
-
getAsString
public java.lang.String 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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid String.java.lang.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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid double.java.lang.IllegalStateException- if the array has more than one element.
-
getAsBigDecimal
public java.math.BigDecimal 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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitive.java.lang.NumberFormatException- if the element at index 0 is not a validBigDecimal.java.lang.IllegalStateException- if the array has more than one element.- Since:
- 1.2
-
getAsBigInteger
public java.math.BigInteger 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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitive.java.lang.NumberFormatException- if the element at index 0 is not a validBigInteger.java.lang.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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid float.java.lang.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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid long.java.lang.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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid integer.java.lang.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 this element as a primitive character value.- Overrides:
getAsCharacterin classJsonElement- Returns:
- get this element as a primitive char value.
-
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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid short.java.lang.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:
java.lang.ClassCastException- if the element in the array is of not aJsonPrimitiveand is not a valid boolean.java.lang.IllegalStateException- if the array has more than one element.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-