Class JsonValue
- java.lang.Object
-
- org.forgerock.json.JsonValue
-
-
Constructor Summary
Constructors Constructor Description JsonValue(Object object)
Constructs a JSON value object with a given object.JsonValue(Object object, JsonPointer pointer)
Constructs a JSON value object with a given object and pointer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonValue
add(int index, Object object)
Adds the specified value to the list.JsonValue
add(Object object)
Adds the specified value to the end of the list.JsonValue
add(String key, Object object)
Adds the specified value.JsonValue
add(JsonPointer pointer, Object object)
Adds the value identified by the specified pointer, relative to this value as root.JsonValue
addIfNotNull(String key, Object object)
Adds the specified object value only if it is not nullJsonValue
addPermissive(JsonPointer pointer, Object object)
Adds the value identified by the specified pointer, relative to this value as root.static List<Object>
array(Object... objects)
Returns a mutable JSON array containing the provided objects.<V,E extends Exception>
Vas(Function<JsonValue,V,E> transformFunction)
Returns the JSON value as an object whose type (and value) is specified by a transformation function.<V,E extends Exception>
Promise<V,E>asAsync(AsyncFunction<JsonValue,V,E> transformFunction)
Returns the JSON value as a promised object whose type (and value) is specified by a transformation function.Boolean
asBoolean()
Returns the JSON value as aBoolean
object.Collection<Object>
asCollection()
Returns the JSON value as aCollection
object.<E> Collection<E>
asCollection(Class<E> type)
Returns the JSON value as aCollection
containing objects of the specified type.Double
asDouble()
Returns the JSON value as aDouble
object.Integer
asInteger()
Returns the JSON value as anInteger
object.List<Object>
asList()
Returns the JSON value as aList
object.<E> List<E>
asList(Class<E> type)
Returns the JSON value as aList
containing objects of the specified type.Long
asLong()
Returns the JSON value as aLong
object.Map<String,Object>
asMap()
Returns the JSON value as aMap
object.<V> Map<String,V>
asMap(Class<V> type)
Returns the JSON value as aMap
containing objects of the specified type.<E> Map<String,List<E>>
asMapOfList(Class<E> elementType)
Returns the JSON value as aMap
containing a collection of objects of the specified type.Number
asNumber()
Returns the JSON value as aNumber
object.String
asString()
Returns the JSON value as aString
object.void
clear()
Removes all child values from this JSON value, if it has any.JsonValue
clone()
Returns a shallow copy of this JSON value.boolean
contains(Object object)
Returnstrue
this JSON value contains an item with the specified value.JsonValue
copy()
Returns a deep copy of this JSON value.JsonValue
defaultTo(Object object)
Defaults the JSON value to the specified value if it is currentlynull
.JsonValue
diff(JsonValue target)
Performs a deep comparison of this JSON vlaue with another JSON value, and produces a JSON Patch value, which contains the operations necessary to modify the current value to arrive at thetarget
value.JsonValue
expect(Class<?> type)
Called to enforce that the JSON value is of a particular type.static Map.Entry<String,Object>
field(String key, Object value)
Returns a JSON field for inclusion in a JSON object usingobject
.static Map.Entry<String,Object>
fieldIfNotNull(String key, Object value)
Returns a JSON field for inclusion in a JSON object usingobject
only if its value is notnull
.JsonValue
get(int index)
Returns the specified child value.JsonValue
get(String key)
Returns the specified item value.JsonValue
get(JsonPointer pointer)
Returns the specified child value with a pointer, relative to this value as root.Object
getObject()
Returns the raw Java object representing this JSON value.JsonPointer
getPointer()
Returns the pointer of the JSON value in its JSON structure.boolean
isBoolean()
Returnstrue
if the JSON value is aBoolean
.boolean
isCollection()
Returnstrue
if the JSON value is aCollection
.boolean
isDefined(String key)
Returnstrue
if this JSON value contains the specified item.boolean
isEqualTo(JsonValue other)
Performs a deep comparison of this JSON value with another JSON value, and returns whether the two objects are identical.boolean
isList()
Returnstrue
if the JSON value is aList
.boolean
isMap()
Returnstrue
if the JSON value is aMap
.boolean
isNotNull()
Returnstrue
if the value is notnull
.boolean
isNull()
Returnstrue
if the value isnull
.boolean
isNumber()
Returnstrue
if the JSON value is aNumber
.boolean
isString()
Returnstrue
if the JSON value is aString
.Iterator<JsonValue>
iterator()
Returns an iterator over the child values that this JSON value contains.static JsonValue
json(Object object)
Returns a JSON value whose content is the provided object.Set<String>
keys()
Returns the set of keys for this JSON value's child values.JsonValue
merge(JsonValue other)
Creates a new JSON value which combines the contents this JSON value with another.static Map<String,Object>
object(int size)
Produces an empty JSON object pre-allocated forsize
fields
.static Map<String,Object>
object(Map.Entry<String,Object>... fields)
Returns a JSON object comprised of the provided JSONfields
.void
patch(JsonValue patch)
Applies a set of modifications in a JSON patch value to the current object, resulting in the intended target value.JsonValue
put(int index, Object object)
Sets the value of the specified child list element.JsonValue
put(String key, Object object)
Sets the value of the specified member.JsonValue
put(JsonPointer pointer, Object object)
Sets the value identified by the specified pointer, relative to this value as root.JsonValue
putIfNotNull(String key, Object object)
Sets the value of the specified member, only if it is not null.JsonValue
putPermissive(JsonPointer pointer, Object object)
Sets the value identified by the specified pointer, relative to this value as root.void
remove(int index)
Removes the specified child value, shifting any subsequent elements to the left.void
remove(String key)
Removes the specified child value.void
remove(JsonPointer pointer)
Removes the specified child value with a pointer, relative to this value as root.JsonValue
required()
Throws aJsonValueException
if the JSON value isnull
.void
setObject(Object object)
Sets the Java object representing this JSON value.int
size()
Returns the number of values that this JSON value contains.Stream<JsonValue>
stream()
Construct a Stream from this JsonValue object.static int
toIndex(String key)
Returns the key as an list index value.String
toString()
Returns a string representation of the JSON value.-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
JsonValue
public JsonValue(Object object)
Constructs a JSON value object with a given object. This constructor will automatically unwrapJsonValue
objects.- Parameters:
object
- the Java object representing the JSON value.
-
JsonValue
public JsonValue(Object object, JsonPointer pointer)
Constructs a JSON value object with a given object and pointer. This constructor will automatically unwrapJsonValue
objects.- Parameters:
object
- the Java object representing the JSON value.pointer
- the pointer to the value in a JSON structure.
-
-
Method Detail
-
array
public static List<Object> array(Object... objects)
Returns a mutable JSON array containing the provided objects. This method is provided as a convenience method for constructing JSON arrays. Example usage:JsonValue value = json(array(1, 2, 3));
- Parameters:
objects
- The array elements.- Returns:
- A JSON array.
-
fieldIfNotNull
public static Map.Entry<String,Object> fieldIfNotNull(String key, Object value)
Returns a JSON field for inclusion in a JSON object usingobject
only if its value is notnull
. Example usage:JsonValue value = json(object(fieldIfNotNull("uid", getUid()));
Note: This feature depends on the
object(java.util.Map.Entry...)
method that checks if the entry is notnull
before including it into the map.- Parameters:
key
- The JSON field name.value
- The JSON field value (may benull
).- Returns:
- The JSON field for inclusion in a JSON object or
null
. - See Also:
object(java.util.Map.Entry...)
-
field
public static Map.Entry<String,Object> field(String key, Object value)
Returns a JSON field for inclusion in a JSON object usingobject
. This method is provided as a convenience method for constructing JSON objects. Example usage:JsonValue value = json(object(field("uid", "bjensen"), field("age", 30)));
- Parameters:
key
- The JSON field name.value
- The JSON field value.- Returns:
- The JSON field for inclusion in a JSON object.
-
json
public static JsonValue json(Object object)
Returns a JSON value whose content is the provided object. This method is provided as a convenience method for constructing JSON objects, instead of usingJsonValue(Object)
. Example usage:JsonValue value = json(object(field("uid", "bjensen"), field("roles", array("sales", "marketing"))));
- Parameters:
object
- the Java object representing the JSON value.- Returns:
- The JSON value.
-
object
@SafeVarargs public static Map<String,Object> object(Map.Entry<String,Object>... fields)
Returns a JSON object comprised of the provided JSONfields
. This method is provided as a convenience method for constructing JSON objects. Example usage:JsonValue value = json(object(field("uid", "bjensen"), field("age", 30)));
- Parameters:
fields
- The list offields
to include in the JSON object.null
elements are allowed, but are not included in the returned map (this makes it easier to include optional elements).- Returns:
- The JSON object.
-
object
public static Map<String,Object> object(int size)
Produces an empty JSON object pre-allocated forsize
fields
. This method is provided as a convenience method for constructing JSON objects. Example usage:JsonValue value = json(object(20)); for (Map.Entry<String, Object> entry : someMap.entrySet()) { value.put(entry.getKey(), entry.getValue()); }
- Parameters:
size
- The size of the JSON object to allocate.- Returns:
- The [empty] JSON object.
-
toIndex
public static int toIndex(String key)
Returns the key as an list index value. If the string does not represent a valid list index value, then-1
is returned.- Parameters:
key
- the key to be converted into an list index value.- Returns:
- the converted index value, or
-1
if invalid.
-
add
public JsonValue add(int index, Object object)
Adds the specified value to the list. Adding a value to a list shifts any existing elements at or above the specified index to the right by one.- Parameters:
index
- theList
index of the value to add.object
- the java object to add.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if this JSON value is not aList
or index is out of range.
-
add
public JsonValue add(JsonPointer pointer, Object object)
Adds the value identified by the specified pointer, relative to this value as root. If doing so would require the creation of a new object or list, aJsonValueException
will be thrown.NOTE: values may be added to a list using the reserved JSON pointer token "-". For example, the pointer "/a/b/-" will add a new element to the list referenced by "/a/b".
- Parameters:
pointer
- identifies the child value to add.object
- the Java object value to add.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if the specified pointer is invalid.
-
add
public JsonValue add(Object object)
Adds the specified value to the end of the list. This method is equivalent to the following code:add(size(), object);
- Parameters:
object
- the java object to add.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if this JSON value is not aList
.
-
add
public JsonValue add(String key, Object object)
Adds the specified value.If adding to a list value, the specified key must be parseable as an unsigned base-10 integer and be less than or equal to the list size. Adding a value to a list shifts any existing elements at or above the specified index to the right by one.
- Parameters:
key
- theMap
key orList
index to add.object
- the Java object to add.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if not aMap
orList
, theMap
key already exists, or theList
index is out of range.
-
addIfNotNull
public JsonValue addIfNotNull(String key, Object object)
Adds the specified object value only if it is not nullFor further usage information, see documentation for
add(String, Object)
.- Parameters:
key
- theMap
key orList
index to add.object
- the Java object to add.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if not aMap
orList
, theMap
key already exists, or theList
index is out of range.
-
addPermissive
public JsonValue addPermissive(JsonPointer pointer, Object object)
Adds the value identified by the specified pointer, relative to this value as root. Missing parent objects or lists will be created on demand.NOTE: values may be added to a list using the reserved JSON pointer token "-". For example, the pointer "/a/b/-" will add a new element to the list referenced by "/a/b".
- Parameters:
pointer
- identifies the child value to add.object
- the Java object value to add.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if the specified pointer is invalid.
-
asBoolean
public Boolean asBoolean()
- Returns:
- the boolean value.
- Throws:
JsonValueException
- if the JSON value is not a boolean type.
-
asDouble
public Double asDouble()
Returns the JSON value as aDouble
object. This may involve rounding. If the JSON value isnull
, this method returnsnull
.- Returns:
- the double-precision floating point value.
- Throws:
JsonValueException
- if the JSON value is not a number.
-
asInteger
public Integer asInteger()
Returns the JSON value as anInteger
object. This may involve rounding or truncation. If the JSON value isnull
, this method returnsnull
.- Returns:
- the integer value.
- Throws:
JsonValueException
- if the JSON value is not a number.
-
asCollection
public Collection<Object> asCollection()
- Returns:
- the collection value, or
null
if no value. - Throws:
JsonValueException
- if the JSON value is not aCollection
.
-
asList
public List<Object> asList()
Returns the JSON value as aList
object. If the JSON value isnull
, this method returnsnull
. The returnedList
is not a copy : any interaction with it will affect theJsonValue
.- Returns:
- the list value, or
null
if no value. - Throws:
JsonValueException
- if the JSON value is not aList
.
-
asCollection
public <E> Collection<E> asCollection(Class<E> type)
Returns the JSON value as aCollection
containing objects of the specified type. If the value isnull
, this method returnsnull
. If any of the elements of the collection are notnull
and not of the specified type,JsonValueException
is thrown. The returnedCollection
is not a copy : any interaction with it will affect theJsonValue
.- Type Parameters:
E
- the type of elements in this collection- Parameters:
type
- the type of object that all elements are expected to be.- Returns:
- the collection value, or
null
if no value. - Throws:
JsonValueException
- if the JSON value is not aCollection
or contains an unexpected type.NullPointerException
- iftype
isnull
.
-
asList
public <E> List<E> asList(Class<E> type)
Returns the JSON value as aList
containing objects of the specified type. If the value isnull
, this method returnsnull
. If any of the elements of the list are notnull
and not of the specified type,JsonValueException
is thrown. The returnedList
is not a copy : any interaction with it will affect theJsonValue
.- Type Parameters:
E
- the type of elements in this list- Parameters:
type
- the type of object that all elements are expected to be.- Returns:
- the list value, or
null
if no value. - Throws:
JsonValueException
- if the JSON value is not aList
or contains an unexpected type.NullPointerException
- iftype
isnull
.
-
as
public <V,E extends Exception> V as(Function<JsonValue,V,E> transformFunction) throws E extends Exception
Returns the JSON value as an object whose type (and value) is specified by a transformation function. It is up to the transformation function to transform/enforce source types of the elements in the Json source element and to decide what to do depending on the kind ofJsonValue
: if it is null, aString
, aList
, orMap
. If the type-transformation cannot occur, the exception specified by the transformation function is thrown.- Type Parameters:
V
- the type of elementE
- the type of exception thrown by the transformation function- Parameters:
transformFunction
- aFunction
to transform the JsonValue element to the desired type- Returns:
- the value, or
null
if no value. - Throws:
E
- if the JsonValue element cannot be transformedNullPointerException
- iftransformFunction
isnull
.E extends Exception
-
asAsync
public <V,E extends Exception> Promise<V,E> asAsync(AsyncFunction<JsonValue,V,E> transformFunction)
Returns the JSON value as a promised object whose type (and value) is specified by a transformation function. It is up to the transformation function to transform/enforce source types of the elements in the Json source element and to decide what to do depending on the kind ofJsonValue
: if it is null, aString
, aList
, orMap
. If the type-transformation cannot occur, the exception specified by the transformation function is thrown.- Type Parameters:
V
- the type of elementE
- the type of exception thrown by the transformation function- Parameters:
transformFunction
- aFunction
to transform the JsonValue element to the desired type- Returns:
- the promise of the value, the final value may be null, but the promise itself can't. In case of exception the result promise is failed and contains the exception.
-
asLong
public Long asLong()
Returns the JSON value as aLong
object. This may involve rounding or truncation. If the JSON value isnull
, this method returnsnull
.- Returns:
- the long integer value.
- Throws:
JsonValueException
- if the JSON value is not a number.
-
asMap
public Map<String,Object> asMap()
Returns the JSON value as aMap
object. If the JSON value isnull
, this method returnsnull
.- Returns:
- the map value, or
null
if no value. - Throws:
JsonValueException
- if the JSON value is not aMap
.
-
asMap
public <V> Map<String,V> asMap(Class<V> type)
Returns the JSON value as aMap
containing objects of the specified type. If the value isnull
, this method returnsnull
. If any of the values of the map are notnull
and not of the specified type,JsonValueException
is thrown.- Type Parameters:
V
- the type of values in this map- Parameters:
type
- the type of object that all values are expected to be.- Returns:
- the map value, or
null
if no value. - Throws:
JsonValueException
- if the JSON value is not aMap
or contains an unexpected type.NullPointerException
- iftype
isnull
.
-
asMapOfList
public <E> Map<String,List<E>> asMapOfList(Class<E> elementType)
Returns the JSON value as aMap
containing a collection of objects of the specified type. If the value isnull
, this method returnsnull
. If any of the values of the map are notnull
and not of the specified type,JsonValueException
is thrown.- Type Parameters:
E
- the type of elements in the collection- Parameters:
elementType
- the type of object that all collection elements are expected to be.- Returns:
- the map value, or
null
if no value. - Throws:
JsonValueException
- if the JSON value is not aMap
or contains an unexpected type.NullPointerException
- iftype
isnull
.
-
asNumber
public Number asNumber()
Returns the JSON value as aNumber
object. If the JSON value isnull
, this method returnsnull
.- Returns:
- the numeric value.
- Throws:
JsonValueException
- if the JSON value is not a number.
-
asString
public String asString()
Returns the JSON value as aString
object. If the JSON value isnull
, this method returnsnull
.- Returns:
- the string value.
- Throws:
JsonValueException
- if the JSON value is not a string.
-
clear
public void clear()
Removes all child values from this JSON value, if it has any.
-
clone
public JsonValue clone()
Returns a shallow copy of this JSON value. If this JSON value contains aMap
or aList
object, the returned JSON value will contain a shallow copy of the original contained object.The new value's members can be modified without affecting the original value. Modifying the member's members will almost certainly affect the original value. To avoid this, use the
copy()
method to return a deep copy of the JSON value.This method does not traverse the value's members, nor will it apply any transformations.
-
contains
public boolean contains(Object object)
Returnstrue
this JSON value contains an item with the specified value.- Parameters:
object
- the object to seek within this JSON value.- Returns:
true
if this value contains the specified member value.
-
copy
public JsonValue copy()
Returns a deep copy of this JSON value.Note: This method is recursive, and currently has no ability to detect or correct for structures containing cyclic references. Processing such a structure will result in a
StackOverflowError
being thrown.- Returns:
- a deep copy of this JSON value.
-
defaultTo
public JsonValue defaultTo(Object object)
Defaults the JSON value to the specified value if it is currentlynull
.- Parameters:
object
- the object to default to.- Returns:
- this JSON value or a new JSON value containing the default value.
-
expect
public JsonValue expect(Class<?> type)
Called to enforce that the JSON value is of a particular type. A value ofnull
is allowed.- Parameters:
type
- the class that the underlying value must have.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if the value is not the specified type.
-
get
public JsonValue get(int index)
Returns the specified child value. If this JSON value is not aList
or if no such child exists, then a JSON value containing anull
is returned.- Parameters:
index
- index of child element value to return.- Returns:
- the child value, or a JSON value containing
null
. - Throws:
JsonValueException
- if index is negative.
-
get
public JsonValue get(JsonPointer pointer)
Returns the specified child value with a pointer, relative to this value as root. If the specified child value does not exist, thennull
is returned.- Parameters:
pointer
- the JSON pointer identifying the child value to return.- Returns:
- the child value, or
null
if no such value exists.
-
get
public JsonValue get(String key)
Returns the specified item value. If no such member value exists, then a JSON value containingnull
is returned.- Parameters:
key
- theMap
key orList
index identifying the item to return.- Returns:
- a JSON value containing the value or
null
.
-
getObject
public Object getObject()
Returns the raw Java object representing this JSON value.- Returns:
- the raw Java object representing this JSON value.
-
getPointer
public JsonPointer getPointer()
Returns the pointer of the JSON value in its JSON structure.- Returns:
- the pointer of the JSON value in its JSON structure.
-
isBoolean
public boolean isBoolean()
Returnstrue
if the JSON value is aBoolean
.- Returns:
true
if the JSON value is aBoolean
.
-
isDefined
public boolean isDefined(String key)
Returnstrue
if this JSON value contains the specified item.- Parameters:
key
- theMap
key orList
index of the item to seek.- Returns:
true
if this JSON value contains the specified member.- Throws:
NullPointerException
- ifkey
isnull
.
-
isCollection
public boolean isCollection()
Returnstrue
if the JSON value is aCollection
.- Returns:
true
if the JSON value is aCollection
.
-
isList
public boolean isList()
Returnstrue
if the JSON value is aList
.- Returns:
true
if the JSON value is aList
.
-
isMap
public boolean isMap()
Returnstrue
if the JSON value is aMap
.- Returns:
true
if the JSON value is aMap
.
-
isNull
public boolean isNull()
Returnstrue
if the value isnull
.- Returns:
true
if the value isnull
.
-
isNotNull
public boolean isNotNull()
Returnstrue
if the value is notnull
.- Returns:
true
if the value is notnull
.
-
isNumber
public boolean isNumber()
Returnstrue
if the JSON value is aNumber
.- Returns:
true
if the JSON value is aNumber
.
-
isString
public boolean isString()
Returnstrue
if the JSON value is aString
.- Returns:
true
if the JSON value is aString
.
-
iterator
public Iterator<JsonValue> iterator()
Returns an iterator over the child values that this JSON value contains. If this value is aMap
, then the order of the resulting child values is undefined. Calling theIterator.remove()
method of the returned iterator will throw aUnsupportedOperationException
.
-
keys
public Set<String> keys()
Returns the set of keys for this JSON value's child values. If this value is aMap
, then the order of the resulting keys is the same as the underlying Map implementation. If there are no child values, this method returns an empty set.- Returns:
- the set of keys for this JSON value's child values.
-
put
public JsonValue put(int index, Object object)
Sets the value of the specified child list element.- Parameters:
index
- theList
index identifying the child value to set.object
- the Java value to assign to the list element.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if this JSON value is not aList
or index is out of range.
-
putIfNotNull
public JsonValue putIfNotNull(String key, Object object)
Sets the value of the specified member, only if it is not null.For further usage information, see documentation for
put(String, Object)
.- Parameters:
key
- theMap
key orList
index identifying the child value to set.object
- the object value to assign to the member.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if this JSON value is not aMap
orList
.NullPointerException
- ifkey
isnull
.
-
put
public JsonValue put(JsonPointer pointer, Object object)
Sets the value identified by the specified pointer, relative to this value as root. If doing so would require the creation of a new object or list, aJsonValueException
will be thrown.NOTE: values may be added to a list using the reserved JSON pointer token "-". For example, the pointer "/a/b/-" will add a new element to the list referenced by "/a/b".
- Parameters:
pointer
- identifies the child value to set.object
- the Java object value to set.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if the specified pointer is invalid.
-
put
public JsonValue put(String key, Object object)
Sets the value of the specified member.If setting a list element, the specified key must be parseable as an unsigned base-10 integer and be less than or equal to the size of the list.
- Parameters:
key
- theMap
key orList
index identifying the child value to set.object
- the object value to assign to the member.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if this JSON value is not aMap
orList
.NullPointerException
- ifkey
isnull
.
-
putPermissive
public JsonValue putPermissive(JsonPointer pointer, Object object)
Sets the value identified by the specified pointer, relative to this value as root. Missing parent objects or lists will be created on demand.NOTE: values may be added to a list using the reserved JSON pointer token "-". For example, the pointer "/a/b/-" will add a new element to the list referenced by "/a/b".
- Parameters:
pointer
- identifies the child value to set.object
- the Java object value to set.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if the specified pointer is invalid.
-
remove
public void remove(int index)
Removes the specified child value, shifting any subsequent elements to the left. If the JSON value is not aList
, calling this method has no effect.- Parameters:
index
- theList
index identifying the child value to remove.
-
remove
public void remove(JsonPointer pointer)
Removes the specified child value with a pointer, relative to this value as root. If the specified child value is not defined, calling this method has no effect.- Parameters:
pointer
- the JSON pointer identifying the child value to remove.
-
remove
public void remove(String key)
Removes the specified child value. If the specified child value is not defined, calling this method has no effect.- Parameters:
key
- theMap
key orList
index identifying the child value to remove.
-
required
public JsonValue required()
Throws aJsonValueException
if the JSON value isnull
.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if the JSON value isnull
.
-
setObject
public void setObject(Object object)
Sets the Java object representing this JSON value.This method will automatically unwrap
JsonValue
objects.- Parameters:
object
- the object to set.
-
size
public int size()
Returns the number of values that this JSON value contains.- Returns:
- the number of values that this JSON value contains.
-
toString
public String toString()
Returns a string representation of the JSON value. The result resembles—but is not guaranteed to conform to—JSON syntax. This method does not apply transformations to the value's children.
-
isEqualTo
public boolean isEqualTo(JsonValue other)
Performs a deep comparison of this JSON value with another JSON value, and returns whether the two objects are identical. Fails fast in that afalse
is returned as soon as a difference is detected.Note: Only values recognisable as JSON primitives (
Map
,List
,Number
,Boolean
,String
andnull
) are supported.- Parameters:
other
- another value.- Returns:
- whether the two objects are equal.
- Throws:
NullPointerException
- ifother
isnull
.IllegalArgumentException
- if this or theother
value contains non-JSON primitive values.
-
diff
public JsonValue diff(JsonValue target)
Performs a deep comparison of this JSON vlaue with another JSON value, and produces a JSON Patch value, which contains the operations necessary to modify the current value to arrive at thetarget
value.- Parameters:
target
- the intended target value.- Returns:
- the resulting JSON Patch value.
- Throws:
NullPointerException
- if either oforiginal
ortarget
arenull
.
-
patch
public void patch(JsonValue patch)
Applies a set of modifications in a JSON patch value to the current object, resulting in the intended target value. In the event of a failure, this method does not revert any modifications applied up to the point of failure.- Parameters:
patch
- the JSON Patch value, specifying the modifications to apply to the original value.- Throws:
JsonValueException
- if application of the patch failed.
-
merge
public JsonValue merge(JsonValue other)
Creates a new JSON value which combines the contents this JSON value with another.If there are any matching pointers in the two JSON values, then the value in
other
overrides the value inthis
.- Parameters:
other
- Another JSON value which should be merged with this one.- Returns:
- a new JSON value which combines the contents of this JSON value and
other
.
-
-