Interface JsonArray
- All Superinterfaces:
JsonDocument
Elements are read by position. A JSON null element produces an empty optional;
accessing an element as the wrong type, narrowing a fraction to an integer, or overflowing the
requested numeric type throws IllegalArgumentException. Numeric strings and other scalar
types are never coerced. An index outside [0, size()) throws IndexOutOfBoundsException.
Nested objects and arrays are returned as JsonObject and JsonArray. For a bulk
typed view of a homogeneous array use valuesAs(Class); for heterogeneous or nested
generic shapes decode the enclosing payload with a serde.
- Author:
- Konstantin Pavlov
-
Method Summary
Modifier and TypeMethodDescriptionarrayOpt(int index) Returns the element atindexas an array, or an empty optional when it is JSON null.default JsonArrayReturns the array element atindexorfallbackwhen it is JSON null.default JsonArrayarrayValue(int index) Returns the required array element atindex.asList()Returns an immutable list representation.boolOpt(int index) Returns the element atindexas a boolean, or an empty optional when it is JSON null.default booleanboolOr(int index, boolean fallback) Returns the boolean element atindexorfallbackwhen it is JSON null.default booleanboolValue(int index) Returns the required boolean element atindex.decimalOpt(int index) Returns the element atindexas a number without precision loss.default BigDecimaldecimalOr(int index, BigDecimal fallback) Returns the decimal element atindexorfallbackwhen it is JSON null.default BigDecimaldecimalValue(int index) Returns the required number element atindexwithout precision loss.doubleOpt(int index) Returns the element atindexconverted to a finitedouble.default doubledoubleOr(int index, double fallback) Returns the finitedoubleelement atindexorfallbackwhen JSON null.default doubledoubleValue(int index) Returns the required finitedoubleelement atindex.static JsonArrayempty()Returns an empty JSON array.intOpt(int index) Returns the element atindexas an exactint.default intintOr(int index, int fallback) Returns the exactintelement atindexorfallbackwhen JSON null.default intintValue(int index) Returns the required exactintelement atindex.default booleanisEmpty()Returns whether the array has no elements.longOpt(int index) Returns the element atindexas an exactlong.default longlongOr(int index, long fallback) Returns the exactlongelement atindexorfallbackwhen JSON null.default longlongValue(int index) Returns the required exactlongelement atindex.objectOpt(int index) Returns the element atindexas an object, or an empty optional when it is JSON null.default JsonObjectobjectOr(int index, JsonObject fallback) Returns the object element atindexorfallbackwhen it is JSON null.default JsonObjectobjectValue(int index) Returns the required object element atindex.static JsonArrayCreates an immutable array by recursively snapshotting JSON-compatible values.intsize()Returns the number of elements.stringOpt(int index) Returns the element atindexas a string, or an empty optional when it is JSON null.default StringReturns the string element atindexorfallbackwhen it is JSON null.default StringstringValue(int index) Returns the required string element atindex.<T> List<T> Returns every element coerced toelement.Methods inherited from interface dev.tachyonmcp.api.json.JsonDocument
json, unwrap
-
Method Details
-
size
int size()Returns the number of elements.- Returns:
- the element count
-
isEmpty
default boolean isEmpty()Returns whether the array has no elements.- Returns:
- true if empty
-
objectOpt
Returns the element atindexas an object, or an empty optional when it is JSON null.- Parameters:
index- the element index- Returns:
- the object value, or empty if null
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
arrayOpt
Returns the element atindexas an array, or an empty optional when it is JSON null.- Parameters:
index- the element index- Returns:
- the array value, or empty if null
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
stringOpt
Returns the element atindexas a string, or an empty optional when it is JSON null.- Parameters:
index- the element index- Returns:
- the string value, or empty if null
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
boolOpt
Returns the element atindexas a boolean, or an empty optional when it is JSON null.- Parameters:
index- the element index- Returns:
- the boolean value, or empty if null
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
decimalOpt
Returns the element atindexas a number without precision loss.- Parameters:
index- the element index- Returns:
- the decimal value, or empty if null
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
intOpt
Returns the element atindexas an exactint.- Parameters:
index- the element index- Returns:
- the int value, or empty if null
- Throws:
IndexOutOfBoundsException- if the index is out of rangeIllegalArgumentException- if the value is not a number or is not exactly representable as anint
-
longOpt
Returns the element atindexas an exactlong.- Parameters:
index- the element index- Returns:
- the long value, or empty if null
- Throws:
IndexOutOfBoundsException- if the index is out of rangeIllegalArgumentException- if the value is not a number or is not exactly representable as along
-
doubleOpt
Returns the element atindexconverted to a finitedouble.- Parameters:
index- the element index- Returns:
- the double value, or empty if null
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
objectValue
Returns the required object element atindex.- Parameters:
index- the element index- Returns:
- the object value
- Throws:
IllegalArgumentException- if the element is JSON nullIndexOutOfBoundsException- if the index is out of range
-
arrayValue
Returns the required array element atindex.- Parameters:
index- the element index- Returns:
- the array value
- Throws:
IllegalArgumentException- if the element is JSON nullIndexOutOfBoundsException- if the index is out of range
-
stringValue
Returns the required string element atindex.- Parameters:
index- the element index- Returns:
- the string value
- Throws:
IllegalArgumentException- if the element is JSON nullIndexOutOfBoundsException- if the index is out of range
-
boolValue
default boolean boolValue(int index) Returns the required boolean element atindex.- Parameters:
index- the element index- Returns:
- the boolean value
- Throws:
IllegalArgumentException- if the element is JSON nullIndexOutOfBoundsException- if the index is out of range
-
decimalValue
Returns the required number element atindexwithout precision loss.- Parameters:
index- the element index- Returns:
- the decimal value
- Throws:
IllegalArgumentException- if the element is JSON nullIndexOutOfBoundsException- if the index is out of range
-
intValue
default int intValue(int index) Returns the required exactintelement atindex.- Parameters:
index- the element index- Returns:
- the int value
- Throws:
IllegalArgumentException- if the element is JSON nullIndexOutOfBoundsException- if the index is out of range
-
longValue
default long longValue(int index) Returns the required exactlongelement atindex.- Parameters:
index- the element index- Returns:
- the long value
- Throws:
IllegalArgumentException- if the element is JSON nullIndexOutOfBoundsException- if the index is out of range
-
doubleValue
default double doubleValue(int index) Returns the required finitedoubleelement atindex.- Parameters:
index- the element index- Returns:
- the double value
- Throws:
IllegalArgumentException- if the element is JSON nullIndexOutOfBoundsException- if the index is out of range
-
objectOr
Returns the object element atindexorfallbackwhen it is JSON null.- Parameters:
index- the element indexfallback- the fallback value- Returns:
- the object value, or fallback
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
arrayOr
Returns the array element atindexorfallbackwhen it is JSON null.- Parameters:
index- the element indexfallback- the fallback value- Returns:
- the array value, or fallback
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
stringOr
Returns the string element atindexorfallbackwhen it is JSON null.- Parameters:
index- the element indexfallback- the fallback value- Returns:
- the string value, or fallback
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
boolOr
default boolean boolOr(int index, boolean fallback) Returns the boolean element atindexorfallbackwhen it is JSON null.- Parameters:
index- the element indexfallback- the fallback value- Returns:
- the boolean value, or fallback
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
decimalOr
Returns the decimal element atindexorfallbackwhen it is JSON null.- Parameters:
index- the element indexfallback- the fallback value- Returns:
- the decimal value, or fallback
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
intOr
default int intOr(int index, int fallback) Returns the exactintelement atindexorfallbackwhen JSON null.- Parameters:
index- the element indexfallback- the fallback value- Returns:
- the int value, or fallback
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
longOr
default long longOr(int index, long fallback) Returns the exactlongelement atindexorfallbackwhen JSON null.- Parameters:
index- the element indexfallback- the fallback value- Returns:
- the long value, or fallback
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
doubleOr
default double doubleOr(int index, double fallback) Returns the finitedoubleelement atindexorfallbackwhen JSON null.- Parameters:
index- the element indexfallback- the fallback value- Returns:
- the double value, or fallback
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
valuesAs
Returns every element coerced toelement.Supported element types:
String,Boolean,BigDecimal,Integer,Long,Double,JsonObject, andJsonArray. A JSON null, wrong-typed, or overflowing element throwsIllegalArgumentExceptionnaming its index. Bean and nested generic element types are not supported — decode the enclosing payload with a serde instead.- Type Parameters:
T- the element type- Parameters:
element- the element class- Returns:
- an immutable list of the coerced elements
- Throws:
IllegalArgumentException- if the element type is unsupported or an element cannot be coerced
-
asList
Returns an immutable list representation.- Returns:
- the list view
-
of
Creates an immutable array by recursively snapshotting JSON-compatible values.- Parameters:
values- the source list- Returns:
- a new JSON array
-
empty
Returns an empty JSON array.- Returns:
- the empty instance
-