Class JsonArray
If boxed return values are unboxed, the calling code needs to make sure to handle potential
NullPointerException
s.
The JsonArray
is backed by a List
and is intended to work similar to it API wise, but to only
allow to store such objects which can be represented by JSON.
-
Method Summary
Modifier and TypeMethodDescriptionadd
(boolean value) add
(double value) add
(int value) add
(long value) add
(JsonObject value) Append anJsonObject
element to theJsonArray
.Append an element to theJsonArray
.addNull()
Append a null element to theJsonArray
.boolean
Returns true if the object is part of the array.static JsonArray
create()
Creates an emptyJsonArray
.static JsonArray
create
(int initialCapacity) Creates an emptyJsonArray
.boolean
static JsonArray
Creates a newJsonArray
and populates it with the values supplied.static JsonArray
static JsonArray
fromJson
(byte[] jsonArray) static JsonArray
get
(int index) Retrieves the value by the position in theJsonArray
and does not cast it.getArray
(int index) getBigDecimal
(int index) Retrieves the value by the position in theJsonArray
and casts it toBigDecimal
.getBigInteger
(int index) Retrieves the value by the position in theJsonArray
and casts it toBigInteger
.boolean
getBoolean
(int index) getDouble
(int index) getInt
(int index) getLong
(int index) getNumber
(int index) getObject
(int index) Retrieves the value by the position in theJsonArray
and casts it toJsonObject
.getString
(int index) int
hashCode()
boolean
isEmpty()
Checks if theJsonArray
is empty or not.iterator()
int
size()
Returns the size of theJsonArray
.byte[]
toBytes()
Similar totoString()
but turns this array directly into an encoded byte array.toList()
toString()
Converts theJsonArray
into its JSON string representation.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
create
Creates an emptyJsonArray
.- Returns:
- an empty
JsonArray
.
-
create
Creates an emptyJsonArray
.- Parameters:
initialCapacity
- the initial capacity for this json array.- Returns:
- an empty
JsonArray
.
-
from
Creates a newJsonArray
and populates it with the values supplied.- Parameters:
items
- the items to be stored in theJsonArray
.- Returns:
- a populated
JsonArray
. - Throws:
IllegalArgumentException
- if an element type is not supported.
-
from
Returns a newJsonArray
containing items from the givenList
, in the same order they were returned by the list's iterator.Sub Maps and Iterables: If possible, contained Maps and Iterables are converted to JsonObject and JsonArray respectively. However, some restrictions apply. Any non-convertible item will raise a
ClassCastException
. If the sub-conversion raises an exception (like an InvalidArgumentException) then it is put as cause for the ClassCastException.- Parameters:
items
- the list of items to be stored in theJsonArray
.- Returns:
- a new
JsonArray
containing the elements from the given list, in the order they were returned by the list's iterator. - Throws:
IllegalArgumentException
- if one or more list elements is of unsupported type.NullPointerException
- if the given list is null.
-
fromJson
Static method to create aJsonArray
from a JSONString
.Not to be confused with
from(Object...)
from(aString) which will populate a new array with the string.The string is expected to be a valid JSON array representation (eg. starting with a '[').
- Parameters:
jsonArray
- the JSON String to convert to aJsonArray
.- Returns:
- the corresponding
JsonArray
. - Throws:
IllegalArgumentException
- if the given String is not valid JSON Array.
-
fromJson
-
get
Retrieves the value by the position in theJsonArray
and does not cast it.- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
Append an element to theJsonArray
.- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
. - Throws:
IllegalArgumentException
- if the value type is not supported.
-
addNull
Append a null element to theJsonArray
.This is equivalent to calling
add(Object)
with null.- Returns:
- the
JsonArray
.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getString
- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getLong
Retrieves the value by the position in theJsonArray
and casts it toLong
.Note that if value was stored as another numerical type, some truncation or rounding may occur.
- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getInt
Retrieves the value by the position in theJsonArray
and casts it toInteger
.Note that if value was stored as another numerical type, some truncation or rounding may occur.
- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getDouble
Retrieves the value by the position in theJsonArray
and casts it toDouble
.Note that if value was stored as another numerical type, some truncation or rounding may occur.
- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getBoolean
public boolean getBoolean(int index) - Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
Append anJsonObject
element to theJsonArray
.- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
. - See Also:
-
getObject
Retrieves the value by the position in theJsonArray
and casts it toJsonObject
.- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
. - See Also:
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getArray
- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
getBigInteger
Retrieves the value by the position in theJsonArray
and casts it toBigInteger
.- Parameters:
index
- the index of the value.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
getBigDecimal
Retrieves the value by the position in theJsonArray
and casts it toBigDecimal
.- Parameters:
index
- the index of the value.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
getNumber
- Parameters:
index
- the index of the value.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
toList
-
isEmpty
public boolean isEmpty()Checks if theJsonArray
is empty or not.- Returns:
- true if it is, false otherwise.
-
size
public int size()Returns the size of theJsonArray
.- Returns:
- the size.
-
contains
Returns true if the object is part of the array.- Parameters:
value
- the value to check.- Returns:
- true if it is part of the array, false otherwise.
-
iterator
-
toString
Converts theJsonArray
into its JSON string representation. -
toBytes
public byte[] toBytes()Similar totoString()
but turns this array directly into an encoded byte array.- Returns:
- the byte array representing this
JsonArray
.
-
equals
-
hashCode
public int hashCode()
-