Class JsonArray
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Object>,Collection<Object>,List<Object>,RandomAccess
Values 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, values that are
assigned through list iterators bypass this conversion.
The typed retrieval methods are lenient: strings are parsed into the requested numeric or boolean types, numbers are truncated when narrower types are requested, and null elements 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.- Since:
- 1.10
-
JsonArray
Creates a JSON array with the elements of a collection.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
-
-
Method Details
-
parse
Parses a JSON document that is expected to be a JSON array.This is equivalent to
Json.parseArray(String)and also 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
-
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.- Parameters:
value- the value to append- Returns:
- this
JsonArrayinstance - Since:
- 1.10
-
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.- Parameters:
action- the action that constructs the nested object- Returns:
- this
JsonArrayinstance - Since:
- 1.10
-
array
Appends a new nested JSON array that is constructed by the provided action.- Parameters:
action- the action that constructs the nested array- Returns:
- this
JsonArrayinstance - Since:
- 1.10
-
getString
Retrieves an element as a string.- Parameters:
index- the index of the element- Returns:
- the element value as a string; or
nullwhen null - Since:
- 1.10
-
getInt
public int getInt(int index) Retrieves an element as an int.- Parameters:
index- the index of the element- Returns:
- the element value as an int; or
0when null - Since:
- 1.10
-
getLong
public long getLong(int index) Retrieves an element as a long.- Parameters:
index- the index of the element- Returns:
- the element value as a long; or
0Lwhen null - Since:
- 1.10
-
getDouble
public double getDouble(int index) Retrieves an element as a double.- Parameters:
index- the index of the element- Returns:
- the element value as a double; or
0.0when null - Since:
- 1.10
-
getBoolean
public boolean getBoolean(int index) Retrieves an element as a boolean.- Parameters:
index- the index of the element- Returns:
- the element value as a boolean; or
falsewhen null - Since:
- 1.10
-
getDate
Retrieves an element as a date.ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as a
Date; ornullwhen null - Since:
- 1.10
-
getInstant
Retrieves an element as an instant.ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as an
Instant; ornullwhen null - Since:
- 1.10
-
getLocalDate
Retrieves an element as a local date.ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as a
LocalDate; ornullwhen null - Since:
- 1.10
-
getLocalDateTime
Retrieves an element as a local date and time.ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as a
LocalDateTime; ornullwhen null - Since:
- 1.10
-
getLocalTime
Retrieves an element as a local time.ISO 8601 strings and epoch millisecond numbers convert through RIFE2's standard conversions.
- Parameters:
index- the index of the element- Returns:
- the element value as a
LocalTime; ornullwhen null - Since:
- 1.10
-
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>),nullelements staynull.- Parameters:
beanClass- the class of the beans- Returns:
- the list with a bean instance for each element
- Since:
- 1.10
-
getObject
Retrieves an element as a nested JSON object.- Parameters:
index- the index of the element- Returns:
- the nested
JsonObject; ornullwhen null - Since:
- 1.10
-
getArray
Retrieves an element as a nested JSON array.- Parameters:
index- the index of the element- Returns:
- the nested
JsonArray; ornullwhen null - Since:
- 1.10
-
toString
Serializes this JSON array into its compact string representation.- Overrides:
toStringin classAbstractCollection<Object>- Returns:
- the JSON string
- Since:
- 1.10
-
toPrettyString
Serializes this JSON array into its multi-line indented string representation.- Returns:
- the pretty-printed JSON string
- Since:
- 1.10
-
print
Serializes this JSON array in its compact representation to a writer.The output is streamed, no intermediate string is built.
- Parameters:
writer- the writer to serialize to- Throws:
IOException- when an error occurred while writing- Since:
- 1.10
-
prettyPrint
Serializes this JSON array in its multi-line indented representation to a writer.The output is streamed, no intermediate string is built.
- Parameters:
writer- the writer to serialize to- Throws:
IOException- when an error occurred while writing- Since:
- 1.10
-