A JsArray
is a collection of JSON values arranged in an ordered sequence. It allows for easy manipulation of JSON arrays, such as adding, updating, or removing elements, while preserving immutability. This class is designed to be used for creating and working with JSON arrays in a functional and safe manner.
JsArray
implements the Json
interface, which allows it to seamlessly integrate with other JSON-related classes and operations.
As a persistent data structure, modifications to a JsArray
result in a new instance being created, leaving the original array unchanged. This immutability ensures safe concurrent access and simplifies handling of JSON data.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Enumeration representing different types of arrays: SET, LIST, or MULTISET. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final JsOptics.JsArrayLenses
Provides a set of optics (lenses) for working with JSON arrays.static final JsOptics.JsArrayOptionals
Provides a set of optional optics for working with JSON arrays. -
Method Summary
Modifier and TypeMethodDescriptionAppends one or moreJsValue
elements to the end of this JSON array.Appends all elements from anotherJsArray
to the end of this JSON array.boolean
containsValue
(JsValue el) Checks if this JSON contains the given value in its first level.delete
(int index) Deletes the element at the specified index from thisJsArray
.Removes the element at the specified path within this immutable JSON object, if it exists.static JsArray
empty()
Returns the singleton empty JSON arrayboolean
boolean
equals
(JsArray array, JsArray.TYPE ARRAY_AS) Checks if this JSON array is equal to another JSON array.filterKeys
(BiPredicate<? super JsPath, ? super JsValue> filter) Filters all the keys of this json, removing those that don't satisfy the given predicate.filterKeys
(Predicate<? super String> filter) Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.filterObjs
(BiPredicate<? super JsPath, ? super JsObj> filter) Filters all the pair of jsons of this json, removing those that don't satisfy the given predicate.filterObjs
(Predicate<? super JsObj> filter) Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.filterValues
(BiPredicate<? super JsPath, ? super JsPrimitive> filter) Filters all the pairs of elements of this json, removing those that don't satisfy the given predicate.filterValues
(Predicate<? super JsPrimitive> filter) Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.get
(int i) Returns the element located at the specified index orJsNothing
if it doesn't exist.Gets the value located at the given path orJsNothing
if it doesn't exist.getArray
(int index) Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a JSON array (JsArray).Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a JSON array (JsArray).getBigDec
(int index) Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a decimal number, returning it as a BigDecimal.getBigDec
(int index, Supplier<BigDecimal> orElse) Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a decimal number, returning it as a BigDecimal.getBigInt
(int index) Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as an integral number, returning it as a BigInteger.getBigInt
(int index, Supplier<BigInteger> orElse) Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as an integral number, returning it as a BigInteger.byte[]
getBinary
(int index) Retrieves the binary data located at the specified index within the JSON-like array and returns it as an array of bytes.byte[]
Retrieves the binary data located at the specified index within the JSON-like array and returns it as an array of bytes.getBool
(int index) Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a boolean.Retrieves the boolean value located at the specified index within the JSON-like array and returns it.getDouble
(int index) Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a decimal number, converting it to a double.Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a decimal number, converting it to a double.getInstant
(int index) Returns the instant located at the given index as anInstant
ornull
if it doesn't exist or is not a valid instant representation.getInstant
(int index, Supplier<Instant> orElse) getInt
(int index) Returns the integral number located at the given index as an integer or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.Returns the integral number located at the given index as an integer or a default value if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.getLong
(int index) Returns the integral number located at the given index as a long or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in a long.Returns the integral number located at the given index as a long or a default value provided by theorElse
supplier if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in a long.getObj
(int index) Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a JSON object (JsObj).Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a JSON object (JsObj).getStr
(int index) Returns the string located at the given index ornull
if it doesn't exist or it's not a string.Returns the string located at the given index or a default value supplied by the providedorElse
function if it doesn't exist or is not a string.int
hashCode()
equals method is inherited, so it's implemented.head()
Returns the first element of this array.init()
Returns all the elements of this array except the last one.intersection
(JsArray that, JsArray.TYPE ARRAY_AS) this.union(that, SET)
returnsthis
plus those elements fromthat
that don't exist inthis
.boolean
isArray()
Returns true if this JsValue is a JsArray.boolean
isEmpty()
Determines whether this Json instance is empty, i.e., it contains no elements.iterator()
last()
Returns the last element of this array.mapKeys
(BiFunction<? super JsPath, ? super JsValue, String> fn) Maps all the keys of this JSON object, recursively traversing the entire JSON structure.Maps all the keys of this JSON object, recursively traversing the entire JSON structure.mapObjs
(BiFunction<? super JsPath, ? super JsObj, ? extends JsValue> fn) Maps all the JSON objects of this JSON, traversing the entire JSON if necessary.Maps all the JSON objects of this JSON, traversing the entire JSON if necessary.mapValues
(BiFunction<? super JsPath, ? super JsPrimitive, ? extends JsValue> fn) Recursively maps all the values of this JSON, replacing each value with the result of applying the given mapping function.mapValues
(Function<? super JsPrimitive, ? extends JsValue> fn) Recursively maps all the values of this JSON, replacing each value with the result of applying the given mapping function.static JsArray
of
(boolean bool, boolean... others) Creates a newJsArray
containing the specified boolean elements.static JsArray
of
(double number, double... others) Creates a newJsArray
containing the specifieddouble
elements.static JsArray
of
(int number, int... others) Creates a newJsArray
containing the specified integer elements.static JsArray
of
(long number, long... others) Creates a newJsArray
containing the specified long elements.static JsArray
Creates a newJsArray
containing the specified string elements.static JsArray
of
(BigInteger number, BigInteger... others) Creates a newJsArray
containing the specifiedBigInteger
elements.static JsArray
Creates a newJsArray
containing the specified elements.static JsArray
ofIterable
(Iterable<? extends JsValue> iterable) static JsArray
parse
(byte[] bytes) Parses a JSON array represented as a byte array and returns a newJsArray
instance.static JsArray
Parses a JSON array represented as a string and returns a newJsArray
instance.Adds one or more elements, starting from the last, to the front of this array.prependAll
(JsArray array) Adds all the elements of theJsArray
, starting from the last, to the front of this array.<R> Optional<R>
reduce
(BinaryOperator<R> op, BiFunction<? super JsPath, ? super JsPrimitive, R> map, BiPredicate<? super JsPath, ? super JsPrimitive> predicate) Reduces the values of this JSON object that satisfy the given predicate, allowing access to the element's path.<R> Optional<R>
reduce
(BinaryOperator<R> op, Function<? super JsPrimitive, R> map, Predicate<? super JsPrimitive> predicate) Reduces the values of this JSON object that satisfy the given predicate.Sets the value at the specified index within the JSON-like array to the provided JsValue.Inserts the given element at the specified path in this JSON, replacing any existing element.Inserts an element at the specified path in this JSON, replacing any existing element.int
size()
Returns the number of elements in the first level of this JSON object or array.stream()
Returns a stream over all the key-value pairsJsPair
of elements in this JSON object.Returns a non-recursive stream of values contained within the JSON-like array.tail()
Returns a JSON array consisting of all elements of this array except the first one.toString()
// Single-check idiom Item 83 from effective javaunion
(JsArray that, JsArray.TYPE ARRAY_AS) Computes the union of this JSON and another JSON object 'that' with respect to the given array merging strategy.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface jsonvalues.Json
containsPath, equals, getArray, getArray, getBigDec, getBigDec, getBigInt, getBigInt, getBinary, getBinary, getBool, getBool, getDouble, getDouble, getInstant, getInstant, getInt, getInt, getLong, getLong, getObj, getObj, getStr, getStr, ifEmptyElse, isNotEmpty, serialize, serialize, toJsPrimitive, toPrettyString, toPrettyString
Methods inherited from interface jsonvalues.JsValue
ifNothing, ifNull, isArray, isBigDec, isBigDec, isBigInt, isBigInt, isBinary, isBool, isDecimal, isDouble, isDouble, isFalse, isInstant, isInstant, isInt, isInt, isIntegral, isJson, isJson, isLong, isLong, isNothing, isNotNothing, isNotNull, isNotNumber, isNull, isNumber, isObj, isObj, isPrimitive, isSameType, isStr, isStr, isTrue, toJsArray, toJsBigDec, toJsBigInt, toJsBinary, toJsBool, toJsDouble, toJsInstant, toJsInt, toJsLong, toJsNumber, toJsObj, toJson, toJsStr
-
Field Details
-
lens
Provides a set of optics (lenses) for working with JSON arrays.The
JsArrayLenses
class offers a collection of optics that allow you to focus on specific elements within a JSON array, making it easier to perform targeted modifications and transformations. These optics are designed to work seamlessly with theJsArray
class, enabling precise handling of JSON array data. -
optional
Provides a set of optional optics for working with JSON arrays.The
JsArrayOptionals
class offers a collection of optional optics that allow you to work with JSON arrays more flexibly. These optics provide ways to safely access and manipulate elements within a JSON array, taking into account the possibility of absent or missing elements. -
prism
-
-
Method Details
-
empty
Returns the singleton empty JSON array- Returns:
- the singleton empty JSON array
-
of
Creates a newJsArray
containing the specified elements.The
of
method returns a newJsArray
instance with the specified elements in the order they appear in the method's argument list. You can provide one or moreJsValue
elements to include in the resulting array.- Parameters:
e
- The firstJsValue
element to include in the array.rest
- AdditionalJsValue
elements to include in the array (optional).- Returns:
- A new
JsArray
containing the specified elements. - Throws:
NullPointerException
- if any of the provided elements (includinge
andrest
) are null.
-
of
Creates a newJsArray
containing the specified string elements.The
of
method returns a newJsArray
instance with the specified string elements in the order they appear in the method's argument list. You can provide one or more strings to include in the resulting array.Example usage:
// Creating a JsArray with specified string elements JsArray array = JsArray.of("Hello", "World");
- Parameters:
str
- The first string element to include in the array.others
- Additional string elements to include in the array (optional).- Returns:
- A new
JsArray
containing the specified string elements. - Throws:
NullPointerException
- if any of the provided string elements (includingstr
andothers
) are null.
-
of
Creates a newJsArray
containing the specified integer elements.The
of
method returns a newJsArray
instance with the specified integer elements in the order they appear in the method's argument list. You can provide one or more integers to include in the resulting array.- Parameters:
number
- The first integer element to include in the array.others
- Additional integer elements to include in the array (optional).- Returns:
- A new
JsArray
containing the specified integer elements.
-
of
Creates a newJsArray
containing the specified boolean elements.The
of
method returns a newJsArray
instance with the specified boolean elements in the order they appear in the method's argument list. You can provide one or more boolean values to include in the resulting array.- Parameters:
bool
- The first boolean element to include in the array.others
- Additional boolean elements to include in the array (optional).- Returns:
- A new
JsArray
containing the specified boolean elements.
-
of
Creates a newJsArray
containing the specified long elements.The
of
method returns a newJsArray
instance with the specified long elements in the order they appear in the method's argument list. You can provide one or more long values to include in the resulting array.- Parameters:
number
- The first long element to include in the array.others
- Additional long elements to include in the array (optional).- Returns:
- A new
JsArray
containing the specified long elements.
-
of
Creates a newJsArray
containing the specifiedBigInteger
elements.The
of
method returns a newJsArray
instance with the specifiedBigInteger
elements in the order they appear in the method's argument list. You can provide one or moreBigInteger
values to include in the resulting array.- Parameters:
number
- The firstBigInteger
element to include in the array.others
- AdditionalBigInteger
elements to include in the array (optional).- Returns:
- A new
JsArray
containing the specifiedBigInteger
elements.
-
of
Creates a newJsArray
containing the specifieddouble
elements.The
of
method returns a newJsArray
instance with the specifieddouble
elements in the order they appear in the method's argument list. You can provide one or moredouble
values to include in the resulting array.- Parameters:
number
- The firstdouble
element to include in the array.others
- Additionaldouble
elements to include in the array (optional).- Returns:
- A new
JsArray
containing the specifieddouble
elements.
-
ofIterable
-
parse
Parses a JSON array represented as a string and returns a newJsArray
instance.The
parse
method takes a JSON array string as input, parses it, and returns a newJsArray
containing the parsed JSON values. If the input string is not a valid JSON array representation, aJsParserException
is thrown.- Parameters:
str
- The JSON array string to parse.- Returns:
- A new
JsArray
containing the parsed JSON values. - Throws:
JsParserException
- If the input string is not a valid JSON array representation.- See Also:
-
parse
Parses a JSON array represented as a byte array and returns a newJsArray
instance.The
parse
method takes a JSON array represented as a byte array, parses it, and returns a newJsArray
containing the parsed JSON values. If the input byte array does not represent a valid JSON array, aJsParserException
is thrown.- Parameters:
bytes
- The byte array representing the JSON array to parse.- Returns:
- A new
JsArray
containing the parsed JSON values. - Throws:
JsParserException
- If the input byte array does not represent a valid JSON array.- See Also:
-
append
-
appendAll
-
equals
Checks if this JSON array is equal to another JSON array.The
equals
method compares this JSON array to another JSON array to determine if they are equal. Equality is defined based on the specifiedJsArray.TYPE
of array comparison. IfARRAY_AS
isJsArray.TYPE.SET
, both arrays must contain the same elements (order doesn't matter, duplicates are ignored). IfARRAY_AS
isJsArray.TYPE.LIST
, both arrays must have the same elements in the same order. IfARRAY_AS
isJsArray.TYPE.MULTISET
, both arrays must contain the same elements (order doesn't matter, duplicates are counted).- Parameters:
array
- TheJsArray
to compare to this array.ARRAY_AS
- TheJsArray.TYPE
specifying the type of array comparison (SET, LIST, or MULTISET).- Returns:
true
if the arrays are equal according to the specified type,false
otherwise.- See Also:
-
getInt
Returns the integral number located at the given index as an integer or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.This method retrieves the JSON value at the specified index in the array and attempts to parse it as an integral number. If the value at the index is a valid integral number that can be represented as an integer, it is returned as an
Integer
. If the value is not an integral number or cannot fit in an integer, this method returnsnull
.Example usage:
// Create a JSON array and retrieve an integral number as an integer JsArray array = JsArray.of(1, 2, 3); Integer value = array.getInt(1); // Returns 2
- Parameters:
index
- The index of the JSON value to retrieve as an integer.- Returns:
- The integral number located at the given index as an
Integer
, ornull
if it doesn't exist or is not a valid integral number.
-
getInt
Returns the integral number located at the given index as an integer or a default value if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.This method retrieves the JSON value at the specified index in the array and attempts to parse it as an integral number. If the value at the index is a valid integral number that can be represented as an integer, it is returned as an
Integer
. If the value is not an integral number or cannot fit in an integer, the default value provided by theorElse
supplier is returned.- Parameters:
index
- The index of the JSON value to retrieve as an integer.orElse
- A supplier that provides a default integer value to return if the JSON value at the index is not a valid integral number or cannot fit in an integer.- Returns:
- The integral number located at the given index as an
Integer
, or the default value provided byorElse
if it doesn't exist or is not a valid integral number. - See Also:
-
getLong
Returns the integral number located at the given index as a long or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in a long.This method retrieves the JSON value at the specified index in the array and attempts to parse it as an integral number. If the value at the index is a valid integral number that can be represented as a long, it is returned as a
Long
. If the value is not an integral number or cannot fit in a long,null
is returned.- Parameters:
index
- The index of the JSON value to retrieve as a long.- Returns:
- The integral number located at the given index as a
Long
, ornull
if it doesn't exist or is not a valid integral number.
-
getLong
Returns the integral number located at the given index as a long or a default value provided by theorElse
supplier if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in a long.This method retrieves the JSON value at the specified index in the array and attempts to parse it as an integral number. If the value at the index is a valid integral number that can be represented as a long, it is returned as a
Long
. If the value is not an integral number or cannot fit in a long, theorElse
supplier is used to provide a default value.- Parameters:
index
- The index of the JSON value to retrieve as a long.orElse
- ASupplier
that provides the default value to return if the value at the given index is not a valid integral number or cannot fit in a long.- Returns:
- The integral number located at the given index as a
Long
, or the default value provided byorElse
if it doesn't exist or is not a valid integral number.
-
getStr
Returns the string located at the given index ornull
if it doesn't exist or it's not a string.This method retrieves the JSON value at the specified index in the array and attempts to interpret it as a string. If the value at the index is a string, it is returned as a
String
. If the value is not a string, or if it doesn't exist at the specified index,null
is returned.- Parameters:
index
- The index of the JSON value to retrieve as a string.- Returns:
- The string located at the given index, or
null
if it doesn't exist or is not a string.
-
getStr
Returns the string located at the given index or a default value supplied by the providedorElse
function if it doesn't exist or is not a string.This method retrieves the JSON value at the specified index in the array and attempts to interpret it as a string. If the value at the index is a string, it is returned as a
String
. If the value is not a string, or if it doesn't exist at the specified index, theorElse
function is invoked to provide a default value.- Parameters:
index
- The index of the JSON value to retrieve as a string.orElse
- ASupplier<String>
providing a default value if the value is not a string or doesn't exist.- Returns:
- The string located at the given index, or the result of invoking
orElse
if it doesn't exist or is not a string.
-
getInstant
Returns the instant located at the given index as anInstant
ornull
if it doesn't exist or is not a valid instant representation.This method retrieves the JSON value at the specified index in the array and attempts to interpret it as an instant. If the value at the index is a valid instant representation, it is returned as an
Instant
. If the value is not a valid instant representation, or if it doesn't exist at the specified index,null
is returned. -
getInstant
Returns the instant located at the given index as anInstant
or a defaultInstant
provided by theorElse
supplier if it doesn't exist or is not a valid instant representation.This method retrieves the JSON value at the specified index in the array and attempts to interpret it as an instant. If the value at the index is a valid instant representation, it is returned as an
Instant
. If the value is not a valid instant representation, or if it doesn't exist at the specified index, the defaultInstant
provided by theorElse
supplier is returned.- Parameters:
index
- The index of the JSON value to retrieve as anInstant
.orElse
- ASupplier
providing the defaultInstant
to return if the value at the specified index is not a valid instant representation or doesn't exist.- Returns:
- The instant located at the given index as an
Instant
, or the defaultInstant
provided by theorElse
supplier if it doesn't exist or is not a valid instant representation.
-
getBinary
public byte[] getBinary(int index) Retrieves the binary data located at the specified index within the JSON-like array and returns it as an array of bytes. If the element at the given index is either a binary array or a string encoded in Base64, it is converted into an array of bytes and returned. If the element does not exist at the specified index or is not a valid binary representation, this method returns null.- Parameters:
index
- The index at which to retrieve the binary data.- Returns:
- An array of bytes representing the binary data at the specified index, or null if the data does not exist or is not a valid binary representation.
-
getBinary
Retrieves the binary data located at the specified index within the JSON-like array and returns it as an array of bytes. If the element at the given index is either a binary array or a string encoded in Base64, it is converted into an array of bytes and returned. If the element does not exist at the specified index or is not a valid binary representation, this method returns the result obtained from the provided default value supplier.- Parameters:
index
- The index at which to retrieve the binary data.orElse
- A supplier function that provides a default value (an array of bytes) to be returned when the data does not exist at the specified index or is not a valid binary representation.- Returns:
- An array of bytes representing the binary data at the specified index, or the default value provided by the supplier if the data does not exist or is not a valid binary representation.
-
getBool
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a boolean. If the value at the given index is a boolean, it returns the boolean value; otherwise, it returns null.- Parameters:
index
- The index at which to retrieve the value.- Returns:
- The boolean value located at the given index, or null if it doesn't exist or the value at the index is not a boolean.
-
getBool
Retrieves the boolean value located at the specified index within the JSON-like array and returns it. If the element at the given index is a valid boolean value, it is returned. If the element does not exist at the specified index or is not a valid boolean representation, this method returns the result obtained from the provided default value supplier.- Parameters:
index
- The index at which to retrieve the boolean value.orElse
- A supplier function that provides a default boolean value to be returned when the value does not exist at the specified index or is not a valid boolean representation.- Returns:
- The boolean value located at the given index, or the default value provided by the supplier if the value does not exist or is not a valid boolean representation.
-
getDouble
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a decimal number, converting it to a double. If the value at the given index is a decimal number (either a double or a BigDecimal), it returns the corresponding double value. If the value is not a decimal number or the index doesn't exist, it returns null.The conversion process is designed to handle decimal numbers, but it's important to note that if the value is a BigDecimal, the conversion to a double may result in a potential loss of precision. BigDecimal numbers can represent decimal values with high precision, whereas double has limited precision. Therefore, when converting a BigDecimal to a double, there may be a loss of information beyond the double's precision, potentially leading to rounding or truncation.
- Parameters:
index
- The index at which to retrieve the value.- Returns:
- The double value located at the given index, or null if it doesn't exist, or the value at the index is not a decimal number.
-
getDouble
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a decimal number, converting it to a double. If the value at the given index is a decimal number (either a double or a BigDecimal), it returns the corresponding double value. If the value is not a decimal number or the index doesn't exist, it returns the default value provided by theorElse
supplier.The conversion process is designed to handle decimal numbers, but it's important to note that if the value is a BigDecimal, the conversion to a double may result in a potential loss of precision. BigDecimal numbers can represent decimal values with high precision, whereas double has limited precision. Therefore, when converting a BigDecimal to a double, there may be a loss of information beyond the double's precision, potentially leading to rounding or truncation.
- Parameters:
index
- The index at which to retrieve the value.orElse
- A supplier providing the default double value to return if the value is not a decimal number or the index doesn't exist.- Returns:
- The double value located at the given index, or the default value provided by
orElse
if it doesn't exist or the value at the index is not a decimal number.
-
getBigDec
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a decimal number, returning it as a BigDecimal. If the value at the given index is a decimal number (either a BigDecimal or a double), it returns the corresponding BigDecimal value. If the value is not a decimal number or the index doesn't exist, it returns null.- Parameters:
index
- The index at which to retrieve the value.- Returns:
- The BigDecimal value located at the given index, or null if it doesn't exist or the value at the index is not a decimal number.
-
getBigDec
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a decimal number, returning it as a BigDecimal. If the value at the given index is a decimal number (either a BigDecimal or a double), it returns the corresponding BigDecimal value. If the value is not a decimal number or the index doesn't exist, it returns the default BigDecimal value provided by the specified supplier.- Parameters:
index
- The index at which to retrieve the value.orElse
- A supplier function that provides a default BigDecimal value if the value at the index is not a decimal number or the index doesn't exist.- Returns:
- The BigDecimal value located at the given index, or the default value provided by the supplier if it doesn't exist or the value at the index is not a decimal number.
-
getBigInt
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as an integral number, returning it as a BigInteger. If the value at the given index is an integral number (either a BigInteger, long, or int), it returns the corresponding BigInteger value. If the value is not an integral number or the index doesn't exist, it returns null.- Parameters:
index
- The index at which to retrieve the value.- Returns:
- The BigInteger value located at the given index, or null if it doesn't exist or the value at the index is not an integral number.
-
getBigInt
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as an integral number, returning it as a BigInteger. If the value at the given index is an integral number (either a BigInteger, long, or int), it returns the corresponding BigInteger value. If the value is not an integral number or the index doesn't exist, it returns the default value provided.- Parameters:
index
- The index at which to retrieve the value.orElse
- The default value to return if the value at the index is not an integral number or if the index doesn't exist.- Returns:
- The integral number located at the given index, or the default value if it doesn't exist or the value at the index is not an integral number.
-
getObj
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a JSON object (JsObj). If the value at the given index is a JSON object, it returns the corresponding JsObj. If the value is not a JSON object or the index doesn't exist, it returns null.- Parameters:
index
- The index at which to retrieve the value.- Returns:
- The JsObj located at the given index, or null if it doesn't exist or the value at the index is not a JSON object.
-
getObj
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a JSON object (JsObj). If the value at the given index is a JSON object, it returns the corresponding JsObj. If the value is not a JSON object or the index doesn't exist, it returns the default JsObj provided by the `orElse` supplier.- Parameters:
index
- The index at which to retrieve the value.orElse
- A supplier that provides the default JsObj if the index is out of bounds or the value at the index is not a JSON object.- Returns:
- The JsObj located at the given index or the default JsObj provided by `orElse`.
-
getArray
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a JSON array (JsArray). If the value at the given index is a JSON array, it returns the corresponding JsArray. If the value is not a JSON array or the index doesn't exist, it returns null.- Parameters:
index
- The index at which to retrieve the value.- Returns:
- The JsArray located at the given index or null if it doesn't exist or is not a JSON array.
-
getArray
Retrieves the value located at the specified index within the JSON-like array and attempts to interpret it as a JSON array (JsArray). If the value at the given index is a JSON array, it returns the corresponding JsArray. If the value is not a JSON array or the index doesn't exist, it returns the default value provided by the specified Supplier.- Parameters:
index
- The index at which to retrieve the value.orElse
- A Supplier that provides the default JsArray value if the index doesn't exist or the value is not a JSON array.- Returns:
- The JsArray located at the given index or the default value provided by orElse.
-
containsValue
Description copied from interface:Json
Checks if this JSON contains the given value in its first level.- Specified by:
containsValue
in interfaceJson<JsArray>
- Parameters:
el
- The value to check for.- Returns:
- True if this JSON contains the value, otherwise false.
-
get
Description copied from interface:Json
Gets the value located at the given path orJsNothing
if it doesn't exist. -
filterValues
Description copied from interface:Json
Filters all the pairs of elements of this json, removing those that don't satisfy the given predicate.- Specified by:
filterValues
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as input every JsPair of this json- Returns:
- this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
-
filterValues
Description copied from interface:Json
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterValues
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
-
filterKeys
Description copied from interface:Json
Filters all the keys of this json, removing those that don't satisfy the given predicate.- Specified by:
filterKeys
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as input every JsPair of this json- Returns:
- this instance if all the keys satisfy the predicate or a new filtered json of the same type T
-
filterKeys
Description copied from interface:Json
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterKeys
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
-
filterObjs
Description copied from interface:Json
Filters all the pair of jsons of this json, removing those that don't satisfy the given predicate.- Specified by:
filterObjs
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as input every JsPair of this json- Returns:
- this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
-
filterObjs
Description copied from interface:Json
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterObjs
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
-
isEmpty
public boolean isEmpty()Description copied from interface:Json
Determines whether this Json instance is empty, i.e., it contains no elements. -
mapValues
Description copied from interface:Json
Recursively maps all the values of this JSON, replacing each value with the result of applying the given mapping function. This operation traverses the entire JSON structure. -
mapValues
Description copied from interface:Json
Recursively maps all the values of this JSON, replacing each value with the result of applying the given mapping function. This operation traverses the entire JSON structure. -
mapKeys
Description copied from interface:Json
Maps all the keys of this JSON object, recursively traversing the entire JSON structure. -
mapKeys
Description copied from interface:Json
Maps all the keys of this JSON object, recursively traversing the entire JSON structure. -
mapObjs
Description copied from interface:Json
Maps all the JSON objects of this JSON, traversing the entire JSON if necessary. -
mapObjs
Description copied from interface:Json
Maps all the JSON objects of this JSON, traversing the entire JSON if necessary. -
set
Description copied from interface:Json
Inserts the given element at the specified path in this JSON, replacing any existing element. If necessary, it fills empty indexes in arrays withJsNull
. You have the option to useJson.set(JsPath, JsValue, JsValue)
to specify a custom padding element. -
set
-
set
Sets the value at the specified index within the JSON-like array to the provided JsValue. If the array size is less than the specified index, it will be padded with the given padElement to accommodate the new value.- Parameters:
index
- The index at which to set the value.value
- The JsValue to set at the specified index.padElement
- The JsValue to use for padding the array if necessary.- Returns:
- The modified JsArray with the updated value at the specified index.
-
set
Description copied from interface:Json
Inserts an element at the specified path in this JSON, replacing any existing element.- Specified by:
set
in interfaceJson<JsArray>
- Parameters:
path
- The path to insert the element.value
- The element to insert.padElement
- The element to use for padding in arrays when necessary.- Returns:
- A new JSON with the inserted element, or this instance if the head of the path is incompatible.
-
reduce
public <R> Optional<R> reduce(BinaryOperator<R> op, BiFunction<? super JsPath, ? super JsPrimitive, R> map, BiPredicate<? super JsPath, ? super JsPrimitive> predicate) Description copied from interface:Json
Reduces the values of this JSON object that satisfy the given predicate, allowing access to the element's path. This reduction traverses the entire JSON recursively if necessary.- Specified by:
reduce
in interfaceJson<JsArray>
- Type Parameters:
R
- the type of the reduction result- Parameters:
op
- the operator to apply to values of type Rmap
- the mapping function to convert JsPath and JsPrimitive to type Rpredicate
- the predicate that determines which values are included in the reduction- Returns:
- an
Optional
describing the result of the reduction, or empty if no values satisfy the predicate
-
reduce
public <R> Optional<R> reduce(BinaryOperator<R> op, Function<? super JsPrimitive, R> map, Predicate<? super JsPrimitive> predicate) Description copied from interface:Json
Reduces the values of this JSON object that satisfy the given predicate. This reduction traverses the entire JSON recursively if necessary.- Specified by:
reduce
in interfaceJson<JsArray>
- Type Parameters:
R
- the type of the reduction result- Parameters:
op
- the operator to apply to values of type Rmap
- the mapping function to convert JsValue to type Rpredicate
- the predicate that determines which values are included in the reduction- Returns:
- an
Optional
describing the result of the reduction, or empty if no values satisfy the predicate
-
delete
Description copied from interface:Json
Removes the element at the specified path within this immutable JSON object, if it exists. Returns a new JSON object with the element removed, or the original JSON object if the element does not exist. -
size
public int size()Description copied from interface:Json
Returns the number of elements in the first level of this JSON object or array. This method provides the count of elements directly contained in the JSON object or array. -
stream
Description copied from interface:Json
Returns a stream over all the key-value pairsJsPair
of elements in this JSON object. This method provides a way to traverse and operate on the key-value pairs within the JSON object. -
streamOfValues
Returns a non-recursive stream of values contained within the JSON-like array. This stream allows for sequential processing of the immediate values in the array without recursively traversing nested objects or arrays.- Returns:
- A stream of JsValue objects representing the immediate values in the array.
- See Also:
-
get
Returns the element located at the specified index orJsNothing
if it doesn't exist. This method is a total function and never throws exceptions. -
hashCode
public int hashCode()equals method is inherited, so it's implemented. The purpose of this method is to cache the hashcode once calculated. the object is immutable and it won't change Single-check idiom Item 83 from Effective Java -
equals
-
toString
// Single-check idiom Item 83 from effective java -
head
Returns the first element of this array. -
tail
Returns a JSON array consisting of all elements of this array except the first one. -
init
Returns all the elements of this array except the last one. -
intersection
this.union(that, SET)
returnsthis
plus those elements fromthat
that don't exist inthis
.this.union(that, MULTISET)
returnsthis
plus those elements fromthat
appended to the back.this.union(that, LIST)
returnsthis
plus those elements fromthat
which position is>= this.size()
. For those elements that are containers of the same type and are located at the same position, the result is their intersection. So this operation is kind of a recursive intersection- Specified by:
intersection
in interfaceJson<JsArray>
- Parameters:
that
- the other arrayARRAY_AS
- the array merging strategy, either 'MERGE' or 'REPLACE'- Returns:
- a JsArray of the same type as the inputs
-
isArray
public boolean isArray()Description copied from interface:JsValue
Returns true if this JsValue is a JsArray. -
iterator
-
last
Returns the last element of this array. -
prepend
Adds one or more elements, starting from the last, to the front of this array. -
prependAll
Adds all the elements of theJsArray
, starting from the last, to the front of this array. -
delete
Deletes the element at the specified index from thisJsArray
.- Parameters:
index
- The index of the element to be deleted.- Returns:
- A new
JsArray
with the element at the specified index removed. - Throws:
IllegalArgumentException
- if the index is negative.
-
union
Description copied from interface:Json
Computes the union of this JSON and another JSON object 'that' with respect to the given array merging strategy. The union of two JSON objects is another JSON object that contains all the key-value pairs present in either 'this' or 'that'. If a key is present in both JSON objects, the value from 'this' will be used. Array merging strategy 'ARRAY_AS' determines how arrays are merged during the union operation. If 'ARRAY_AS' is 'MERGE', arrays are merged by concatenating elements. If 'ARRAY_AS' is 'REPLACE', arrays in 'this' will be replaced with arrays from 'that'.
-