Package jsonvalues
Class JsObj
- java.lang.Object
-
- jsonvalues.JsObj
-
public class JsObj extends Object implements Json<JsObj>, Iterable<io.vavr.Tuple2<String,JsValue>>
Represents a json object, which is an unordered set of name/element pairs. Two implementations are provided, an immutable which uses the persistent Scala HashMap, and a mutable which uses the conventional Java HashMap.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsObj
append(JsPath path, JsValue elem)
Appends one element to the array located at the given path in this json.JsObj
appendAll(JsPath path, JsArray elems)
Appends all the elements of the array, starting from the head, to the array located at the given path in this json.boolean
containsKey(String key)
return true if the key is presentboolean
containsValue(JsValue el)
Returns true if this json contains the given element in the first level.static JsObj
empty()
boolean
equals(@Nullable Object that)
boolean
equals(JsObj that, JsArray.TYPE ARRAY_AS)
return true if this obj is equal to the given as a parameter.JsObj
filterAllKeys(Predicate<? super JsPair> filter)
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.JsObj
filterAllObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.JsObj
filterAllValues(Predicate<? super JsPair> filter)
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.JsObj
filterKeys(Predicate<? super JsPair> filter)
Filters the keys in the first level of this json, removing those that don't ifPredicateElse the predicate.JsObj
filterObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Filters the pair of jsons in the first level of this json, removing those that don't ifPredicateElse the predicate.JsObj
filterValues(Predicate<? super JsPair> filter)
Filters the pairs of elements in the first level of this json, removing those that don't ifPredicateElse the predicate.JsValue
get(String key)
JsValue
get(JsPath path)
Returns the element located at the given path orJsNothing
if it doesn't exist.JsArray
getArray(String key)
Returns the array located at the given key or null if it doesn't exist or it's not an array.BigDecimal
getBigDec(String key)
Returns the number located at the given key as a big decimal or null if it doesn't exist or it's not a decimal number.BigInteger
getBigInt(String key)
Returns the big integer located at the given key as a big integer or null if it doesn't exist or it's not an integral number.Boolean
getBool(String key)
Returns the boolean located at the given key or null if it doesn't exist.Double
getDouble(String key)
Returns the number located at the given key as a double or null if it doesn't exist or it's not a decimal number.Integer
getInt(String key)
Returns the integral number located at the given key 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.Long
getLong(String key)
Returns the integral number located at the given key 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.JsObj
getObj(String key)
Returns the json object located at the given key or null if it doesn't exist or it's not an object.Optional<JsArray>
getOptArray(String key)
Returns the array located at the given key orOptional.empty()
if it doesn't exist or it's not an array.Optional<BigDecimal>
getOptBigDec(String key)
Returns the number located at the given key as a big decimal orOptional.empty()
if it doesn't exist or it's not a decimal number.Optional<BigInteger>
getOptBigInt(String key)
Returns the big integer located at the given key as a big integer orOptional.empty()
if it doesn't exist or it's not an integral number.Optional<Boolean>
getOptBool(String key)
Returns the boolean located at the given key orOptional.empty()
if it doesn't exist.OptionalDouble
getOptDouble(String key)
Returns the number located at the given key as a double orOptionalDouble.empty()
if it doesn't exist or it's not a decimal number.OptionalInt
getOptInt(String key)
Returns the integral number located at the given key as an integer orOptionalInt.empty()
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.OptionalLong
getOptLong(String key)
Returns the integral number located at the given key as a long orOptionalLong.empty()
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.Optional<JsObj>
getOptObj(String key)
Returns the json object located at the given key orOptional.empty()
if it doesn't exist or it's not an object.Optional<String>
getOptStr(String key)
Returns the string located at the given key orOptional.empty()
if it doesn't exist or it's not an string.String
getStr(String key)
Returns the string located at the given key or null if it doesn't exist or it's not an string.int
hashCode()
equals method is inherited, so it's implemented.io.vavr.Tuple2<String,JsValue>
head()
Returns a pair with an arbitrary key of this object and its associated element.int
id()
JsObj
intersection(JsObj that, JsArray.TYPE ARRAY_AS)
this.intersection(that, SET)
returns an array with the elements that exist in boththis
andthat
this.intersection(that, MULTISET)
returns an array with the elements that exist in boththis
andthat
, being duplicates allowed.JsObj
intersectionAll(JsObj that, JsArray.TYPE ARRAY_AS)
this.intersectionAll(that)
behaves asthis.intersection(that, LIST)
, but for those elements that are containers of the same type and are located at the same position, the result is their intersection.boolean
isEmpty()
return true if there's no element in this jsonboolean
isObj()
Iterator<io.vavr.Tuple2<String,JsValue>>
iterator()
Set<String>
keySet()
Returns a set containing each key fo this object.JsObj
mapAllKeys(Function<? super JsPair,String> fn)
Maps all the keys of this json.JsObj
mapAllKeys(Function<? super JsPair,String> fn, Predicate<? super JsPair> predicate)
Maps all the keys of this json that satisfies a given predicate.JsObj
mapAllObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn)
Maps all the jsons of this json.JsObj
mapAllObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn, BiPredicate<? super JsPath,? super JsObj> predicate)
Maps all the jsons of this json that satisfies a given predicate.JsObj
mapAllValues(Function<? super JsPair,? extends JsValue> fn)
Maps all the values of this json.JsObj
mapAllValues(Function<? super JsPair,? extends JsValue> fn, Predicate<? super JsPair> predicate)
Maps all the values of this json that satisfies a given predicate.JsObj
mapKeys(Function<? super JsPair,String> fn)
Maps the keys in the first level of this json.JsObj
mapKeys(Function<? super JsPair,String> fn, Predicate<? super JsPair> predicate)
Maps the keys in the first level of this json that satisfies a given predicate.JsObj
mapObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn)
Maps the jsons in the first level of this json.JsObj
mapObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn, BiPredicate<? super JsPath,? super JsObj> predicate)
Maps the jsons in the first level of this json that satisfies a given predicate.JsObj
mapValues(Function<? super JsPair,? extends JsValue> fn)
Maps the values in the first level of this json.JsObj
mapValues(Function<? super JsPair,? extends JsValue> fn, Predicate<? super JsPair> predicate)
Maps the values in the first level of this json that satisfies a given predicate.static JsObj
of(String key, JsValue el)
Returns a one-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2)
Returns a two-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3)
Returns a three-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4)
Returns a four-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5)
Returns a five-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6)
Returns a six-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7)
Returns a six-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8)
Returns a six-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8, String key9, JsValue el9)
Returns a six-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8, String key9, JsValue el9, String key10, JsValue el10)
Returns a six-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8, String key9, JsValue el9, String key10, JsValue el10, String key11, JsValue el11)
Returns a six-element immutable object.static JsObj
of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8, String key9, JsValue el9, String key10, JsValue el10, String key11, JsValue el11, String key12, JsValue el12)
Returns a six-element immutable object.static JsObj
of(JsPair pair, JsPair... others)
Returns an immutable object from one or more pairs.static JsObj
ofIterable(Iterable<Map.Entry<String,JsValue>> xs)
static JsObj
parse(String str)
Tries to parse the string into an immutable object.static JsObj
parse(String str, ParseBuilder builder)
Tries to parse the string into an immutable object, performing the specified transformations during the parsing.JsObj
prepend(JsPath path, JsValue elem)
prepends one element to the array located at the path in this json.JsObj
prependAll(JsPath path, JsArray elems)
prepends all the elements of the array, starting from the head, to the array located at the path in this json.JsObj
put(String key, boolean value)
Inserts the boolean at the key in this json, replacing any existing element.JsObj
put(String key, int value)
Inserts the integer number at the key in this json, replacing any existing element.JsObj
put(String key, long value)
Inserts the long number at the key in this json, replacing any existing element.JsObj
put(String key, String value)
Inserts the string at the key in this json, replacing any existing element.JsObj
put(String key, BigDecimal value)
Inserts the big decimal number at the key in this json, replacing any existing element.JsObj
put(String key, BigInteger value)
Inserts the big integer number at the key in this json, replacing any existing element.JsObj
put(String key, Function<? super JsValue,? extends JsValue> fn)
Inserts the element returned by the function at the given key in this json, replacing any existing elementJsObj
put(String key, JsArray value)
Inserts the array at the key in this json, replacing any existing element.JsObj
put(String key, JsObj value)
Inserts the object at the key in this json, replacing any existing element.JsObj
put(String key, JsValue value)
Inserts the element at the key in this json, replacing any existing element.JsObj
put(JsPath path, Function<? super JsValue,? extends JsValue> fn)
Inserts the element returned by the function at the given path in this json, replacing any existing element and filling withJsNull
empty indexes in arrays when necessary.JsObj
putIfAbsent(String key, boolean bool)
Inserts at the given key in this json, if no element is present, the specified boolean.JsObj
putIfAbsent(String key, double number)
Inserts at the given key in this json, if no element is present, the specified double number.JsObj
putIfAbsent(String key, int number)
Inserts at the given key in this json, if no element is present, the specified integer.JsObj
putIfAbsent(String key, long number)
Inserts at the given key in this json, if no element is present, the specified long number.JsObj
putIfAbsent(String key, String str)
Inserts at the given key in this json, if no element is present, the specified string.JsObj
putIfAbsent(String key, Supplier<? extends JsValue> supplier)
Inserts the element returned by the supplier at the given key in this json, if no element is present.JsObj
putIfAbsent(String key, JsArray array)
Inserts at the given key in this json, if no element is present, the specified array.JsObj
putIfAbsent(String key, JsObj obj)
Inserts at the given key in this json, if no element is present, the specified obj.JsObj
putIfPresent(String key, boolean bool)
Inserts at the given key in this json, if some element is present, the specified boolean.JsObj
putIfPresent(String key, double number)
Inserts at the given key in this json, if some element is present, the specified double number.JsObj
putIfPresent(String key, int number)
Inserts at the given key in this json, if some element is present, the specified integer number.JsObj
putIfPresent(String key, long number)
Inserts at the given key in this json, if some element is present, the specified long number.JsObj
putIfPresent(String key, String str)
Inserts at the given key in this json, if some element is present, the specified string.JsObj
putIfPresent(String key, BigDecimal number)
Inserts at the given key in this json, if some element is present, the specified big decimal number.JsObj
putIfPresent(String key, BigInteger number)
Inserts at the given key in this json, if some element is present, the specified big integer number.JsObj
putIfPresent(String key, Function<? super JsValue,? extends JsValue> fn)
Inserts at the given key in this json, if some element is present, the element returned by the function.JsObj
putIfPresent(String key, JsArray array)
Inserts at the given key in this json, if some element is present, the specified array.JsObj
putIfPresent(String key, JsObj obj)
Inserts at the given key in this json, if some element is present, the specified obj.JsObj
putNull(String key)
<R> Optional<R>
reduce(BinaryOperator<R> op, Function<? super JsPair,R> map, Predicate<? super JsPair> predicate)
Performs a reduction on the values that satisfy the predicate in the first level of this json.<R> Optional<R>
reduceAll(BinaryOperator<R> op, Function<? super JsPair,R> map, Predicate<? super JsPair> predicate)
Performs a reduction on the values of this json that satisfy the predicate.JsObj
remove(JsPath path)
Removes the element in this json located at the given path, if it exists, returning the same this instance otherwiseboolean
same(JsObj obj)
int
size()
Returns the number of elements in the first level of this jsonStream<JsPair>
stream()
Returns a stream over the pairs of elements in the first level of this json object.Stream<JsPair>
streamAll()
Returns a stream over all the pairs of elements in this json object.JsObj
tail()
Returns a new object with all the entries of this json object except the one with the given key.String
toString()
// Single-check idiom Item 83 from effective javaJsObj
union(JsObj that)
returnsthis
json object plus those pairs from the given json objectthat
which keys don't exist inthis
.JsObj
unionAll(JsObj that, JsArray.TYPE ARRAY_AS)
behaves like theunion(JsObj)
but, for those keys that exit in boththis
andthat
json objects, which associated elements are **containers of the same type**, the result is their union.-
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
append, append, append, append, append, append, appendAllIfPresent, appendIfPresent, appendIfPresent, appendIfPresent, appendIfPresent, appendIfPresent, appendIfPresent, containsPath, equals, getArray, getBigDec, getBigInt, getBool, getDouble, getInt, getLong, getObj, getOptArray, getOptBigDec, getOptBigInt, getOptBool, getOptDouble, getOptInt, getOptLong, getOptObj, getOptStr, getStr, ifEmptyElse, isNotEmpty, map, prepend, prepend, prepend, prepend, prepend, prepend, prependAllIfPresent, prependIfPresent, prependIfPresent, prependIfPresent, prependIfPresent, prependIfPresent, prependIfPresent, put, put, put, put, put, put, put, putIf, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, serialize, serialize, size, sizeAll, sizeAll, times, timesAll, toPrettyString
-
Methods inherited from interface jsonvalues.JsValue
isArray, isArray, isBigDec, isBigDec, isBigInt, isBigInt, isBool, isDecimal, isDouble, isDouble, isFalse, isInstant, isInstant, isInt, isInt, isIntegral, isJson, isJson, isLocalDate, isLocalDate, isLocalDateTime, isLocalDateTime, isLong, isLong, isNothing, isNotJson, isNotNothing, isNotNull, isNotNumber, isNull, isNumber, isObj, isSameType, isStr, isStr, isTrue, toJsArray, toJsBigDec, toJsBigInt, toJsBool, toJsDouble, toJsInt, toJsLong, toJsNumber, toJsObj, toJson, toJsStr
-
-
-
-
Field Detail
-
EMPTY
public static final JsObj EMPTY
-
-
Method Detail
-
append
public final JsObj append(JsPath path, JsValue elem)
Description copied from interface:Json
Appends one element to the array located at the given path in this json. If the array doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays withJsNull
when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
-
appendAll
public final JsObj appendAll(JsPath path, JsArray elems)
Description copied from interface:Json
Appends all the elements of the array, starting from the head, to the array located at the given path in this json. If the array doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays withJsNull
when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
-
containsKey
public boolean containsKey(String key)
return true if the key is present- Parameters:
key
- the key- Returns:
- true if the specified key exists
-
containsValue
public final boolean containsValue(JsValue el)
Description copied from interface:Json
Returns true if this json contains the given element in the first level.- Specified by:
containsValue
in interfaceJson<JsObj>
- Parameters:
el
- the give element JsElem whose presence in this JsArray is to be tested- Returns:
- true if this JsArray contains the JsElem
-
empty
public static JsObj empty()
-
equals
public boolean equals(JsObj that, JsArray.TYPE ARRAY_AS)
return true if this obj is equal to the given as a parameter. In the case of ARRAY_AS=LIST, this method is equivalent to JsObj.equals(Object).- Parameters:
that
- the given arrayARRAY_AS
- enum to specify if arrays are considered as lists or sets or multisets- Returns:
- true if both objs are equals
-
keySet
public final Set<String> keySet()
Returns a set containing each key fo this object.- Returns:
- a Set containing each key of this JsObj
-
filterAllKeys
public JsObj filterAllKeys(Predicate<? super JsPair> filter)
Description copied from interface:Json
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterAllKeys
in interfaceJson<JsObj>
- 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
- See Also:
how to filter the keys of only the first level
-
filterAllObjs
public final JsObj filterAllObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Description copied from interface:Json
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterAllObjs
in interfaceJson<JsObj>
- 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
- See Also:
how to filter the pair of jsons of only the first level
-
filterAllValues
public final JsObj filterAllValues(Predicate<? super JsPair> filter)
Description copied from interface:Json
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterAllValues
in interfaceJson<JsObj>
- 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
- See Also:
how to filter the pairs of values of only the first level
-
filterKeys
public final JsObj filterKeys(Predicate<? super JsPair> filter)
Description copied from interface:Json
Filters the keys in the first level of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterKeys
in interfaceJson<JsObj>
- Parameters:
filter
- the predicate which takes as the input every JsPair in the first level of this json- Returns:
- same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
- See Also:
how to filter the keys of the whole json and not only the first level
-
filterObjs
public final JsObj filterObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Description copied from interface:Json
Filters the pair of jsons in the first level of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterObjs
in interfaceJson<JsObj>
- Parameters:
filter
- the predicate which takes as the input every JsPair in the first level of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
- See Also:
how to filter the pair of jsons of the whole json and not only the first level
-
filterValues
public final JsObj filterValues(Predicate<? super JsPair> filter)
Description copied from interface:Json
Filters the pairs of elements in the first level of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterValues
in interfaceJson<JsObj>
- Parameters:
filter
- the predicate which takes as the input every JsPair in the first level of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
- See Also:
how to filter the pair of elements of the whole json and not only the first level
-
get
public JsValue get(JsPath path)
Description copied from interface:Json
Returns the element located at the given path orJsNothing
if it doesn't exist.
-
getOptArray
public Optional<JsArray> getOptArray(String key)
Returns the array located at the given key orOptional.empty()
if it doesn't exist or it's not an array.- Parameters:
key
- the key- Returns:
- the JsArray located at the given key wrapped in an Optional
-
getArray
public JsArray getArray(String key)
Returns the array located at the given key or null if it doesn't exist or it's not an array.- Parameters:
key
- the key- Returns:
- the JsArray located at the given key or null
-
getOptBigDec
public Optional<BigDecimal> getOptBigDec(String key)
Returns the number located at the given key as a big decimal orOptional.empty()
if it doesn't exist or it's not a decimal number.- Parameters:
key
- the key- Returns:
- the BigDecimal located at the given key wrapped in an Optional
-
getBigDec
public BigDecimal getBigDec(String key)
Returns the number located at the given key as a big decimal or null if it doesn't exist or it's not a decimal number.- Parameters:
key
- the key- Returns:
- the BigDecimal located at the given key or null
-
getOptBigInt
public Optional<BigInteger> getOptBigInt(String key)
Returns the big integer located at the given key as a big integer orOptional.empty()
if it doesn't exist or it's not an integral number.- Parameters:
key
- the key- Returns:
- the BigInteger located at the given key wrapped in an Optional
-
getBigInt
public BigInteger getBigInt(String key)
Returns the big integer located at the given key as a big integer or null if it doesn't exist or it's not an integral number.- Parameters:
key
- the key- Returns:
- the BigInteger located at the given key or null
-
getOptBool
public Optional<Boolean> getOptBool(String key)
Returns the boolean located at the given key orOptional.empty()
if it doesn't exist.- Parameters:
key
- the key- Returns:
- the Boolean located at the given key wrapped in an Optional
-
getBool
public Boolean getBool(String key)
Returns the boolean located at the given key or null if it doesn't exist.- Parameters:
key
- the key- Returns:
- the Boolean located at the given key or null
-
getOptDouble
public OptionalDouble getOptDouble(String key)
Returns the number located at the given key as a double orOptionalDouble.empty()
if it doesn't exist or it's not a decimal number. If the number is a BigDecimal, the conversion is identical to the specified inBigDecimal.doubleValue()
and in some cases it can lose information about the precision of the BigDecimal- Parameters:
key
- the key- Returns:
- the decimal number located at the given key wrapped in an OptionalDouble
-
getDouble
public Double getDouble(String key)
Returns the number located at the given key 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 the specified inBigDecimal.doubleValue()
and in some cases it can lose information about the precision of the BigDecimal- Parameters:
key
- the key- Returns:
- the decimal number located at the given key or null
-
getOptInt
public OptionalInt getOptInt(String key)
Returns the integral number located at the given key as an integer orOptionalInt.empty()
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.- Parameters:
key
- the key- Returns:
- the integral number located at the given key wrapped in an OptionalInt
-
getInt
public Integer getInt(String key)
Returns the integral number located at the given key 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.- Parameters:
key
- the key- Returns:
- the integral number located at the given key or null
-
getOptLong
public OptionalLong getOptLong(String key)
Returns the integral number located at the given key as a long orOptionalLong.empty()
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.- Parameters:
key
- the key- Returns:
- the integral number located at the given key wrapped in an OptionalLong
-
getLong
public Long getLong(String key)
Returns the integral number located at the given key 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.- Parameters:
key
- the key- Returns:
- the integral number located at the given key or null
-
getOptObj
public Optional<JsObj> getOptObj(String key)
Returns the json object located at the given key orOptional.empty()
if it doesn't exist or it's not an object.- Parameters:
key
- the key- Returns:
- the json object located at the given key wrapped in an Optional
-
getObj
public JsObj getObj(String key)
Returns the json object located at the given key or null if it doesn't exist or it's not an object.- Parameters:
key
- the key- Returns:
- the json object located at the given key or null
-
getOptStr
public Optional<String> getOptStr(String key)
Returns the string located at the given key orOptional.empty()
if it doesn't exist or it's not an string.- Parameters:
key
- the key- Returns:
- the string located at the given key wrapped in an Optional
-
getStr
public String getStr(String key)
Returns the string located at the given key or null if it doesn't exist or it's not an string.- Parameters:
key
- the key- Returns:
- the string located at the given key or null
-
hashCode
public final 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
-
head
public final io.vavr.Tuple2<String,JsValue> head()
Returns a pair with an arbitrary key of this object and its associated element. When using head and tail to process a JsObj, the key of the pair returned must be passed in to get the tail using the methodtail()
.- Returns:
- an arbitrary
Map.Entry<String,JsElem>
of this JsObj - Throws:
UserError
- if this json object is empty
-
intersection
public JsObj intersection(JsObj that, JsArray.TYPE ARRAY_AS)
this.intersection(that, SET)
returns an array with the elements that exist in boththis
andthat
this.intersection(that, MULTISET)
returns an array with the elements that exist in boththis
andthat
, being duplicates allowed.this.intersection(that, LIST)
returns an array with the elements that exist in boththis
andthat
, and are located at the same position.- Parameters:
that
- the other objARRAY_AS
- option to define if arrays are considered SETS, LISTS OR MULTISET- Returns:
- a new JsObj of the same type as the inputs (mutable or immutable)
-
intersectionAll
public JsObj intersectionAll(JsObj that, JsArray.TYPE ARRAY_AS)
this.intersectionAll(that)
behaves asthis.intersection(that, LIST)
, but 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.- Parameters:
that
- the other objectARRAY_AS
- option to define if arrays are considered SETS, LISTS OR MULTISET- Returns:
- a new JsObj of the same type as the inputs (mutable or immutable)
-
isEmpty
public final boolean isEmpty()
Description copied from interface:Json
return true if there's no element in this json
-
isObj
public boolean isObj()
-
mapAllKeys
public final JsObj mapAllKeys(Function<? super JsPair,String> fn)
Description copied from interface:Json
Maps all the keys of this json.- Specified by:
mapAllKeys
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map values
,to map jsons
,to map only the first level
-
mapAllKeys
public final JsObj mapAllKeys(Function<? super JsPair,String> fn, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Maps all the keys of this json that satisfies a given predicate.- Specified by:
mapAllKeys
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what keys will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map values
,to map jsons
,to map only the first level
-
mapAllObjs
public final JsObj mapAllObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn, BiPredicate<? super JsPath,? super JsObj> predicate)
Description copied from interface:Json
Maps all the jsons of this json that satisfies a given predicate.- Specified by:
mapAllObjs
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what Jsons will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map values
,to map keys of json objects
,to map only the first level
-
mapAllObjs
public final JsObj mapAllObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn)
Description copied from interface:Json
Maps all the jsons of this json.- Specified by:
mapAllObjs
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map values
,to map keys of json objects
,to map only the first level
-
mapAllValues
public final JsObj mapAllValues(Function<? super JsPair,? extends JsValue> fn)
Description copied from interface:Json
Maps all the values of this json.- Specified by:
mapAllValues
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map jsons
,to map keys of json objects
,to map only the first level
-
mapAllValues
public final JsObj mapAllValues(Function<? super JsPair,? extends JsValue> fn, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Maps all the values of this json that satisfies a given predicate.- Specified by:
mapAllValues
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what JsValues will be mapped- Returns:
- same this instance or a new mapped json of the same type TT
- See Also:
to map jsons
,to map keys of json objects
,to map only the first level
-
mapKeys
public final JsObj mapKeys(Function<? super JsPair,String> fn)
Description copied from interface:Json
Maps the keys in the first level of this json.- Specified by:
mapKeys
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map values
,to map jsons
,to map all the keys and not only the first level
-
mapKeys
public final JsObj mapKeys(Function<? super JsPair,String> fn, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Maps the keys in the first level of this json that satisfies a given predicate.- Specified by:
mapKeys
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what keys will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map jsons
,to map values
,to map all the keys and not only the first level
-
mapObjs
public final JsObj mapObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn, BiPredicate<? super JsPath,? super JsObj> predicate)
Description copied from interface:Json
Maps the jsons in the first level of this json that satisfies a given predicate.- Specified by:
mapObjs
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what Jsons will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map values
,to map keys of json objects
,to map all the jsons and not only the first level
-
mapObjs
public final JsObj mapObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn)
Description copied from interface:Json
Maps the jsons in the first level of this json.- Specified by:
mapObjs
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map values
,to map keys of json objects
,to map all the jsons and not only the first level
-
mapValues
public final JsObj mapValues(Function<? super JsPair,? extends JsValue> fn)
Description copied from interface:Json
Maps the values in the first level of this json.- Specified by:
mapValues
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map jsons
,to map keys of json objects
,to map all the values and not only the first level
-
mapValues
public final JsObj mapValues(Function<? super JsPair,? extends JsValue> fn, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Maps the values in the first level of this json that satisfies a given predicate.- Specified by:
mapValues
in interfaceJson<JsObj>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what JsValues will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map jsons
,to map keys of json objects
,to map all the values and not only the first level
-
of
public static JsObj of(String key, JsValue el)
Returns a one-element immutable object.- Parameters:
key
- name of a keyel
- JsElem to be associated to the key- Returns:
- an immutable one-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2)
Returns a two-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2- Returns:
- an immutable two-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3)
Returns a three-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3- Returns:
- an immutable three-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4)
Returns a four-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4- Returns:
- an immutable four-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5)
Returns a five-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4key5
- name of a keyel5
- JsElem to be associated to the key5- Returns:
- an immutable five-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6)
Returns a six-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4key5
- name of a keyel5
- JsElem to be associated to the key5key6
- name of a keyel6
- JsElem to be associated to the key6- Returns:
- an immutable six-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7)
Returns a six-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4key5
- name of a keyel5
- JsElem to be associated to the key5key6
- name of a keyel6
- JsElem to be associated to the key6key7
- name of a keyel7
- JsElem to be associated to the key7- Returns:
- an immutable seven-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8)
Returns a six-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4key5
- name of a keyel5
- JsElem to be associated to the key5key6
- name of a keyel6
- JsElem to be associated to the key6key7
- name of a keyel7
- JsElem to be associated to the key7key8
- name of a keyel8
- JsElem to be associated to the key8- Returns:
- an immutable eight-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8, String key9, JsValue el9)
Returns a six-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4key5
- name of a keyel5
- JsElem to be associated to the key5key6
- name of a keyel6
- JsElem to be associated to the key6key7
- name of a keyel7
- JsElem to be associated to the key7key8
- name of a keyel8
- JsElem to be associated to the key8key9
- name of a keyel9
- JsElem to be associated to the key9- Returns:
- an immutable nine-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8, String key9, JsValue el9, String key10, JsValue el10)
Returns a six-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4key5
- name of a keyel5
- JsElem to be associated to the key5key6
- name of a keyel6
- JsElem to be associated to the key6key7
- name of a keyel7
- JsElem to be associated to the key7key8
- name of a keyel8
- JsElem to be associated to the key8key9
- name of a keyel9
- JsElem to be associated to the key9key10
- name of a keyel10
- JsElem to be associated to the key10- Returns:
- an immutable ten-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8, String key9, JsValue el9, String key10, JsValue el10, String key11, JsValue el11)
Returns a six-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4key5
- name of a keyel5
- JsElem to be associated to the key5key6
- name of a keyel6
- JsElem to be associated to the key6key7
- name of a keyel7
- JsElem to be associated to the key7key8
- name of a keyel8
- JsElem to be associated to the key8key9
- name of a keyel9
- JsElem to be associated to the key9key10
- name of a keyel10
- JsElem to be associated to the key10key11
- name of a keyel11
- JsElem to be associated to the key11- Returns:
- an immutable eleven-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(String key1, JsValue el1, String key2, JsValue el2, String key3, JsValue el3, String key4, JsValue el4, String key5, JsValue el5, String key6, JsValue el6, String key7, JsValue el7, String key8, JsValue el8, String key9, JsValue el9, String key10, JsValue el10, String key11, JsValue el11, String key12, JsValue el12)
Returns a six-element immutable object.- Parameters:
key1
- name of a keyel1
- JsElem to be associated to the key1key2
- name of a keyel2
- JsElem to be associated to the key2key3
- name of a keyel3
- JsElem to be associated to the key3key4
- name of a keyel4
- JsElem to be associated to the key4key5
- name of a keyel5
- JsElem to be associated to the key5key6
- name of a keyel6
- JsElem to be associated to the key6key7
- name of a keyel7
- JsElem to be associated to the key7key8
- name of a keyel8
- JsElem to be associated to the key8key9
- name of a keyel9
- JsElem to be associated to the key9key10
- name of a keyel10
- JsElem to be associated to the key10key11
- name of a keyel11
- JsElem to be associated to the key11key12
- name of a keyel12
- JsElem to be associated to the key12- Returns:
- an immutable twelve-element JsObj
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsObj of(JsPair pair, JsPair... others)
Returns an immutable object from one or more pairs.- Parameters:
pair
- a pairothers
- more optional pairs- Returns:
- an immutable JsObject
- Throws:
UserError
- if an elem of a pair is mutable
-
parse
public static JsObj parse(String str) throws MalformedJson
Tries to parse the string into an immutable object.- Parameters:
str
- the string to be parsed- Returns:
- a JsOb object
- Throws:
MalformedJson
- if the string doesnt represent a json object
-
parse
public static JsObj parse(String str, ParseBuilder builder) throws MalformedJson
Tries to parse the string into an immutable object, performing the specified transformations during the parsing. It's faster to do certain operations right while the parsing instead of doing the parsing and applying them later.- Parameters:
str
- string to be parsedbuilder
- builder with the transformations that will be applied during the parsing- Returns:
- a JsObj object
- Throws:
MalformedJson
- if the string doesnt represent a json object
-
prepend
public final JsObj prepend(JsPath path, JsValue elem)
Description copied from interface:Json
prepends one element to the array located at the path in this json. If the array at the path doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays withJsNull
when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
-
prependAll
public final JsObj prependAll(JsPath path, JsArray elems)
Description copied from interface:Json
prepends all the elements of the array, starting from the head, to the array located at the path in this json. If the array at the path doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays withJsNull
when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.- Specified by:
prependAll
in interfaceJson<JsObj>
- Parameters:
path
- the JsPath pointing to the array in which all the elements will be prependedelems
- the JsArray of elements to be prepended to the existing or created array- Returns:
- same this instance or a new json of the same type T
-
put
public JsObj put(String key, JsValue value)
Inserts the element at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the element- Returns:
- a new json object
-
put
public JsObj put(String key, String value)
Inserts the string at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the string- Returns:
- a new json object
-
put
public JsObj put(String key, int value)
Inserts the integer number at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the number- Returns:
- a new json object
-
put
public JsObj put(String key, long value)
Inserts the long number at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the number- Returns:
- a new json object
-
put
public JsObj put(String key, boolean value)
Inserts the boolean at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the boolean- Returns:
- a new json object
-
put
public JsObj put(String key, JsObj value)
Inserts the object at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the object- Returns:
- a new json object
-
put
public JsObj put(String key, JsArray value)
Inserts the array at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the array- Returns:
- a new json object
-
put
public JsObj put(String key, BigDecimal value)
Inserts the big decimal number at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the number- Returns:
- a new json object
-
put
public JsObj put(String key, BigInteger value)
Inserts the big integer number at the key in this json, replacing any existing element.- Parameters:
key
- the keyvalue
- the number- Returns:
- a new json object
-
put
public final JsObj put(String key, Function<? super JsValue,? extends JsValue> fn)
Inserts the element returned by the function at the given key in this json, replacing any existing element- Parameters:
key
- the keyfn
- the function that takes as an input the JsElem at the key and produces the JsElem to be inserted at the key- Returns:
- the same instance or a new json of the same type T
-
put
public final JsObj put(JsPath path, Function<? super JsValue,? extends JsValue> fn)
Description copied from interface:Json
Inserts the element returned by the function at the given path in this json, replacing any existing element and filling withJsNull
empty indexes in arrays when necessary. The same instance is returned when the path is empty, or 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. In both cases the function is not invoked. The same instance is returned as well when the element returned by the function isJsNothing
-
putIfAbsent
public JsObj putIfAbsent(String key, Supplier<? extends JsValue> supplier)
Inserts the element returned by the supplier at the given key in this json, if no element is present. The supplier is not invoked if the element is present.- Parameters:
key
- the keysupplier
- the supplier which computes the new JsElem if absent- Returns:
- the same instance or a new obj
-
putIfAbsent
public JsObj putIfAbsent(String key, int number)
Inserts at the given key in this json, if no element is present, the specified integer.- Parameters:
key
- the keynumber
- the integer- Returns:
- the same instance or a new obj
-
putIfAbsent
public JsObj putIfAbsent(String key, String str)
Inserts at the given key in this json, if no element is present, the specified string.- Parameters:
key
- the keystr
- the string- Returns:
- the same instance or a new obj
-
putIfAbsent
public JsObj putIfAbsent(String key, boolean bool)
Inserts at the given key in this json, if no element is present, the specified boolean.- Parameters:
key
- the keybool
- the boolean- Returns:
- the same instance or a new obj
-
putIfAbsent
public JsObj putIfAbsent(String key, JsObj obj)
Inserts at the given key in this json, if no element is present, the specified obj.- Parameters:
key
- the keyobj
- the json object- Returns:
- the same instance or a new obj
-
putIfAbsent
public JsObj putIfAbsent(String key, JsArray array)
Inserts at the given key in this json, if no element is present, the specified array.- Parameters:
key
- the keyarray
- the json array- Returns:
- the same instance or a new obj
-
putIfAbsent
public JsObj putIfAbsent(String key, long number)
Inserts at the given key in this json, if no element is present, the specified long number.- Parameters:
key
- the keynumber
- the number- Returns:
- the same instance or a new obj
-
putIfAbsent
public JsObj putIfAbsent(String key, double number)
Inserts at the given key in this json, if no element is present, the specified double number.- Parameters:
key
- the keynumber
- the number- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, int number)
Inserts at the given key in this json, if some element is present, the specified integer number.- Parameters:
key
- the keynumber
- the number- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, String str)
Inserts at the given key in this json, if some element is present, the specified string.- Parameters:
key
- the keystr
- the string- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, JsObj obj)
Inserts at the given key in this json, if some element is present, the specified obj.- Parameters:
key
- the keyobj
- the object- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, JsArray array)
Inserts at the given key in this json, if some element is present, the specified array.- Parameters:
key
- the keyarray
- the array- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, long number)
Inserts at the given key in this json, if some element is present, the specified long number.- Parameters:
key
- the keynumber
- the number- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, double number)
Inserts at the given key in this json, if some element is present, the specified double number.- Parameters:
key
- the keynumber
- the number- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, boolean bool)
Inserts at the given key in this json, if some element is present, the specified boolean.- Parameters:
key
- the keybool
- the boolean- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, BigInteger number)
Inserts at the given key in this json, if some element is present, the specified big integer number.- Parameters:
key
- the keynumber
- the number- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, BigDecimal number)
Inserts at the given key in this json, if some element is present, the specified big decimal number.- Parameters:
key
- the keynumber
- the number- Returns:
- the same instance or a new obj
-
putIfPresent
public JsObj putIfPresent(String key, Function<? super JsValue,? extends JsValue> fn)
Inserts at the given key in this json, if some element is present, the element returned by the function.- Parameters:
key
- the keyfn
- the function which computes the new JsElem from the existing one- Returns:
- the same instance or a new obj
-
reduce
public final <R> Optional<R> reduce(BinaryOperator<R> op, Function<? super JsPair,R> map, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Performs a reduction on the values that satisfy the predicate in the first level of this json. The reduction is performed mapping each value with the mapping function and then applying the operator- Specified by:
reduce
in interfaceJson<JsObj>
- Type Parameters:
R
- the type of the operands of the operator- Parameters:
op
- 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 reduced- Returns:
- an
Optional
describing the of of the reduction - See Also:
to apply the reduction in all the Json and not only in the first level
-
reduceAll
public final <R> Optional<R> reduceAll(BinaryOperator<R> op, Function<? super JsPair,R> map, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Performs a reduction on the values of this json that satisfy the predicate. The reduction is performed mapping each value with the mapping function and then applying the operator- Specified by:
reduceAll
in interfaceJson<JsObj>
- Type Parameters:
R
- the type of the operands of the operator- Parameters:
op
- 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 reduced- Returns:
- an
Optional
describing the result of the reduction - See Also:
to apply the reduction only in the first level
-
remove
public final JsObj remove(JsPath path)
Description copied from interface:Json
Removes the element in this json located at the given path, if it exists, returning the same this instance otherwise
-
same
public final boolean same(JsObj obj)
-
size
public final int size()
Description copied from interface:Json
Returns the number of elements in the first level of this json
-
stream
public final Stream<JsPair> stream()
Description copied from interface:Json
Returns a stream over the pairs of elements in the first level of this json object.
-
streamAll
public final Stream<JsPair> streamAll()
Description copied from interface:Json
Returns a stream over all the pairs of elements in this json object.
-
tail
public final JsObj tail()
Returns a new object with all the entries of this json object except the one with the given key.- Returns:
- a new JsObj
- Throws:
UserError
- if this json object is empty
-
toString
public final String toString()
// Single-check idiom Item 83 from effective java
-
union
public final JsObj union(JsObj that)
returnsthis
json object plus those pairs from the given json objectthat
which keys don't exist inthis
. Taking that into account, it's not a commutative operation unless the elements associated with the keys that exist in both json objects are equals.- Parameters:
that
- the given json object- Returns:
- a new JsObj of the same type as the inputs (mutable or immutable)
-
unionAll
public final JsObj unionAll(JsObj that, JsArray.TYPE ARRAY_AS)
behaves like theunion(JsObj)
but, for those keys that exit in boththis
andthat
json objects, which associated elements are **containers of the same type**, the result is their union. In this case, we can specify if arrays are considered Sets, Lists, or MultiSets. So this operation is kind of a 'recursive' union.- Parameters:
that
- the given json objectARRAY_AS
- option to define if arrays are considered SETS, LISTS OR MULTISET- Returns:
- a new JsObj of the same type as the inputs (mutable or immutable)
-
-