- Type Parameters:
T
- Type of the container: either an object (JsObj) or an array (JsArray)
- All Superinterfaces:
JsValue
JsPair
) with keys represented by JsPath
and values as JsValue
. The structure allows for recursion.
For instance, the JSON: { "a": 1, "x": { "c": true, "d": null, "e": [false, 1, "hi"] } }
Can be seen as a set: Set[(a, 1), (x.c, true), (x.d, null), (x.e.0, false), (x.e.1, 1), (x.e.2, "hi"), (_, JsNothing)]
The special element JsNothing.NOTHING
represents any other JsPath, making functions like
get(JsPath)
total (defined for every possible path).
JSON modification operations, such as set(JsPath, JsValue)
, can insert JsNothing without changing the JSON.
JSONs can also be treated as streams of pairs, enabling operations like map, filter, and reduce.
All methods throw a NullPointerException when passed null parameters, and UserError is thrown for inappropriate method calls.
/**
The ARRAY_AS
parameter in various methods of the Json
interface allows you to specify how arrays within the JSON data structure should be treated during specific operations. It provides customization options for handling arrays, giving you control over their behavior in various contexts.
JSON data structures can contain arrays, and the behavior of operations involving these arrays can vary depending on your requirements. The ARRAY_AS
parameter serves as a way to define this behavior by offering different options or types, each of which affects how arrays are treated.
Common use cases for the ARRAY_AS
parameter include:
- Defining how arrays should be merged, concatenated, or otherwise combined when performing operations that involve JSON structures.
- Specifying whether arrays should be considered equal only if they have the same elements in the same order or if the order of elements is not important.
- Controlling how arrays are affected by operations such as union and intersection between JSON structures.
The specific values or options available for the ARRAY_AS
parameter will depend on the JSON library or framework being used. Developers should refer to the documentation of the library or framework to understand the available options and their behavior.
By allowing customization of array handling, the ARRAY_AS
parameter enhances the flexibility of working with JSON data, enabling developers to tailor operations to suit specific structural requirements and use cases.
- Author:
- Rafael Merino Garcia
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
containsPath
(JsPath path) Checks if an element exists in this JSON at the given path.boolean
containsValue
(JsValue element) Checks if this JSON contains the given value in its first level.Removes the element at the specified path within this immutable JSON object, if it exists.default boolean
equals
(JsValue elem, JsArray.TYPE ARRAY_AS) Checks if this JSON is equal to another JSON element.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.Gets the value located at the given path orJsNothing
if it doesn't exist.default JsArray
Returns the array located at the given path or null if it doesn't exist or it's not an array.default JsArray
Returns the array located at the given path or the default value provided if it doesn't exist or it's not an array.default BigDecimal
Returns the number located at the given path as a big decimal or null if it doesn't exist or it's not a decimal number.default BigDecimal
getBigDec
(JsPath path, Supplier<BigDecimal> orElse) Returns the number located at the given path as a big decimal or the default value provided if it doesn't exist or it's not a decimal number.default BigInteger
Returns the number located at the given path as a big integer or null if it doesn't exist or it's not an integral number.default BigInteger
getBigInt
(JsPath path, Supplier<BigInteger> orElse) Returns the number located at the given path as a big integer or the default value provided if it doesn't exist or it's not an integral number.default byte[]
Returns the bytes located at the given path or null if it doesn't exist.default byte[]
Returns the bytes located at the given path or the default value provided if it doesn't exist.default Boolean
Returns the boolean located at the given path or null if it doesn't exist.default Boolean
Returns the boolean located at the given path or the default value provided if it doesn't exist.default Double
Returns the decimal number located at the given path as a double or null if it doesn't exist or it's not a decimal number.default Double
Returns the decimal number located at the given path as a double or the default value provided if it doesn't exist or it's not a decimal number.default Instant
getInstant
(JsPath path) Returns the instant located at the given path or null if it doesn't exist.default Instant
getInstant
(JsPath path, Supplier<Instant> orElse) Returns the instant located at the given path or the default value provided if it doesn't exist.default Integer
Returns the integral number located at the given path as an integer or null if it doesn't exist or it's not an integral number.default Integer
Returns the integral number located at the given path as an integer or the default value provided if it doesn't exist or it's not an integral number.default Long
Returns the integral number located at the given path as a long or null if it doesn't exist or it's not an integral number.default Long
Returns the integral number located at the given path as a long or the default value provided if it doesn't exist or it's not an integral number.default JsObj
Returns the object located at the given path or null if it doesn't exist or it's not an object.default JsObj
Returns the object located at the given path or the default value provided if it doesn't exist or it's not an object.default String
Returns the string located at the given path or null if it doesn't exist or it's not a string.default String
Returns the string located at the given path or the default value provided if it doesn't exist or it's not a string.default <A> A
ifEmptyElse
(Supplier<A> emptySupplier, Supplier<A> nonemptySupplier) Declarative way of implementing if(this.isEmpty()) return emptySupplier.get() else return nonEmptySupplier.get().intersection
(T that, JsArray.TYPE ARRAY_AS) Computes the intersection of this JSON and another JSON object 'that' with respect to the given array merging strategy.boolean
isEmpty()
Determines whether this Json instance is empty, i.e., it contains no elements.default boolean
Determines whether this Json instance is not empty, i.e., it contains one or more elements.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.<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.default byte[]
Serializes this JSON object into a byte array, effectively converting it into its serialized form as a byte sequence.default void
serialize
(OutputStream outputstream) Serializes this JSON object into the given output stream, effectively converting it into its serialized form.default T
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.default JsPrimitive
Returns this JsValue as a JsPrimitive.default String
Converts the JSON to a pretty-printed string with a default indentation length of 2 spaces.default String
toPrettyString
(int indentLength) Converts the JSON to a pretty-printed string with the specified indentation length.union
(T 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 interface jsonvalues.JsValue
ifNothing, ifNull, isArray, 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
-
Method Details
-
toPrettyString
Converts the JSON to a pretty-printed string with the specified indentation length.- Parameters:
indentLength
- The number of spaces to use for indentation.- Returns:
- A pretty-printed JSON string.
-
toPrettyString
Converts the JSON to a pretty-printed string with a default indentation length of 2 spaces.- Returns:
- A pretty-printed JSON string.
- See Also:
-
containsValue
Checks if this JSON contains the given value in its first level.- Parameters:
element
- The value to check for.- Returns:
- True if this JSON contains the value, otherwise false.
-
containsPath
Checks if an element exists in this JSON at the given path.- Parameters:
path
- The path to check.- Returns:
- True if a value exists at the path, otherwise false.
-
get
Gets the value located at the given path orJsNothing
if it doesn't exist.- Parameters:
path
- The path to the desired value.- Returns:
- The value at the given path or JsNothing if it doesn't exist.
-
equals
Checks if this JSON is equal to another JSON element.- Parameters:
elem
- The JSON element to compare.ARRAY_AS
- The type to consider arrays during comparison.- Returns:
- True if the JSON elements are equal, otherwise false.
-
filterValues
Filters all the pairs of elements of this json, removing those that don't satisfy the given predicate.- 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
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.- 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
Filters all the keys of this json, removing those that don't satisfy the given predicate.- 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
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.- 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
Filters all the pair of jsons of this json, removing those that don't satisfy the given predicate.- 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
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.- 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
-
getArray
Returns the array located at the given path or null if it doesn't exist or it's not an array.- Parameters:
path
- the path- Returns:
- the JsArray located at the given JsPath or null
-
getArray
Returns the array located at the given path or the default value provided if it doesn't exist or it's not an array.- Parameters:
path
- the pathorElse
- the default value provided- Returns:
- the JsArray located at the given JsPath or null
-
getBigDec
Returns the number located at the given path as a big decimal or null if it doesn't exist or it's not a decimal number.- Parameters:
path
- the path- Returns:
- the number located at the given JsPath or null
-
getBigDec
Returns the number located at the given path as a big decimal or the default value provided if it doesn't exist or it's not a decimal number.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the number located at the given JsPath or null
-
toJsPrimitive
Description copied from interface:JsValue
Returns this JsValue as a JsPrimitive.- Specified by:
toJsPrimitive
in interfaceJsValue
- Returns:
- This JsValue as a JsPrimitive.
-
getBigInt
Returns the number located at the given path as a big integer or null if it doesn't exist or it's not an integral number.- Parameters:
path
- the path- Returns:
- the BigInteger located at the given JsPath or null
-
getBigInt
Returns the number located at the given path as a big integer or the default value provided if it doesn't exist or it's not an integral number.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the BigInteger located at the given JsPath or null
-
getBool
Returns the boolean located at the given path or null if it doesn't exist.- Parameters:
path
- the path- Returns:
- the Boolean located at the given JsPath or null
-
getBool
Returns the boolean located at the given path or the default value provided if it doesn't exist.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the Boolean located at the given JsPath or null
-
getBinary
Returns the bytes located at the given path or null if it doesn't exist.- Parameters:
path
- the path- Returns:
- the bytes located at the given JsPath or null
-
getBinary
Returns the bytes located at the given path or the default value provided if it doesn't exist.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the bytes located at the given JsPath or null
-
getInstant
Returns the instant located at the given path or null if it doesn't exist.- Parameters:
path
- the path- Returns:
- the instant located at the given JsPath or null
-
getInstant
Returns the instant located at the given path or the default value provided if it doesn't exist.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the instant located at the given JsPath or null
-
getDouble
Returns the decimal number located at the given path as a double or null if it doesn't exist or it's not a decimal number. If the number is a BigDecimal, the conversion is identical to that specified inBigDecimal.doubleValue()
.- Parameters:
path
- the path- Returns:
- the decimal number located at the given JsPath or null
-
getDouble
Returns the decimal number located at the given path as a double or the default value provided if it doesn't exist or it's not a decimal number. If the number is a BigDecimal, the conversion is identical to that specified inBigDecimal.doubleValue()
.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the decimal number located at the given JsPath or null
-
getInt
Returns the integral number located at the given path as an integer or null if it doesn't exist or it's not an integral number.- Parameters:
path
- the path- Returns:
- the integral number located at the given JsPath or null
-
getInt
Returns the integral number located at the given path as an integer or the default value provided if it doesn't exist or it's not an integral number.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the integral number located at the given JsPath or null
-
getLong
Returns the integral number located at the given path as a long or null if it doesn't exist or it's not an integral number.- Parameters:
path
- the path- Returns:
- the integral number located at the given JsPath or null
-
getLong
Returns the integral number located at the given path as a long or the default value provided if it doesn't exist or it's not an integral number.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the integral number located at the given JsPath or null
-
getObj
Returns the object located at the given path or the default value provided if it doesn't exist or it's not an object.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the JsObj located at the given JsPath or null
-
getObj
Returns the object located at the given path or null if it doesn't exist or it's not an object.- Parameters:
path
- the path- Returns:
- the JsObj located at the given JsPath or null
-
getStr
Returns the string located at the given path or null if it doesn't exist or it's not a string.- Parameters:
path
- the path- Returns:
- the string located at the given path or null
-
getStr
Returns the string located at the given path or the default value provided if it doesn't exist or it's not a string.- Parameters:
path
- the pathorElse
- the default value- Returns:
- the string located at the given path or null
-
ifEmptyElse
Declarative way of implementing if(this.isEmpty()) return emptySupplier.get() else return nonEmptySupplier.get().- Type Parameters:
A
- the type of the result- Parameters:
emptySupplier
- Supplier that will produce the result if this json is emptynonemptySupplier
- Supplier that will produce the result if this json is not empty- Returns:
- an object of type A
-
isEmpty
boolean isEmpty()Determines whether this Json instance is empty, i.e., it contains no elements.- Returns:
- true if empty, false otherwise
-
isNotEmpty
default boolean isNotEmpty()Determines whether this Json instance is not empty, i.e., it contains one or more elements.- Returns:
- false if empty, true otherwise
-
mapValues
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.- Parameters:
fn
- the mapping function that transforms each value- Returns:
- a new JSON object of the same type T with the mapped values
-
mapValues
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.- Parameters:
fn
- the mapping function that transforms each value- Returns:
- a new JSON object of the same type T with the mapped values
-
mapKeys
Maps all the keys of this JSON object, recursively traversing the entire JSON structure.- Parameters:
fn
- the mapping function that transforms each key- Returns:
- a new JSON object of the same type T with the mapped keys
-
mapKeys
Maps all the keys of this JSON object, recursively traversing the entire JSON structure.- Parameters:
fn
- the mapping function that transforms each key- Returns:
- a new JSON object of the same type T with the mapped keys
-
mapObjs
Maps all the JSON objects of this JSON, traversing the entire JSON if necessary.- Parameters:
fn
- the mapping function that transforms each JSON object- Returns:
- a new JSON object of the same type T with the mapped objects, or the same instance if no objects are found
-
mapObjs
Maps all the JSON objects of this JSON, traversing the entire JSON if necessary.- Parameters:
fn
- the mapping function that transforms each JSON object- Returns:
- a new JSON object of the same type T with the mapped objects, or the same instance if no objects are found
-
set
Inserts an element at the specified path in this JSON, replacing any existing element.- Parameters:
path
- The path to insert the element.element
- 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.
-
set
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 useset(JsPath, JsValue, JsValue)
to specify a custom padding element.- Parameters:
path
- the path where the element will be insertedelement
- the element to be inserted- Returns:
- a JSON object of the same type with the new element, or the same instance if the path is invalid
-
reduce
<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. This reduction traverses the entire JSON recursively if necessary.- 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
<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. This reduction traverses the entire JSON recursively if necessary.- 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
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.- Parameters:
path
- the path specifying the element to be removed- Returns:
- a new JSON object with the specified element removed, or the original JSON object if the element does not exist
-
size
int size()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.- 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. This method provides a way to traverse and operate on the key-value pairs within the JSON object.- Returns:
- a
Stream
over all the key-value pairs (JsPairs) in this JSON object
-
serialize
Serializes this JSON object into the given output stream, effectively converting it into its serialized form. Serialization is the process of converting a JSON object into a byte stream representation. The serialized JSON can be written to an output stream, such as a file or network socket.- Parameters:
outputstream
- the output stream to which the JSON object will be serialized
-
serialize
default byte[] serialize()Serializes this JSON object into a byte array, effectively converting it into its serialized form as a byte sequence. Serialization is the process of converting a JSON object into a byte stream representation. This method returns the serialized JSON as a byte array.- Returns:
- a byte array containing the serialized representation of this JSON object
-
union
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'.- Parameters:
that
- the other JSON object to compute the union withARRAY_AS
- the array merging strategy, either 'MERGE' or 'REPLACE'- Returns:
- a new JSON object representing the union of 'this' and 'that'
-
intersection
Computes the intersection of this JSON and another JSON object 'that' with respect to the given array merging strategy. The intersection of two JSON objects is another JSON object that contains only the key-value pairs present in both 'this' and 'that'. Array merging strategy 'ARRAY_AS' determines how arrays are merged during the intersection 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'.- Parameters:
that
- the other JSON object to compute the intersection withARRAY_AS
- the array merging strategy, either 'MERGE' or 'REPLACE'- Returns:
- a new JSON object representing the intersection of 'this' and 'that'
-