T
- Type of container: either an object or an arraypublic interface Json<T extends Json<T>> extends JsValue
Represents a json of type T, where T is the type of the container, either a JsObj or a JsArray. A json of any type can be modeled as a set of pairsPair
=(JsPath
,JsValue
), where: - a JsValue is aJsBool
orJsStr
orJsNumber
orJsNull
, or anotherJson
likeJsObj
orJsArray
, what makes the data structure recursive. - a JsPath represents the location of the element in the json. For example, the json { "a":1, "x":{ "c": true, "d":null, e: [false, 1, "hi"] } } can be seen as the following set: Set[(a,1), (x.c,true), (x.d,null), (x.e.0,false), (x.e.1,1), (x.e.2,"hi"), (_,NOTHING)] where _, which means any other JsPath, and the special elementJsNothing.NOTHING
, makes the functionget(JsPath)
total (defined for every possible path). Moreover, inserting JsNothing in a json doesn't change the json, which is very convenient when passing functions as parameters to put data in: //all the logic goes into the supplierSupplier<JsValue> supplier = ()-> (doesnt-put-anything-condition) ? JsNothing.NOTHING : JsInt.of(2); json.putIfAbsent(path,supplier)
Another way to see a json is like a stream of pairs, which opens the door to doing all the operations that were introduced in Java 8 (map, filter, reduce, etc). For this purpose the methodsstream()
orstream()
are provided. There are one convention on method names: -Methods that are suffixed with underscore traverse the whole json recursively. All the methods throw a NullPointerException when any of the params passed in is null. The exceptionUserError
is thrown when the user calls a method inappropriately.
to work with jsons that are objects
,
to work with jsons that are arrays
Modifier and Type | Method and Description |
---|---|
default boolean |
containsPath(JsPath path)
Returns true if an element exists in this json at the given path.
|
boolean |
containsValue(JsValue element)
Returns true if this json contains the given element in the first level.
|
T |
delete(JsPath path)
Removes the element in this json located at the given path, if it exists, returning the same this
instance otherwise
|
default boolean |
equals(JsValue elem,
JsArray.TYPE ARRAY_AS) |
T |
filterKeys(BiPredicate<? super JsPath,? super JsValue> filter)
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.
|
T |
filterKeys(Predicate<? super String> filter)
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.
|
T |
filterObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.
|
T |
filterObjs(Predicate<? super JsObj> filter)
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.
|
T |
filterValues(BiPredicate<? super JsPath,? super JsPrimitive> filter)
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.
|
T |
filterValues(Predicate<? super JsPrimitive> filter)
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.
|
JsValue |
get(JsPath path)
Returns the element located at the given path or
JsNothing if it doesn't exist. |
default JsArray |
getArray(JsPath path)
Returns the array located at the given path or null if it doesn't exist or it's not an array.
|
default JsArray |
getArray(JsPath path,
Supplier<JsArray> orElse)
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 |
getBigDec(JsPath path)
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 if
it doesn't exist or it's not a decimal number.
|
default BigInteger |
getBigInt(JsPath path)
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 if it doesn't
exist or it's not an integral number.
|
default byte[] |
getBinary(JsPath path)
Returns the bytes located at the given path or null if it doesn't exist.
|
default byte[] |
getBinary(JsPath path,
Supplier<byte[]> orElse)
Returns the bytes located at the given path or the default value provided if it doesn't exist.
|
default Boolean |
getBool(JsPath path)
Returns the boolean located at the given path or null if it doesn't exist.
|
default Boolean |
getBool(JsPath path,
Supplier<Boolean> orElse)
Returns the boolean located at the given path or the default value provided if it doesn't exist.
|
default Double |
getDouble(JsPath path)
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 |
getDouble(JsPath path,
Supplier<Double> orElse)
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 |
getInt(JsPath path)
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 or it's an integral number but doesn't fit in an integer.
|
default Integer |
getInt(JsPath path,
Supplier<Integer> orElse)
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 or it's an integral number but doesn't fit in an integer.
|
default Long |
getLong(JsPath path)
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 or it's an integral number but doesn't fit in a long.
|
default Long |
getLong(JsPath path,
Supplier<Long> orElse)
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 or it's an integral number but doesn't fit
in a long.
|
default JsObj |
getObj(JsPath path)
Returns the object located at the given path or null if it doesn't exist or it's
not an object.
|
default JsObj |
getObj(JsPath path,
Supplier<JsObj> orElse)
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 |
getStr(JsPath path)
Returns the string located at the given path or null if it doesn't exist or it's
not an string.
|
default String |
getStr(JsPath path,
Supplier<String> orElse)
Returns the string located at the given path or the default value provided if
it doesn't exist or it's not an 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()
|
T |
intersection(T that,
JsArray.TYPE ARRAY_AS) |
boolean |
isEmpty()
return true if there's no element in this json
|
default boolean |
isNotEmpty()
return true if this json it not empty
|
T |
mapKeys(BiFunction<? super JsPath,? super JsValue,String> fn)
Maps all the keys of this json.
|
T |
mapKeys(Function<? super String,String> fn)
Maps all the keys of this json.
|
T |
mapObjs(BiFunction<? super JsPath,? super JsObj,? extends JsValue> fn)
Maps all the jsons of this json.
|
T |
mapObjs(Function<? super JsObj,? extends JsValue> fn)
Maps all the jsons of this json.
|
T |
mapValues(BiFunction<? super JsPath,? super JsPrimitive,? extends JsValue> fn)
Maps all the values of this json.
|
T |
mapValues(Function<? super JsPrimitive,? extends JsValue> fn)
Maps all the values of this json.
|
<R> Optional<R> |
reduce(BinaryOperator<R> op,
BiFunction<? super JsPath,? super JsPrimitive,R> map,
BiPredicate<? super JsPath,? super JsPrimitive> predicate)
Performs a reduction on the values of this json that satisfy the predicate.
|
<R> Optional<R> |
reduce(BinaryOperator<R> op,
Function<? super JsPrimitive,R> map,
Predicate<? super JsPrimitive> predicate)
Performs a reduction on the values of this json that satisfy the predicate.
|
default byte[] |
serialize()
Serialize this Json into an array of bytes.
|
default void |
serialize(OutputStream outputstream)
Serializes this Json into the given output stream, no returning anything
|
default T |
set(JsPath path,
JsValue element)
Inserts the element at the path in this json, replacing any existing element and filling with
JsNull empty indexes in arrays when necessary. |
T |
set(JsPath path,
JsValue element,
JsValue padElement)
Inserts the element at the path in this json, replacing any existing element and filling with padElement
empty indexes in arrays when necessary.
|
int |
size()
Returns the number of elements in the first level of this json
|
Stream<fun.tuple.Pair<JsPath,JsValue>> |
stream()
Returns a stream over all the pairs of elements in this json object.
|
default JsPrimitive |
toJsPrimitive() |
default String |
toPrettyString()
Converts the string representation of this Json to a pretty print version.
|
default String |
toPrettyString(int indentLength)
Converts the string representation of this Json to a pretty print version
It's indented using whitespaces
|
T |
union(T that,
JsArray.TYPE ARRAY_AS) |
id, 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
default String toPrettyString(int indentLength)
indentLength
- indented with this number of spacesdefault String toPrettyString()
boolean containsValue(JsValue element)
element
- the give element JsValue whose presence in this JsArray is to be testeddefault boolean containsPath(JsPath path)
path
- the JsPathJsValue get(JsPath path)
JsNothing
if it doesn't exist.path
- the JsPath object of the element that will be returneddefault boolean equals(JsValue elem, JsArray.TYPE ARRAY_AS)
T filterValues(BiPredicate<? super JsPath,? super JsPrimitive> filter)
filter
- the predicate which takes as the input every JsPair of this jsonT filterValues(Predicate<? super JsPrimitive> filter)
filter
- the predicate which takes as the input every JsPair of this jsonT filterKeys(BiPredicate<? super JsPath,? super JsValue> filter)
filter
- the predicate which takes as the input every JsPair of this jsonT filterKeys(Predicate<? super String> filter)
filter
- the predicate which takes as the input every JsPair of this jsonT filterObjs(BiPredicate<? super JsPath,? super JsObj> filter)
filter
- the predicate which takes as the input every JsPair of this jsonT filterObjs(Predicate<? super JsObj> filter)
filter
- the predicate which takes as the input every JsPair of this jsondefault JsArray getArray(JsPath path)
path
- the pathdefault JsArray getArray(JsPath path, Supplier<JsArray> orElse)
path
- the pathorElse
- the default value provideddefault BigDecimal getBigDec(JsPath path)
path
- the pathdefault BigDecimal getBigDec(JsPath path, Supplier<BigDecimal> orElse)
path
- the pathorElse
- the default valuedefault JsPrimitive toJsPrimitive()
toJsPrimitive
in interface JsValue
default BigInteger getBigInt(JsPath path)
path
- the pathdefault BigInteger getBigInt(JsPath path, Supplier<BigInteger> orElse)
path
- the pathorElse
- the default valuedefault Boolean getBool(JsPath path)
path
- the pathdefault Boolean getBool(JsPath path, Supplier<Boolean> orElse)
path
- the pathorElse
- the default valuedefault byte[] getBinary(JsPath path)
path
- the pathdefault byte[] getBinary(JsPath path, Supplier<byte[]> orElse)
path
- the pathorElse
- the default valuedefault Instant getInstant(JsPath path)
path
- the pathdefault Instant getInstant(JsPath path, Supplier<Instant> orElse)
path
- the pathorElse
- the default valuedefault Double getDouble(JsPath path)
BigDecimal.doubleValue()
and in some cases it can lose information about
the precision of the BigDecimalpath
- the pathdefault Double getDouble(JsPath path, Supplier<Double> orElse)
BigDecimal.doubleValue()
and in some cases it can lose information about
the precision of the BigDecimalpath
- the pathorElse
- the default valuedefault Integer getInt(JsPath path)
path
- the pathdefault Integer getInt(JsPath path, Supplier<Integer> orElse)
path
- the pathorElse
- the default valuedefault Long getLong(JsPath path)
path
- the pathdefault Long getLong(JsPath path, Supplier<Long> orElse)
path
- the pathorElse
- the default valuedefault JsObj getObj(JsPath path, Supplier<JsObj> orElse)
path
- the pathorElse
- the default valuedefault JsObj getObj(JsPath path)
path
- the pathdefault String getStr(JsPath path)
path
- the pathdefault String getStr(JsPath path, Supplier<String> orElse)
path
- the pathorElse
- the default valuedefault <A> A ifEmptyElse(Supplier<A> emptySupplier, Supplier<A> nonemptySupplier)
A
- the type of the resultemptySupplier
- Supplier that will produce the result if this json is emptynonemptySupplier
- Supplier that will produce the result if this json is not emptyboolean isEmpty()
default boolean isNotEmpty()
T mapValues(BiFunction<? super JsPath,? super JsPrimitive,? extends JsValue> fn)
fn
- the mapping functionT mapValues(Function<? super JsPrimitive,? extends JsValue> fn)
fn
- the mapping functionT mapKeys(BiFunction<? super JsPath,? super JsValue,String> fn)
fn
- the mapping functionT mapKeys(Function<? super String,String> fn)
fn
- the mapping functionT mapObjs(BiFunction<? super JsPath,? super JsObj,? extends JsValue> fn)
fn
- the mapping functionT mapObjs(Function<? super JsObj,? extends JsValue> fn)
fn
- the mapping functionT set(JsPath path, JsValue element, JsValue padElement)
The same instance is returned when the head of the path is a key and this is an array or the head
of the path is an index and this is an object or the element is JsNothing
path
- the JsPath object where the element will be inserted atelement
- the JsValue that will be insertedpadElement
- the JsValue that will be inserted in arrays when padding is necessarydefault T set(JsPath path, JsValue element)
JsNull
empty indexes in arrays when necessary.
The same instance is returned when the head of the path is a key and this is an array or the head
of the path is an index and this is an object or the element is JsNothing
path
- the JsPath object where the element will be inserted atelement
- the JsValue that will be inserted<R> Optional<R> reduce(BinaryOperator<R> op, BiFunction<? super JsPath,? super JsPrimitive,R> map, BiPredicate<? super JsPath,? super JsPrimitive> predicate)
R
- the type of the operands of the operatorop
- the operator upon two objects of type Rmap
- the mapping function which produces an object of type R from a JsValuepredicate
- the predicate that determines what JsValue will be mapped and reducedOptional
describing the result of the reduction<R> Optional<R> reduce(BinaryOperator<R> op, Function<? super JsPrimitive,R> map, Predicate<? super JsPrimitive> predicate)
R
- the type of the operands of the operatorop
- the operator upon two objects of type Rmap
- the mapping function which produces an object of type R from a JsValuepredicate
- the predicate that determines what JsValue will be mapped and reducedOptional
describing the result of the reductionT delete(JsPath path)
path
- the given JsPath objectint size()
Stream<fun.tuple.Pair<JsPath,JsValue>> stream()
Stream
over all the JsPairs in this jsondefault void serialize(OutputStream outputstream)
outputstream
- the output streamdefault byte[] serialize()
T union(T that, JsArray.TYPE ARRAY_AS)
T intersection(T that, JsArray.TYPE ARRAY_AS)
Copyright © 2022. All rights reserved.