Share via


JsonArray Class

public final class JsonArray
extends JsonElement

Model representing a JSON array.

Constructor Summary

Constructor Description
JsonArray()

Default constructor.

Method Summary

Modifier and Type Method and Description
JsonArray addElement(boolean element)

Adds a boolean element to the JSON array.

JsonArray addElement(int index, boolean element)

Adds a boolean element to the JSON array at the specified index.

JsonArray addElement(int index, JsonElement element)

Adds a JsonElement to the JSON array at the specified index.

JsonArray addElement(int index, Number element)

Adds a number element to the JSON array at the specified index.

JsonArray addElement(int index, String element)

Adds a string element to the JSON array at the specified index.

JsonArray addElement(JsonElement element)

Adds a JsonElement to the JSON array.

JsonArray addElement(Number element)

Adds a number element to the JSON array.

JsonArray addElement(String element)

Adds a string element to the JSON array.

static JsonArray fromJson(JsonReader jsonReader)

Deserializes a JSON array from a JsonReader.

JsonElement getElement(int index)

Gets the JsonElement at the specified index from the JsonArray.

boolean isArray()

Indicates whether the element is an array.

JsonElement removeElement(int index)

Removes the JsonElement at the specified index from the JsonArray.

JsonArray setElement(int index, boolean element)

Sets a specified boolean element at a specified index within the JsonArray.

JsonArray setElement(int index, JsonElement element)

Sets a specified JsonElement object at a specified index within the JsonArray.

JsonArray setElement(int index, Number element)

Sets a specified number element at a specified index within the JsonArray.

JsonArray setElement(int index, String element)

Sets a specified string element at a specified index within the JsonArray.

int size()

The number of elements in the JsonArray.

JsonWriter toJson(JsonWriter jsonWriter)
String toJsonString()

Methods inherited from JsonElement

Methods inherited from java.lang.Object

Constructor Details

JsonArray

public JsonArray()

Default constructor.

Method Details

addElement

public JsonArray addElement(boolean element)

Adds a boolean element to the JSON array. This element will be appended to the end of the array.

Parameters:

element - The boolean element to add to the array.

Returns:

The updated JsonArray object.

addElement

public JsonArray addElement(int index, boolean element)

Adds a boolean element to the JSON array at the specified index. This element will be inserted at the specified index and all elements at or after the index will be shifted.

Parameters:

index - The index at which to add the element.
element - The boolean element to add to the array.

Returns:

The updated JsonArray object.

addElement

public JsonArray addElement(int index, JsonElement element)

Adds a JsonElement to the JSON array at the specified index. This element will be inserted at the specified index and all elements at or after the index will be shifted.

Parameters:

index - The index at which to add the element.
element - The JsonElement to add to the array.

Returns:

The updated JsonArray object.

addElement

public JsonArray addElement(int index, Number element)

Adds a number element to the JSON array at the specified index. This element will be inserted at the specified index and all elements at or after the index will be shifted.

If the element is null this will be treated as a JSON null.

Parameters:

index - The index at which to add the element.
element - The number element to add to the array.

Returns:

The updated JsonArray object.

addElement

public JsonArray addElement(int index, String element)

Adds a string element to the JSON array at the specified index. This element will be inserted at the specified index and all elements at or after the index will be shifted.

If the element is null this will be treated as a JSON null.

Parameters:

index - The index at which to add the element.
element - The string element to add to the array.

Returns:

The updated JsonArray object.

addElement

public JsonArray addElement(JsonElement element)

Adds a JsonElement to the JSON array. This element will be appended to the end of the array.

Parameters:

element - The JsonElement to add to the array.

Returns:

The updated JsonArray object.

addElement

public JsonArray addElement(Number element)

Adds a number element to the JSON array. This element will be appended to the end of the array.

If the element is null this will be treated as a JSON null.

Parameters:

element - The number element to add to the array.

Returns:

The updated JsonArray object.

addElement

public JsonArray addElement(String element)

Adds a string element to the JSON array. This element will be appended to the end of the array.

If the element is null this will be treated as a JSON null.

Parameters:

element - The string element to add to the array.

Returns:

The updated JsonArray object.

fromJson

public static JsonArray fromJson(JsonReader jsonReader)

Deserializes a JSON array from a JsonReader.

If the JsonReader's current token is null, it is assumed the JsonReader hasn't begun reading and nextToken() will be called to begin reading.

After ensuring the JsonReader has begun reading, if the current token is not START_ARRAY, an IllegalStateException will be thrown. Otherwise, a JSON array representing the array will be created and returned.

Parameters:

jsonReader - The JsonReader to deserialize from.

Returns:

The deserialized JSON array.

Throws:

IOException

- If an error occurs while deserializing the JSON array.

getElement

public JsonElement getElement(int index)

Gets the JsonElement at the specified index from the JsonArray.

Parameters:

index - The index at which to get the element.

Returns:

The JsonElement at the specified index.

Throws:

IndexOutOfBoundsException

- If the index is less than zero or greater than or equal to #size().

isArray

public boolean isArray()

Indicates whether the element is an array.

Overrides:

JsonArray.isArray()

Returns:

boolean of whether this JsonElement object is of type JsonArray.

removeElement

public JsonElement removeElement(int index)

Removes the JsonElement at the specified index from the JsonArray. This will shift all elements after the specified index.

Parameters:

index - The index at which to remove the element.

Returns:

The removed JsonElement.

Throws:

IndexOutOfBoundsException

- If the index is less than zero or greater than or equal to #size().

setElement

public JsonArray setElement(int index, boolean element)

Sets a specified boolean element at a specified index within the JsonArray. This will replace the current JsonElement at the specified index with the newly specified boolean element.

Parameters:

index - The index at which to set the element.
element - The boolean element to set at the specified index.

Returns:

The updated JsonArray object.

setElement

public JsonArray setElement(int index, JsonElement element)

Sets a specified JsonElement object at a specified index within the JsonArray. This will replace the current JsonElement at the specified index with the newly specified JsonElement object.

Parameters:

index - The index at which to set the element.
element - The JsonElement to set at the specified index.

Returns:

The updated JsonArray object.

setElement

public JsonArray setElement(int index, Number element)

Sets a specified number element at a specified index within the JsonArray. This will replace the current JsonElement at the specified index with the newly specified number element.

If the element is null this will be treated as a JSON null.

Parameters:

index - The index at which to set the element.
element - The number element to set at the specified index.

Returns:

The updated JsonArray object.

setElement

public JsonArray setElement(int index, String element)

Sets a specified string element at a specified index within the JsonArray. This will replace the current JsonElement at the specified index with the newly specified string element.

If the element is null this will be treated as a JSON null.

Parameters:

index - The index at which to set the element.
element - The string element to set at the specified index.

Returns:

The updated JsonArray object.

size

public int size()

The number of elements in the JsonArray.

Returns:

The number of elements in the JsonArray.

toJson

public JsonWriter toJson(JsonWriter jsonWriter)

Parameters:

jsonWriter

Throws:

toJsonString

public String toJsonString()

Throws:

Applies to