Top Description Fields Constructors Methods
com.google.gson

public final Class JsonArray

extends JsonElement
implements Iterable<JsonElement>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Imports
com.google.errorprone.annotations.CanIgnoreReturnValue, com.google.gson.internal.NonNullElementWrapperList, java.math.BigDecimal, .BigInteger, java.util.ArrayList, .Iterator, .List

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. This class does not support null values. If null is provided as element argument to any of the methods, it is converted to a JsonNull.

JsonArray only implements the Iterable interface but not the List interface. A List view of it can be obtained with asList().

See the JsonElement documentation for details on how to convert JsonArray and generally any JsonElement from and to JSON.

Authors
Inderjeet Singh, Joel Leitch

Field Summary

Modifier and TypeField and Description
private final ArrayList<JsonElement>

Constructor Summary

AccessConstructor and Description
public
JsonArray()

Creates an empty JsonArray.

public
JsonArray(int
initial capacity.
capacity
)

Creates an empty JsonArray with the desired initial capacity.

Method Summary

Modifier and TypeMethod and Description
public void
add(Boolean
the boolean that needs to be added to the array.
bool
)

Adds the specified boolean to self.

public void
add(Character
the character that needs to be added to the array.
character
)

Adds the specified character to self.

public void
add(Number
the number that needs to be added to the array.
number
)

Adds the specified number to self.

public void
add(String
the string that needs to be added to the array.
string
)

Adds the specified string to self.

public void
add(JsonElement
the element that needs to be added to the array.
element
)

Adds the specified element to self.

public void
addAll(JsonArray
the array whose elements need to be added to the array.
array
)

Adds all the elements of the specified array to self.

public List<JsonElement>

Returns:

mutable List view
asList
()

Returns a mutable List view of this JsonArray.

public boolean

Returns:

true if this array contains the specified element.
contains
(JsonElement
whose presence in this array is to be tested
element
)

Returns true if this array contains the specified element.

public JsonArray
deepCopy()

Implements abstract com.google.gson.JsonElement.deepCopy.

Creates a deep copy of this element and all its children.

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Returns whether the other object is equal to this.

public JsonElement

Returns:

the element present at the i-th index.
get
(int
the index of the element that is being sought.
i
)

Returns the i-th element of the array.

public BigDecimal

Returns:

this element as a BigDecimal if it is single element array.
getAsBigDecimal
()

Overrides com.google.gson.JsonElement.getAsBigDecimal.

Convenience method to get this array as a BigDecimal if it contains a single element.

public BigInteger

Returns:

this element as a BigInteger if it is single element array.
getAsBigInteger
()

Overrides com.google.gson.JsonElement.getAsBigInteger.

Convenience method to get this array as a BigInteger if it contains a single element.

public boolean

Returns:

this element as a boolean if it is single element array.
getAsBoolean
()

Overrides com.google.gson.JsonElement.getAsBoolean.

Convenience method to get this array as a boolean if it contains a single element.

public byte

Returns:

this element as a primitive byte if it is single element array.
getAsByte
()

Overrides com.google.gson.JsonElement.getAsByte.

Convenience method to get this array as a primitive byte if it contains a single element.

public char

Returns:

this element as a primitive short if it is single element array.
getAsCharacter
()

Overrides com.google.gson.JsonElement.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.

public double

Returns:

this element as a double if it is single element array.
getAsDouble
()

Overrides com.google.gson.JsonElement.getAsDouble.

Convenience method to get this array as a double if it contains a single element.

public float

Returns:

this element as a float if it is single element array.
getAsFloat
()

Overrides com.google.gson.JsonElement.getAsFloat.

Convenience method to get this array as a float if it contains a single element.

public int

Returns:

this element as an integer if it is single element array.
getAsInt
()

Overrides com.google.gson.JsonElement.getAsInt.

Convenience method to get this array as an integer if it contains a single element.

public long

Returns:

this element as a long if it is single element array.
getAsLong
()

Overrides com.google.gson.JsonElement.getAsLong.

Convenience method to get this array as a long if it contains a single element.

public Number

Returns:

this element as a number if it is single element array.
getAsNumber
()

Overrides com.google.gson.JsonElement.getAsNumber.

Convenience method to get this array as a Number if it contains a single element.

public short

Returns:

this element as a primitive short if it is single element array.
getAsShort
()

Overrides com.google.gson.JsonElement.getAsShort.

Convenience method to get this array as a primitive short if it contains a single element.

private JsonElement
public String

Returns:

this element as a String if it is single element array.
getAsString
()

Overrides com.google.gson.JsonElement.getAsString.

Convenience method to get this array as a String if it contains a single element.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this array.

public boolean

Returns:

true if the array is empty.
isEmpty
()

Returns true if the array is empty.

public Iterator<JsonElement>

Returns:

an iterator to navigate the elements of the array.
iterator
()

Implements java.lang.Iterable.iterator.

Returns an iterator to navigate the elements of the array.

public boolean

Returns:

true if this array contained the specified element, false otherwise
remove
(JsonElement
element to be removed from this array, if present
element
)

Removes the first occurrence of the specified element from this array, if it is present.

public JsonElement

Returns:

the element previously at the specified position
remove
(int
index the index of the element to be removed
index
)

Removes the element at the specified position in this array.

public JsonElement

Returns:

the element previously at the specified position
set
(int
index of the element to replace
index
,
JsonElement
element to be stored at the specified position
element
)

Replaces the element at the specified position in this array with the specified element.

public int

Returns:

the number of elements in the array.
size
()

Returns the number of elements in the array.

Inherited from com.google.gson.JsonElement:
getAsJsonArraygetAsJsonNullgetAsJsonObjectgetAsJsonPrimitiveisJsonArrayisJsonNullisJsonObjectisJsonPrimitivetoString

Field Detail

elementsback to summary
private final ArrayList<JsonElement> elements

Constructor Detail

JsonArrayback to summary
public JsonArray()

Creates an empty JsonArray.

Annotations
@SuppressWarnings:deprecation
JsonArrayback to summary
public JsonArray(int capacity)

Creates an empty JsonArray with the desired initial capacity.

Parameters
capacity:int

initial capacity.

Annotations
@SuppressWarnings:deprecation
Exceptions
IllegalArgumentException:
if the capacity is negative
Since
2.8.1

Method Detail

addback to summary
public void add(Boolean bool)

Adds the specified boolean to self.

Parameters
bool:Boolean

the boolean that needs to be added to the array.

Since
2.4
addback to summary
public void add(Character character)

Adds the specified character to self.

Parameters
character:Character

the character that needs to be added to the array.

Since
2.4
addback to summary
public void add(Number number)

Adds the specified number to self.

Parameters
number:Number

the number that needs to be added to the array.

Since
2.4
addback to summary
public void add(String string)

Adds the specified string to self.

Parameters
string:String

the string that needs to be added to the array.

Since
2.4
addback to summary
public void add(JsonElement element)

Adds the specified element to self.

Parameters
element:JsonElement

the element that needs to be added to the array.

addAllback to summary
public void addAll(JsonArray array)

Adds all the elements of the specified array to self.

Parameters
array:JsonArray

the array whose elements need to be added to the array.

asListback to summary
public List<JsonElement> asList()

Returns a mutable List view of this JsonArray. Changes to the List are visible in this JsonArray and the other way around.

The List does not permit null elements. Unlike JsonArray's null handling, a NullPointerException is thrown when trying to add null. Use JsonNull for JSON null values.

Returns:List<JsonElement>

mutable List view

Since
2.10
containsback to summary
public boolean contains(JsonElement element)

Returns true if this array contains the specified element.

Parameters
element:JsonElement

whose presence in this array is to be tested

Returns:boolean

true if this array contains the specified element.

Since
2.3
deepCopyback to summary
public JsonArray deepCopy()

Implements abstract com.google.gson.JsonElement.deepCopy.

Creates a deep copy of this element and all its children.

Annotations
@Override
Since
2.8.2
equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Returns whether the other object is equal to this. This method only considers the other object to be equal if it is an instance of JsonArray and has equal elements in the same order.

Annotations
@Override
getback to summary
public JsonElement get(int i)

Returns the i-th element of the array.

Parameters
i:int

the index of the element that is being sought.

Returns:JsonElement

the element present at the i-th index.

Exceptions
IndexOutOfBoundsException:
if i is negative or greater than or equal to the size() of the array.
getAsBigDecimalback to summary
public BigDecimal getAsBigDecimal()

Overrides com.google.gson.JsonElement.getAsBigDecimal.

Convenience method to get this array as a BigDecimal if it contains a single element. This method calls JsonElement#getAsBigDecimal() on the element, therefore any of the exceptions declared by that method can occur.

Returns:BigDecimal

this element as a BigDecimal if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
Since
1.2
getAsBigIntegerback to summary
public BigInteger getAsBigInteger()

Overrides com.google.gson.JsonElement.getAsBigInteger.

Convenience method to get this array as a BigInteger if it contains a single element. This method calls JsonElement#getAsBigInteger() on the element, therefore any of the exceptions declared by that method can occur.

Returns:BigInteger

this element as a BigInteger if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
Since
1.2
getAsBooleanback to summary
public boolean getAsBoolean()

Overrides com.google.gson.JsonElement.getAsBoolean.

Convenience method to get this array as a boolean if it contains a single element. This method calls JsonElement#getAsBoolean() on the element, therefore any of the exceptions declared by that method can occur.

Returns:boolean

this element as a boolean if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsByteback to summary
public byte getAsByte()

Overrides com.google.gson.JsonElement.getAsByte.

Convenience method to get this array as a primitive byte if it contains a single element. This method calls JsonElement#getAsByte() on the element, therefore any of the exceptions declared by that method can occur.

Returns:byte

this element as a primitive byte if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsCharacterback to summary
public char getAsCharacter()

Overrides com.google.gson.JsonElement.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 calls JsonElement#getAsCharacter() on the element, therefore any of the exceptions declared by that method can occur.

Returns:char

this element as a primitive short if it is single element array.

Annotations
@Deprecated
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsDoubleback to summary
public double getAsDouble()

Overrides com.google.gson.JsonElement.getAsDouble.

Convenience method to get this array as a double if it contains a single element. This method calls JsonElement#getAsDouble() on the element, therefore any of the exceptions declared by that method can occur.

Returns:double

this element as a double if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsFloatback to summary
public float getAsFloat()

Overrides com.google.gson.JsonElement.getAsFloat.

Convenience method to get this array as a float if it contains a single element. This method calls JsonElement#getAsFloat() on the element, therefore any of the exceptions declared by that method can occur.

Returns:float

this element as a float if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsIntback to summary
public int getAsInt()

Overrides com.google.gson.JsonElement.getAsInt.

Convenience method to get this array as an integer if it contains a single element. This method calls JsonElement#getAsInt() on the element, therefore any of the exceptions declared by that method can occur.

Returns:int

this element as an integer if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsLongback to summary
public long getAsLong()

Overrides com.google.gson.JsonElement.getAsLong.

Convenience method to get this array as a long if it contains a single element. This method calls JsonElement#getAsLong() on the element, therefore any of the exceptions declared by that method can occur.

Returns:long

this element as a long if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsNumberback to summary
public Number getAsNumber()

Overrides com.google.gson.JsonElement.getAsNumber.

Convenience method to get this array as a Number if it contains a single element. This method calls JsonElement#getAsNumber() on the element, therefore any of the exceptions declared by that method can occur.

Returns:Number

this element as a number if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsShortback to summary
public short getAsShort()

Overrides com.google.gson.JsonElement.getAsShort.

Convenience method to get this array as a primitive short if it contains a single element. This method calls JsonElement#getAsShort() on the element, therefore any of the exceptions declared by that method can occur.

Returns:short

this element as a primitive short if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
getAsSingleElementback to summary
private JsonElement getAsSingleElement()
getAsStringback to summary
public String getAsString()

Overrides com.google.gson.JsonElement.getAsString.

Convenience method to get this array as a String if it contains a single element. This method calls JsonElement#getAsString() on the element, therefore any of the exceptions declared by that method can occur.

Returns:String

this element as a String if it is single element array.

Annotations
@Override
Exceptions
IllegalStateException:
if the array is empty or has more than one element.
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this array. This method calculates the hash code based on the elements of this array.

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Returns true if the array is empty.

Returns:boolean

true if the array is empty.

Since
2.8.7
iteratorback to summary
public Iterator<JsonElement> iterator()

Implements java.lang.Iterable.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.

Returns:Iterator<JsonElement>

an iterator to navigate the elements of the array.

Annotations
@Override
removeback to summary
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:JsonElement

element to be removed from this array, if present

Returns:boolean

true if this array contained the specified element, false otherwise

Annotations
@CanIgnoreReturnValue
Since
2.3
removeback to summary
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:int

index the index of the element to be removed

Returns:JsonElement

the element previously at the specified position

Annotations
@CanIgnoreReturnValue
Exceptions
IndexOutOfBoundsException:
if the specified index is outside the array bounds
Since
2.3
setback to summary
public JsonElement set(int index, JsonElement element)

Replaces the element at the specified position in this array with the specified element.

Parameters
index:int

index of the element to replace

element:JsonElement

element to be stored at the specified position

Returns:JsonElement

the element previously at the specified position

Annotations
@CanIgnoreReturnValue
Exceptions
IndexOutOfBoundsException:
if the specified index is outside the array bounds
sizeback to summary
public int size()

Returns the number of elements in the array.

Returns:int

the number of elements in the array.