Class JsonValue
- Direct Known Subclasses:
JsonCaveatSet
-
Constructor Summary
ConstructorDescriptionConstructs 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
Modifier and TypeMethodDescriptionAdds the specified value to the list.Adds the specified value to the end of the list.Adds the specified value.add
(JsonPointer pointer, Object object) Adds the value identified by the specified pointer, relative to this value as root.addIfNotNull
(String key, Object object) Adds the specified object value only if it is not nulladdPermissive
(JsonPointer pointer, Object object) Adds the value identified by the specified pointer, relative to this value as root.Returns a mutable JSON array containing the provided objects.<V,
E extends Exception>
VReturns the JSON value as an object whose type (and value) is specified by a transformation function.asAsync
(AsyncFunction<JsonValue, V, E> transformFunction) Returns the JSON value as a promised object whose type (and value) is specified by a transformation function.Returns the JSON value as aBoolean
object.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.asDouble()
Returns the JSON value as aDouble
object.Returns the JSON value as anInteger
object.asList()
Returns the JSON value as aList
object.<E> List<E>
Returns the JSON value as aList
containing objects of the specified type.asLong()
Returns the JSON value as aLong
object.asMap()
Returns the JSON value as aMap
object.Returns the JSON value as aMap
containing objects of the specified type.asMapOfList
(Class<E> elementType) Returns the JSON value as aMap
containing a collection of objects of the specified type.asNumber()
Returns the JSON value as aNumber
object.asString()
Returns the JSON value as aString
object.void
clear()
Removes all child values from this JSON value, if it has any.clone()
Returns a shallow copy of this JSON value.boolean
Returnstrue
this JSON value contains an item with the specified value.copy()
Returns a deep copy of this JSON value.Defaults the JSON value to the specified value if it is currentlynull
.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.Called to enforce that the JSON value is of a particular type.Returns a JSON field for inclusion in a JSON object usingobject
.fieldIfNotNull
(String key, Object value) Returns a JSON field for inclusion in a JSON object usingobject
only if its value is notnull
.get
(int index) Returns the specified child value.Returns the specified item value.get
(JsonPointer pointer) Returns the specified child value with a pointer, relative to this value as root.Returns the raw Java object representing this JSON value.Returns the pointer of the JSON value in its JSON structure.boolean
Returnstrue
if the JSON value is aBoolean
.boolean
Returnstrue
if the JSON value is aCollection
.boolean
Returnstrue
if this JSON value contains the specified item.boolean
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
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()
Returns an iterator over the child values that this JSON value contains.static JsonValue
Returns a JSON value whose content is the provided object.keys()
Returns the set of keys for this JSON value's child values.Creates a new JSON value which combines the contents this JSON value with another.object
(int size) Produces an empty JSON object pre-allocated forsize
fields
.Returns a JSON object comprised of the provided JSONfields
.void
Applies a set of modifications in a JSON patch value to the current object, resulting in the intended target value.Sets the value of the specified child list element.Sets the value of the specified member.put
(JsonPointer pointer, Object object) Sets the value identified by the specified pointer, relative to this value as root.putIfNotNull
(String key, Object object) Sets the value of the specified member, only if it is not null.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
Removes the specified child value.void
remove
(JsonPointer pointer) Removes the specified child value with a pointer, relative to this value as root.required()
Throws aJsonValueException
if the JSON value isnull
.void
Sets the Java object representing this JSON value.int
size()
Returns the number of values that this JSON value contains.stream()
Construct a Stream from this JsonValue object.static int
Returns the key as an list index value.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 Details
-
JsonValue
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
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 Details
-
array
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
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:
-
field
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
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
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
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
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
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
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
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
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
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
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
- Returns:
- the boolean value.
- Throws:
JsonValueException
- if the JSON value is not a boolean type.
-
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
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
- Returns:
- the collection value, or
null
if no value. - Throws:
JsonValueException
- if the JSON value is not aCollection
.
-
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
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
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
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
.
-
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Returns the raw Java object representing this JSON value.- Returns:
- the raw Java object representing this JSON value.
-
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
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
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
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
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
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
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
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
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
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
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
Throws aJsonValueException
if the JSON value isnull
.- Returns:
- this JSON value.
- Throws:
JsonValueException
- if the JSON value isnull
.
-
setObject
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
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
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
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
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
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
.
-
stream
Construct a Stream from this JsonValue object.- Returns:
- A Stream of JsonValue-wrapped Objects.
-