- java.lang.Object
-
- io.github.jopenlibs.vault.json.JsonValue
-
- io.github.jopenlibs.vault.json.JsonArray
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<JsonValue>
public class JsonArray extends JsonValue implements java.lang.Iterable<JsonValue>
Represents a JSON array, an ordered collection of JSON values.Elements can be added using the
add(...)methods which accept instances ofJsonValue, strings, primitive numbers, and boolean values. To replace an element of an array, use theset(int, ...)methods.Elements can be accessed by their index using
get(int). This class also supports iterating over the elements in document order using aniterator()or an enhanced for loop:for (JsonValue value : jsonArray) { ... }An equivalent
Listcan be obtained from the methodvalues().Note that this class is not thread-safe. If multiple threads access a
JsonArrayinstance 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:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonArrayadd(boolean value)Appends the JSON representation of the specifiedbooleanvalue to the end of this array.JsonArrayadd(double value)Appends the JSON representation of the specifieddoublevalue to the end of this array.JsonArrayadd(float value)Appends the JSON representation of the specifiedfloatvalue to the end of this array.JsonArrayadd(int value)Appends the JSON representation of the specifiedintvalue to the end of this array.JsonArrayadd(long value)Appends the JSON representation of the specifiedlongvalue to the end of this array.JsonArrayadd(JsonValue value)Appends the specified JSON value to the end of this array.JsonArrayadd(java.lang.String value)Appends the JSON representation of the specified string to the end of this array.JsonArrayasArray()Returns this JSON value asJsonArray, assuming that this value represents a JSON array.booleanequals(java.lang.Object object)Indicates whether some other object is "equal to" this one according to the contract specified inObject.equals(Object).JsonValueget(int index)Returns the value of the element at the specified position in this array.inthashCode()booleanisArray()Detects whether this value represents a JSON array.booleanisEmpty()Returnstrueif this array contains no elements.java.util.Iterator<JsonValue>iterator()Returns an iterator over the values of this array in document order.JsonArrayremove(int index)Removes the element at the specified index from this array.JsonArrayset(int index, boolean value)Replaces the element at the specified position in this array with the JSON representation of the specifiedbooleanvalue.JsonArrayset(int index, double value)Replaces the element at the specified position in this array with the JSON representation of the specifieddoublevalue.JsonArrayset(int index, float value)Replaces the element at the specified position in this array with the JSON representation of the specifiedfloatvalue.JsonArrayset(int index, int value)Replaces the element at the specified position in this array with the JSON representation of the specifiedintvalue.JsonArrayset(int index, long value)Replaces the element at the specified position in this array with the JSON representation of the specifiedlongvalue.JsonArrayset(int index, JsonValue value)Replaces the element at the specified position in this array with the specified JSON value.JsonArrayset(int index, java.lang.String value)Replaces the element at the specified position in this array with the JSON representation of the specified string.intsize()Returns the number of elements in this array.static JsonArrayunmodifiableArray(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.-
Methods inherited from class io.github.jopenlibs.vault.json.JsonValue
asBoolean, asDouble, asFloat, asInt, asLong, asObject, asString, isBoolean, isFalse, isNull, isNumber, isObject, isString, isTrue, toString, toString, writeTo, writeTo
-
-
-
-
Constructor Detail
-
JsonArray
public JsonArray()
Creates a new empty JsonArray.
-
JsonArray
public JsonArray(JsonArray array)
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 Detail
-
unmodifiableArray
public static JsonArray unmodifiableArray(JsonArray array)
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
public JsonArray add(int value)
Appends the JSON representation of the specifiedintvalue to the end of this array.- Parameters:
value- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
public JsonArray add(long value)
Appends the JSON representation of the specifiedlongvalue to the end of this array.- Parameters:
value- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
public JsonArray add(float value)
Appends the JSON representation of the specifiedfloatvalue to the end of this array.- Parameters:
value- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
public JsonArray add(double value)
Appends the JSON representation of the specifieddoublevalue to the end of this array.- Parameters:
value- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
public JsonArray add(boolean value)
Appends the JSON representation of the specifiedbooleanvalue to the end of this array.- Parameters:
value- the value to add to the array- Returns:
- the array itself, to enable method chaining
-
add
public JsonArray add(java.lang.String value)
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
public JsonArray add(JsonValue value)
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
public JsonArray set(int index, int value)
Replaces the element at the specified position in this array with the JSON representation of the specifiedintvalue.- 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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= size
-
set
public JsonArray set(int index, long value)
Replaces the element at the specified position in this array with the JSON representation of the specifiedlongvalue.- 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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= size
-
set
public JsonArray set(int index, float value)
Replaces the element at the specified position in this array with the JSON representation of the specifiedfloatvalue.- 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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= size
-
set
public JsonArray set(int index, double value)
Replaces the element at the specified position in this array with the JSON representation of the specifieddoublevalue.- 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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= size
-
set
public JsonArray set(int index, boolean value)
Replaces the element at the specified position in this array with the JSON representation of the specifiedbooleanvalue.- 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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= size
-
set
public 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.- 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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= size
-
set
public JsonArray set(int index, JsonValue value)
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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= size
-
remove
public JsonArray remove(int index)
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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= 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()
Returnstrueif this array contains no elements.- Returns:
trueif this array contains no elements
-
get
public JsonValue get(int index)
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:
java.lang.IndexOutOfBoundsException- if the index is out of range, i.e.index < 0orindex >= size
-
values
public java.util.List<JsonValue> 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
public java.util.Iterator<JsonValue> iterator()
Returns an iterator over the values of this array in document order. The returned iterator cannot be used to modify this array.- Specified by:
iteratorin interfacejava.lang.Iterable<JsonValue>- Returns:
- an iterator over the values of this array
-
isArray
public boolean isArray()
Description copied from class:JsonValueDetects whether this value represents a JSON array. If this is the case, this value is an instance ofJsonArray.
-
asArray
public JsonArray asArray()
Description copied from class:JsonValueReturns this JSON value asJsonArray, assuming that this value represents a JSON array. If this is not the case, an exception is thrown.
-
equals
public boolean equals(java.lang.Object object)
Description copied from class:JsonValueIndicates whether some other object is "equal to" this one according to the contract specified inObject.equals(Object).Two JsonValues are considered equal if and only if they represent the same JSON text. As a consequence, two given JsonObjects may be different even though they contain the same set of names with the same values, but in a different order.
-
-