public class JsonArray extends JsonValue implements java.lang.Iterable<JsonValue>
Elements can be added using the add(...)
methods which accept
instances of JsonValue
, strings, primitive numbers, and boolean
values. To replace an element of an array, use the set(int, ...)
methods.
Elements can be accessed by their index using get(int)
. This class
also supports iterating over the elements in document order using an
iterator()
or an enhanced for loop:
for (JsonValue value : jsonArray) { ... }
An equivalent List
can be obtained from the method values()
.
Note that this class is not thread-safe. If multiple threads
access a JsonArray
instance concurrently, while at least one of
these threads modifies the contents of this array, access to the instance
must be synchronized externally. Failure to do so may lead to an inconsistent
state.
This class is not supposed to be extended by clients.
Constructor and Description |
---|
JsonArray()
Creates a new empty JsonArray.
|
JsonArray(JsonArray array)
Creates a new JsonArray with the contents of the specified JSON array.
|
Modifier and Type | Method and Description |
---|---|
JsonArray |
add(boolean value)
Appends the JSON representation of the specified
boolean value
to the end of this array. |
JsonArray |
add(double value)
Appends the JSON representation of the specified
double value to
the end of this array. |
JsonArray |
add(float value)
Appends the JSON representation of the specified
float value to
the end of this array. |
JsonArray |
add(int value)
Appends the JSON representation of the specified
int value to
the end of this array. |
JsonArray |
add(JsonValue value)
Appends the specified JSON value to the end of this array.
|
JsonArray |
add(long value)
Appends the JSON representation of the specified
long value to
the end of this array. |
JsonArray |
add(java.lang.String value)
Appends the JSON representation of the specified string to the end of this
array.
|
JsonArray |
asArray()
Returns this JSON value as
JsonArray , assuming that this value
represents a JSON array. |
JsonValue |
cloneMe()
Added for PlantUML
|
boolean |
equals(java.lang.Object object)
Indicates whether a given object is "equal to" this JsonArray.
|
JsonValue |
get(int index)
Returns the value of the element at the specified position in this array.
|
int |
hashCode() |
boolean |
isArray()
Detects whether this value represents a JSON array.
|
boolean |
isEmpty()
Returns
true if this array contains no elements. |
java.util.Iterator<JsonValue> |
iterator()
Returns an iterator over the values of this array in document order.
|
static JsonArray |
readFrom(java.io.Reader reader)
Deprecated.
Use
Json.parse(Reader) .asArray() instead |
static JsonArray |
readFrom(java.lang.String string)
Deprecated.
Use
Json.parse(String) .asArray() instead |
JsonArray |
remove(int index)
Removes the element at the specified index from this array.
|
JsonArray |
set(int index,
boolean value)
Replaces the element at the specified position in this array with the JSON
representation of the specified
boolean value. |
JsonArray |
set(int index,
double value)
Replaces the element at the specified position in this array with the JSON
representation of the specified
double value. |
JsonArray |
set(int index,
float value)
Replaces the element at the specified position in this array with the JSON
representation of the specified
float value. |
JsonArray |
set(int index,
int value)
Replaces the element at the specified position in this array with the JSON
representation of the specified
int value. |
JsonArray |
set(int index,
JsonValue value)
Replaces the element at the specified position in this array with the
specified JSON value.
|
JsonArray |
set(int index,
long value)
Replaces the element at the specified position in this array with the JSON
representation of the specified
long value. |
JsonArray |
set(int index,
java.lang.String value)
Replaces the element at the specified position in this array with the JSON
representation of the specified string.
|
int |
size()
Returns the number of elements in this array.
|
static JsonArray |
unmodifiableArray(JsonArray array)
Returns an unmodifiable wrapper for the specified JsonArray.
|
java.util.List<JsonValue> |
values()
Returns a list of the values in this array in document order.
|
asBoolean, asDouble, asFloat, asInt, asLong, asObject, asString, isBoolean, isFalse, isNull, isNumber, isObject, isString, isTrue, toString, toString, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, writeTo, writeTo
public JsonArray()
public JsonArray(JsonArray array)
array
- the JsonArray to get the initial contents from, must not be
null
@Deprecated public static JsonArray readFrom(java.io.Reader reader) throws java.io.IOException
Json.parse(Reader)
.asArray()
instead
Characters are read in chunks and buffered internally, therefore wrapping an
existing reader in an additional BufferedReader
does
not improve reading performance.
reader
- the reader to read the JSON array fromjava.io.IOException
- if an I/O error occurs in the readerParseException
- if the input is not valid JSONjava.lang.UnsupportedOperationException
- if the input does not contain a JSON
array@Deprecated public static JsonArray readFrom(java.lang.String string)
Json.parse(String)
.asArray()
insteadstring
- the string that contains the JSON arrayParseException
- if the input is not valid JSONjava.lang.UnsupportedOperationException
- if the input does not contain a JSON
arraypublic static JsonArray unmodifiableArray(JsonArray array)
The returned JsonArray is backed by the given array and reflects subsequent
changes. Attempts to modify the returned JsonArray result in an
UnsupportedOperationException
.
array
- the JsonArray for which an unmodifiable JsonArray is to be
returnedpublic JsonArray add(int value)
int
value to
the end of this array.value
- the value to add to the arraypublic JsonArray add(long value)
long
value to
the end of this array.value
- the value to add to the arraypublic JsonArray add(float value)
float
value to
the end of this array.value
- the value to add to the arraypublic JsonArray add(double value)
double
value to
the end of this array.value
- the value to add to the arraypublic JsonArray add(boolean value)
boolean
value
to the end of this array.value
- the value to add to the arraypublic JsonArray add(java.lang.String value)
value
- the string to add to the arraypublic JsonArray add(JsonValue value)
value
- the JsonValue to add to the array, must not be null
public JsonArray set(int index, int value)
int
value.index
- the index of the array element to replacevalue
- the value to be stored at the specified array positionjava.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public JsonArray set(int index, long value)
long
value.index
- the index of the array element to replacevalue
- the value to be stored at the specified array positionjava.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public JsonArray set(int index, float value)
float
value.index
- the index of the array element to replacevalue
- the value to be stored at the specified array positionjava.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public JsonArray set(int index, double value)
double
value.index
- the index of the array element to replacevalue
- the value to be stored at the specified array positionjava.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public JsonArray set(int index, boolean value)
boolean
value.index
- the index of the array element to replacevalue
- the value to be stored at the specified array positionjava.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public JsonArray set(int index, java.lang.String value)
index
- the index of the array element to replacevalue
- the string to be stored at the specified array positionjava.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public JsonArray set(int index, JsonValue value)
index
- the index of the array element to replacevalue
- the value to be stored at the specified array position, must not
be null
java.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public JsonArray remove(int index)
index
- the index of the element to removejava.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public int size()
public boolean isEmpty()
true
if this array contains no elements.true
if this array contains no elementspublic JsonValue get(int index)
index
- the index of the array element to returnjava.lang.IndexOutOfBoundsException
- if the index is out of range, i.e.
index < 0
or
index >= size
public java.util.List<JsonValue> values()
public java.util.Iterator<JsonValue> iterator()
iterator
in interface java.lang.Iterable<JsonValue>
public boolean isArray()
JsonValue
JsonArray
.public JsonArray asArray()
JsonValue
JsonArray
, assuming that this value
represents a JSON array. If this is not the case, an exception is thrown.public boolean equals(java.lang.Object object)
JsonArray
and both arrays
contain the same list of values.
If two JsonArrays are equal, they will also produce the same JSON output.