Class JsonArray
If boxed return values are unboxed, the calling code needs to make sure to handle potential
NullPointerExceptions.
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(@Nullable JsonObject value) Append anJsonObjectelement to theJsonArray.Append an element to theJsonArray.addNull()Append a null element to theJsonArray.booleanReturns true if the object is part of the array.static JsonArraycreate()Creates an emptyJsonArray.static JsonArraycreate(int initialCapacity) Creates an emptyJsonArray.booleanstatic JsonArrayCreates a newJsonArrayand populates it with the values supplied.static JsonArraystatic JsonArrayfromJson(byte[] jsonArray) static JsonArray@Nullable Objectget(int index) Retrieves the value by the position in theJsonArrayand does not cast it.@Nullable JsonArraygetArray(int index) @Nullable BigDecimalgetBigDecimal(int index) Retrieves the value by the position in theJsonArrayand casts it toBigDecimal.@Nullable BigIntegergetBigInteger(int index) Retrieves the value by the position in theJsonArrayand casts it toBigInteger.booleangetBoolean(int index) @Nullable DoublegetDouble(int index) @Nullable IntegergetInt(int index) @Nullable LonggetLong(int index) @Nullable NumbergetNumber(int index) @Nullable JsonObjectgetObject(int index) Retrieves the value by the position in theJsonArrayand casts it toJsonObject.@Nullable StringgetString(int index) inthashCode()booleanisEmpty()Checks if theJsonArrayis empty or not.iterator()intsize()Returns the size of theJsonArray.byte[]toBytes()Similar totoString()but turns this array directly into an encoded byte array.toList()toString()Converts theJsonArrayinto its JSON string representation.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods 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 newJsonArrayand 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 newJsonArraycontaining 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
JsonArraycontaining 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 aJsonArrayfrom 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 theJsonArrayand 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 theJsonArrayand 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 theJsonArrayand 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 theJsonArrayand 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 anJsonObjectelement 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 theJsonArrayand 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 theJsonArrayand 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 theJsonArrayand 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 theJsonArrayis 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 theJsonArrayinto 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()
-