Class JsonArray
- All Implemented Interfaces:
Serializable
,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.
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionadd
(boolean value) Appends the JSON representation of the specifiedboolean
value to the end of this array.add
(double value) Appends the JSON representation of the specifieddouble
value to the end of this array.add
(float value) Appends the JSON representation of the specifiedfloat
value to the end of this array.add
(int value) Appends the JSON representation of the specifiedint
value to the end of this array.add
(long value) Appends the JSON representation of the specifiedlong
value to the end of this array.Appends the specified JSON value to the end of this array.Appends the JSON representation of the specified string to the end of this array.asArray()
Returns this JSON value asJsonArray
, assuming that this value represents a JSON array.boolean
Indicates whether a given object is "equal to" this JsonArray.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()
Returnstrue
if this array contains no elements.iterator()
Returns an iterator over the values of this array in document order.static JsonArray
Deprecated.static JsonArray
Deprecated.UseJson.parse(String)
.asArray()
insteadremove
(int index) Removes the element at the specified index from this array.set
(int index, boolean value) Replaces the element at the specified position in this array with the JSON representation of the specifiedboolean
value.set
(int index, double value) Replaces the element at the specified position in this array with the JSON representation of the specifieddouble
value.set
(int index, float value) Replaces the element at the specified position in this array with the JSON representation of the specifiedfloat
value.set
(int index, int value) Replaces the element at the specified position in this array with the JSON representation of the specifiedint
value.set
(int index, long value) Replaces the element at the specified position in this array with the JSON representation of the specifiedlong
value.Replaces the element at the specified position in this array with the specified JSON 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.values()
Returns a list of the values in this array in document order.Methods inherited from class com.espertech.esper.common.client.json.minimaljson.JsonValue
asBoolean, asDouble, asFloat, asInt, asLong, asObject, asString, isBoolean, isFalse, isNull, isNumber, isObject, isString, isTrue, toString, toString, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, writeTo, writeTo
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
JsonArray
public JsonArray()Creates a new empty JsonArray. -
JsonArray
Creates a new JsonArray with the contents of the specified JSON array.- Parameters:
array
- the JsonArray to get the initial contents from, must not benull
-
-
Method Details
-
readFrom
Deprecated.UseJson.parse(Reader)
.asArray()
insteadReads a JSON array from the given reader.Characters are read in chunks and buffered internally, therefore wrapping an existing reader in an additional
BufferedReader
does not improve reading performance.- Parameters:
reader
- the reader to read the JSON array from- Returns:
- the JSON array that has been read
- Throws:
IOException
- if an I/O error occurs in the readerParseException
- if the input is not valid JSONUnsupportedOperationException
- if the input does not contain a JSON array
-
readFrom
Deprecated.UseJson.parse(String)
.asArray()
insteadReads a JSON array from the given string.- Parameters:
string
- the string that contains the JSON array- Returns:
- the JSON array that has been read
- Throws:
ParseException
- if the input is not valid JSONUnsupportedOperationException
- if the input does not contain a JSON array
-
unmodifiableArray
Returns an unmodifiable wrapper for the specified JsonArray. This method allows to provide read-only access to a JsonArray.The returned JsonArray is backed by the given array and reflects subsequent changes. Attempts to modify the returned JsonArray result in an
UnsupportedOperationException
.- Parameters:
array
- the JsonArray for which an unmodifiable JsonArray is to be returned- Returns:
- an unmodifiable view of the specified JsonArray
-
add
Appends the JSON representation of the specifiedint
value to the end of this array.- Parameters:
value
- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
Appends the JSON representation of the specifiedlong
value to the end of this array.- Parameters:
value
- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
Appends the JSON representation of the specifiedfloat
value to the end of this array.- Parameters:
value
- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
Appends the JSON representation of the specifieddouble
value to the end of this array.- Parameters:
value
- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
Appends the JSON representation of the specifiedboolean
value to the end of this array.- Parameters:
value
- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
Appends the JSON representation of the specified string to the end of this array.- Parameters:
value
- the string to add to the array- Returns:
- the array itself, to enable method chaining
-
add
Appends the specified JSON value to the end of this array.- Parameters:
value
- the JsonValue to add to the array, must not benull
- Returns:
- the array itself, to enable method chaining
-
set
Replaces the element at the specified position in this array with the JSON representation of the specifiedint
value.- Parameters:
index
- the index of the array element to replacevalue
- the value to be stored at the specified array position- Returns:
- the array itself, to enable method chaining
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
set
Replaces the element at the specified position in this array with the JSON representation of the specifiedlong
value.- Parameters:
index
- the index of the array element to replacevalue
- the value to be stored at the specified array position- Returns:
- the array itself, to enable method chaining
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
set
Replaces the element at the specified position in this array with the JSON representation of the specifiedfloat
value.- Parameters:
index
- the index of the array element to replacevalue
- the value to be stored at the specified array position- Returns:
- the array itself, to enable method chaining
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
set
Replaces the element at the specified position in this array with the JSON representation of the specifieddouble
value.- Parameters:
index
- the index of the array element to replacevalue
- the value to be stored at the specified array position- Returns:
- the array itself, to enable method chaining
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
set
Replaces the element at the specified position in this array with the JSON representation of the specifiedboolean
value.- Parameters:
index
- the index of the array element to replacevalue
- the value to be stored at the specified array position- Returns:
- the array itself, to enable method chaining
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
set
Replaces the element at the specified position in this array with the JSON representation of the specified string.- Parameters:
index
- the index of the array element to replacevalue
- the string to be stored at the specified array position- Returns:
- the array itself, to enable method chaining
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
set
Replaces the element at the specified position in this array with the specified JSON value.- Parameters:
index
- the index of the array element to replacevalue
- the value to be stored at the specified array position, must not benull
- Returns:
- the array itself, to enable method chaining
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
remove
Removes the element at the specified index from this array.- Parameters:
index
- the index of the element to remove- Returns:
- the array itself, to enable method chaining
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
size
public int size()Returns the number of elements in this array.- Returns:
- the number of elements in this array
-
isEmpty
public boolean isEmpty()Returnstrue
if this array contains no elements.- Returns:
true
if this array contains no elements
-
get
Returns the value of the element at the specified position in this array.- Parameters:
index
- the index of the array element to return- Returns:
- the value of the element at the specified position
- Throws:
IndexOutOfBoundsException
- if the index is out of range, i.e.index < 0
orindex >= size
-
values
Returns a list of the values in this array in document order. The returned list is backed by this array and will reflect subsequent changes. It cannot be used to modify this array. Attempts to modify the returned list will result in an exception.- Returns:
- a list of the values in this array
-
iterator
Returns an iterator over the values of this array in document order. The returned iterator cannot be used to modify this array. -
isArray
public boolean isArray()Description copied from class:JsonValue
Detects whether this value represents a JSON array. If this is the case, this value is an instance ofJsonArray
. -
asArray
Description copied from class:JsonValue
Returns this JSON value asJsonArray
, assuming that this value represents a JSON array. If this is not the case, an exception is thrown. -
hashCode
public int hashCode() -
equals
Indicates whether a given object is "equal to" this JsonArray. An object is considered equal if it is also aJsonArray
and both arrays contain the same list of values.If two JsonArrays are equal, they will also produce the same JSON output.
-
Json.parse(Reader)
.asArray()
instead