Class JsonArray
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Object>,Collection<Object>,List<Object>,RandomAccess
JsonArray represents a JSON array as a regular list, with
additional fluent construction methods and typed value retrieval.
The values that are stored are plain Java instances: String,
Long, Double, Boolean, null,
JsonObject and JsonArray.
All the mutation methods convert maps, collections and arrays to
JsonObject and JsonArray instances, while values that
are assigned through sublist views bypass this conversion.
The typed retrieval methods are lenient. Strings will be parsed into the requested numeric or boolean types and numbers will be truncated when narrower types are requested. Elements that are null return zero-like values.
- Since:
- 1.10
- See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty JSON array.JsonArray(Collection<?> elements) Creates a JSON array with the elements of a collection. -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanbooleanaddAll(int index, Collection<?> elements) booleanaddAll(Collection<?> elements) Appends a value to this JSON array.array(JsonArrayAction action) Appends a new nested JSON array that is constructed by the provided action.getArray(int index) Retrieves an element as a nested JSON array.booleangetBoolean(int index) Retrieves an element as a boolean.getDate(int index) Retrieves an element as a date.doublegetDouble(int index) Retrieves an element as a double.getInstant(int index) Retrieves an element as an instant.intgetInt(int index) Retrieves an element as an int.getLocalDate(int index) Retrieves an element as a local date.getLocalDateTime(int index) Retrieves an element as a local date and time.getLocalTime(int index) Retrieves an element as a local time.longgetLong(int index) Retrieves an element as a long.getObject(int index) Retrieves an element as a nested JSON object.getString(int index) Retrieves an element as a string.object(JsonObjectAction action) Appends a new nested JSON object that is constructed by the provided action.static JsonArrayParses a JSON document that is expected to be a JSON array.voidprettyPrint(Writer writer) Serializes this JSON array in its multi-line indented representation to a writer.voidSerializes this JSON array in its compact representation to a writer.voidreplaceAll(UnaryOperator<Object> operator) <T> List<T>toBeanList(Class<T> beanClass) Converts the elements of this JSON array into a list of beans.Serializes this JSON array into its multi-line indented string representation.toString()Serializes this JSON array into its compact string representation.Methods inherited from class java.util.ArrayList
clear, clone, contains, ensureCapacity, equals, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, retainAll, size, sort, spliterator, subList, toArray, toArray, trimToSizeMethods inherited from class java.util.AbstractCollection
containsAllMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, stream, toArrayMethods inherited from interface java.util.List
containsAll
-
Constructor Details
-
JsonArray
public JsonArray()Creates an empty JSON array.Elements can afterwards be added with the regular list methods or with the fluent construction methods.
- Since:
- 1.10
- See Also:
-
JsonArray
Creates a JSON array with the elements of a collection.This constructor will copy over the elements of the provided collection in the collection's iteration order, while nested maps, collections and arrays are converted to
JsonObjectandJsonArrayinstances.- Parameters:
elements- the collection whose elements become the elements of this JSON array, in the collection's iteration order- Since:
- 1.10
- See Also:
-
-
Method Details
-
parse
Parses a JSON document that is expected to be a JSON array.This method does the same as
Json.parseArray(String)and additionally enables RIFE2's standard conversions to convert strings toJsonArrayinstances.- Parameters:
json- the JSON document to parse- Returns:
- the parsed
JsonArray - Throws:
JsonParseException- when the document couldn't be parsed or isn't a JSON array- Since:
- 1.10
- See Also:
-
append
Appends a value to this JSON array.Maps, collections and arrays are converted to
JsonObjectandJsonArrayinstances so that the typed retrieval methods work on them too.- Parameters:
value- the value to append- Returns:
- this
JsonArrayinstance - Since:
- 1.10
- See Also:
-
add
-
add
-
addAll
-
addAll
-
set
-
replaceAll
- Specified by:
replaceAllin interfaceList<Object>- Overrides:
replaceAllin classArrayList<Object>
-
object
Appends a new nested JSON object that is constructed by the provided action.This method will create the nested
JsonObjectfor you and hand it to the action so that you can populate it inline, after which it is appended to this JSON array.- Parameters:
action- the action that constructs the nested object- Returns:
- this
JsonArrayinstance - Since:
- 1.10
- See Also:
-
array
Appends a new nested JSON array that is constructed by the provided action.This method will create the nested
JsonArrayfor you and hand it to the action so that you can populate it inline, after which it is appended to this JSON array.- Parameters:
action- the action that constructs the nested array- Returns:
- this
JsonArrayinstance - Since:
- 1.10
- See Also:
-
getString
Retrieves an element as a string.- Parameters:
index- the index of the element- Returns:
- the element value as a string; or
nullif the element is null - Since:
- 1.10
- See Also:
-
getInt
public int getInt(int index) Retrieves an element as an int.Strings will be parsed and wider numbers will be truncated so that they fit into an int.
- Parameters:
index- the index of the element- Returns:
- the element value as an int; or
0if the element is null - Throws:
NumberFormatException- when the value is a string that can't be parsed into this typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
getLong
public long getLong(int index) Retrieves an element as a long.Strings will be parsed and floating point numbers will be truncated so that they fit into a long.
- Parameters:
index- the index of the element- Returns:
- the element value as a long; or
0Lif the element is null - Throws:
NumberFormatException- when the value is a string that can't be parsed into this typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
getDouble
public double getDouble(int index) Retrieves an element as a double.Strings will be parsed so that they can be used as a double too.
- Parameters:
index- the index of the element- Returns:
- the element value as a double; or
0.0if the element is null - Throws:
NumberFormatException- when the value is a string that can't be parsed into this typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
getBoolean
public boolean getBoolean(int index) Retrieves an element as a boolean.Strings will be parsed so that they can be used as a boolean too.
- Parameters:
index- the index of the element- Returns:
- the element value as a boolean; or
falseif the element is null - Since:
- 1.10
- See Also:
-
getDate
Retrieves an element as a date.ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as a
Date; ornullif the element is null - Throws:
IllegalArgumentException- when the value can't be converted into this typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
getInstant
Retrieves an element as an instant.ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as an
Instant; ornullif the element is null - Throws:
IllegalArgumentException- when the value can't be converted into this typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
getLocalDate
Retrieves an element as a local date.ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as a
LocalDate; ornullif the element is null - Throws:
IllegalArgumentException- when the value can't be converted into this typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
getLocalDateTime
Retrieves an element as a local date and time.ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as a
LocalDateTime; ornullif the element is null - Throws:
IllegalArgumentException- when the value can't be converted into this typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
getLocalTime
Retrieves an element as a local time.ISO 8601 strings and epoch millisecond numbers are converted through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as a
LocalTime; ornullif the element is null - Throws:
IllegalArgumentException- when the value can't be converted into this typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
toBeanList
Converts the elements of this JSON array into a list of beans.Each element is converted with the same rules as
Json.toBean(rife.json.JsonObject, java.lang.Class<T>), while elements that arenullstaynull.- Parameters:
beanClass- the class of the beans- Returns:
- the list with a bean instance for each element
- Since:
- 1.10
- See Also:
-
getObject
Retrieves an element as a nested JSON object.- Parameters:
index- the index of the element- Returns:
- the nested
JsonObject; ornullif the element is null - Throws:
ClassCastException- when the value is of another typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
getArray
Retrieves an element as a nested JSON array.- Parameters:
index- the index of the element- Returns:
- the nested
JsonArray; ornullif the element is null - Throws:
ClassCastException- when the value is of another typeIndexOutOfBoundsException- when the index is out of range- Since:
- 1.10
- See Also:
-
toString
Serializes this JSON array into its compact string representation.- Overrides:
toStringin classAbstractCollection<Object>- Returns:
- the JSON string
- Since:
- 1.10
- See Also:
-
toPrettyString
Serializes this JSON array into its multi-line indented string representation.- Returns:
- the pretty-printed JSON string
- Since:
- 1.10
- See Also:
-
print
Serializes this JSON array in its compact representation to a writer.The output is streamed so that no intermediate string has to be built up in memory.
- Parameters:
writer- the writer to serialize to- Throws:
IOException- when an error occurred while writing- Since:
- 1.10
- See Also:
-
prettyPrint
Serializes this JSON array in its multi-line indented representation to a writer.The output is streamed so that no intermediate string has to be built up in memory.
- Parameters:
writer- the writer to serialize to- Throws:
IOException- when an error occurred while writing- Since:
- 1.10
- See Also:
-