Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
JsonValue |
add(int index,
Object object)
Adds the specified value to the list.
|
JsonValue |
add(JsonPointer pointer,
Object object)
Adds the value identified by the specified pointer, relative to this
value as root.
|
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 |
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> |
as(Function<JsonValue,V,E> transformFunction)
Returns the JSON value as an object whose type
(and value) is specified by a transformation function.
|
Boolean |
asBoolean()
Returns the JSON value as a
Boolean object. |
Collection<Object> |
asCollection()
Returns the JSON value as a
Collection object. |
<E> Collection<E> |
asCollection(Class<E> type)
Returns the JSON value as a
Collection containing objects of the
specified type. |
Double |
asDouble()
Returns the JSON value as a
Double object. |
Integer |
asInteger()
Returns the JSON value as an
Integer object. |
List<Object> |
asList()
Returns the JSON value as a
List object. |
<E> List<E> |
asList(Class<E> type)
Returns the JSON value as a
List containing objects of the
specified type. |
Long |
asLong()
Returns the JSON value as a
Long object. |
Map<String,Object> |
asMap()
Returns the JSON value as a
Map object. |
<V> Map<String,V> |
asMap(Class<V> type)
Returns the JSON value as a
Map containing objects of the
specified type. |
<E> Map<String,List<E>> |
asMapOfList(Class<E> elementType)
Returns the JSON value as a
Map containing a collection of
objects of the specified type. |
Number |
asNumber()
Returns the JSON value as a
Number object. |
String |
asString()
Returns the JSON value as a
String 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)
Returns
true 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 currently
null . |
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 the
target 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 using
object . |
static Map.Entry<String,Object> |
fieldIfNotNull(String key,
Object value)
Returns a JSON field for inclusion in a JSON object using
object only if its value is not null . |
JsonValue |
get(int index)
Returns the specified child value.
|
JsonValue |
get(JsonPointer pointer)
Returns the specified child value with a pointer, relative to this value
as root.
|
JsonValue |
get(String key)
Returns the specified item value.
|
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()
Returns
true if the JSON value is a Boolean . |
boolean |
isCollection()
Returns
true if the JSON value is a Collection . |
boolean |
isDefined(String key)
Returns
true 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()
Returns
true if the JSON value is a List . |
boolean |
isMap()
Returns
true if the JSON value is a Map . |
boolean |
isNotNull()
Returns
true if the value is not null . |
boolean |
isNull()
Returns
true if the value is null . |
boolean |
isNumber()
Returns
true if the JSON value is a Number . |
boolean |
isString()
Returns
true if the JSON value is a String . |
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 for
size
fields . |
static Map<String,Object> |
object(Map.Entry<String,Object>... fields)
Returns a JSON object comprised of the provided JSON
fields . |
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(JsonPointer pointer,
Object object)
Sets the value identified by the specified pointer, relative to this
value as root.
|
JsonValue |
put(String key,
Object object)
Sets the value of the specified member.
|
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(JsonPointer pointer)
Removes the specified child value with a pointer, relative to this value
as root.
|
void |
remove(String key)
Removes the specified child value.
|
JsonValue |
required()
Throws a
JsonValueException if the JSON value is null . |
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.
|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
public JsonValue(Object object)
JsonValue
objects.object
- the Java object representing the JSON value.public JsonValue(Object object, JsonPointer pointer)
JsonValue
objects.object
- the Java object representing the JSON value.pointer
- the pointer to the value in a JSON structure.public static List<Object> array(Object... objects)
JsonValue value = json(array(1, 2, 3));
objects
- The array elements.public static Map.Entry<String,Object> fieldIfNotNull(String key, Object value)
object
only if its value is not null
.
Example usage:
JsonValue value = json(object(fieldIfNotNull("uid", getUid()));
Note: This feature depends on the JsonValue.object(java.util.Map.Entry...)
method that
checks if the entry is not null
before including it into the map.
key
- The JSON field name.value
- The JSON field value (may be null
).null
.JsonValue.object(java.util.Map.Entry...)
public static Map.Entry<String,Object> field(String key, Object value)
object
. This method is provided
as a convenience method for constructing JSON objects. Example usage:
JsonValue value = json(object(field("uid", "bjensen"), field("age", 30)));
key
- The JSON field name.value
- The JSON field value.public static JsonValue json(Object object)
JsonValue.JsonValue(Object)
. Example usage:
JsonValue value = json(object(field("uid", "bjensen"), field("roles", array("sales", "marketing"))));
object
- the Java object representing the JSON value.@SafeVarargs public static Map<String,Object> object(Map.Entry<String,Object>... fields)
fields
. This method is provided as a
convenience method for constructing JSON objects. Example usage:
JsonValue value = json(object(field("uid", "bjensen"), field("age", 30)));
fields
- The list of fields
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).public static Map<String,Object> object(int size)
size
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()); }
size
- The size of the JSON object to allocate.public static int toIndex(String key)
-1
is returned.key
- the key to be converted into an list index value.-1
if invalid.public JsonValue add(int index, Object object)
index
- the List
index of the value to add.object
- the java object to add.JsonValueException
- if this JSON value is not a List
or index is out of
range.public JsonValue add(JsonPointer pointer, Object object)
JsonValueException
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".
pointer
- identifies the child value to add.object
- the Java object value to add.JsonValueException
- if the specified pointer is invalid.public JsonValue add(Object object)
add(size(), object);
object
- the java object to add.JsonValueException
- if this JSON value is not a List
.public JsonValue add(String key, Object object)
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.
key
- the Map
key or List
index to add.object
- the Java object to add.JsonValueException
- if not a Map
or List
, the Map
key
already exists, or the List
index is out of range.public JsonValue addPermissive(JsonPointer pointer, Object object)
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".
pointer
- identifies the child value to add.object
- the Java object value to add.JsonValueException
- if the specified pointer is invalid.public Boolean asBoolean()
JsonValueException
- if the JSON value is not a boolean type.public Double asDouble()
Double
object. This may involve
rounding. If the JSON value is null
, this method returns
null
.JsonValueException
- if the JSON value is not a number.public Integer asInteger()
Integer
object. This may involve
rounding or truncation. If the JSON value is null
, this method
returns null
.JsonValueException
- if the JSON value is not a number.public Collection<Object> asCollection()
null
if no value.JsonValueException
- if the JSON value is not a Collection
.public List<Object> asList()
List
object. If the JSON value is
null
, this method returns null
.
The returned List
is not a copy : any interaction with it
will affect the JsonValue
.null
if no value.JsonValueException
- if the JSON value is not a List
.public <E> Collection<E> asCollection(Class<E> type)
Collection
containing objects of the
specified type. If the value is null
, this method returns
null
. If any of the elements of the collection are not null
and
not of the specified type, JsonValueException
is thrown.
The returned Collection
is not a copy : any interaction with it
will affect the JsonValue
.E
- the type of elements in this collectiontype
- the type of object that all elements are expected to be.null
if no value.JsonValueException
- if the JSON value is not a Collection
or contains an
unexpected type.NullPointerException
- if type
is null
.public <E> List<E> asList(Class<E> type)
List
containing objects of the
specified type. If the value is null
, this method returns
null
. If any of the elements of the list are not null
and
not of the specified type, JsonValueException
is thrown.
The returned List
is not a copy : any interaction with it
will affect the JsonValue
.E
- the type of elements in this listtype
- the type of object that all elements are expected to be.null
if no value.JsonValueException
- if the JSON value is not a List
or contains an unexpected type.NullPointerException
- if type
is null
.public <V,E extends Exception> V as(Function<JsonValue,V,E> transformFunction) throws E extends Exception
JsonValue
: if it is null, a String
, a List
,
or Map
. If the type-transformation cannot occur,
the exception specified by the transformation function is thrown.V
- the type of elementE
- the type of exception thrown by the transformation functiontransformFunction
- a Function
to transform the JsonValue element to the desired typenull
if no value.E
- if the JsonValue element cannot be transformedNullPointerException
- if transformFunction
is null
.E extends Exception
public Long asLong()
Long
object. This may involve
rounding or truncation. If the JSON value is null
, this method
returns null
.JsonValueException
- if the JSON value is not a number.public Map<String,Object> asMap()
Map
object. If the JSON value is
null
, this method returns null
.null
if no value.JsonValueException
- if the JSON value is not a Map
.public <V> Map<String,V> asMap(Class<V> type)
Map
containing objects of the
specified type. If the value is null
, this method returns
null
. If any of the values of the map are not null
and
not of the specified type, JsonValueException
is thrown.V
- the type of values in this maptype
- the type of object that all values are expected to be.null
if no value.JsonValueException
- if the JSON value is not a Map
or contains an
unexpected type.NullPointerException
- if type
is null
.public <E> Map<String,List<E>> asMapOfList(Class<E> elementType)
Map
containing a collection of
objects of the specified type. If the value is null
, this method
returns null
. If any of the values of the map are not null
and
not of the specified type, JsonValueException
is thrown.E
- the type of elements in the collectionelementType
- the type of object that all collection elements are
expected to be.null
if no value.JsonValueException
- if the JSON value is not a Map
or contains an
unexpected type.NullPointerException
- if type
is null
.public Number asNumber()
Number
object. If the JSON value is
null
, this method returns null
.JsonValueException
- if the JSON value is not a number.public String asString()
String
object. If the JSON value is
null
, this method returns null
.JsonValueException
- if the JSON value is not a string.public void clear()
public JsonValue clone()
Map
or a List
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 JsonValue.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.
public boolean contains(Object object)
true
this JSON value contains an item with the specified
value.object
- the object to seek within this JSON value.true
if this value contains the specified member value.public JsonValue copy()
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.
public JsonValue defaultTo(Object object)
null
.object
- the object to default to.public JsonValue expect(Class<?> type)
null
is allowed.type
- the class that the underlying value must have.JsonValueException
- if the value is not the specified type.public JsonValue get(int index)
List
or if no such child exists, then a JSON value containing a
null
is returned.index
- index of child element value to return.null
.JsonValueException
- if index is negative.public JsonValue get(JsonPointer pointer)
null
is returned.pointer
- the JSON pointer identifying the child value to return.null
if no such value exists.public JsonValue get(String key)
null
is returned.key
- the Map
key or List
index identifying the item
to return.null
.public Object getObject()
public JsonPointer getPointer()
public boolean isBoolean()
true
if the JSON value is a Boolean
.true
if the JSON value is a Boolean
.public boolean isDefined(String key)
true
if this JSON value contains the specified item.key
- the Map
key or List
index of the item to seek.true
if this JSON value contains the specified member.NullPointerException
- if key
is null
.public boolean isCollection()
true
if the JSON value is a Collection
.true
if the JSON value is a Collection
.public boolean isList()
true
if the JSON value is a List
.true
if the JSON value is a List
.public boolean isMap()
true
if the JSON value is a Map
.true
if the JSON value is a Map
.public boolean isNull()
true
if the value is null
.true
if the value is null
.public boolean isNotNull()
true
if the value is not null
.true
if the value is not null
.public boolean isNumber()
true
if the JSON value is a Number
.true
if the JSON value is a Number
.public boolean isString()
true
if the JSON value is a String
.true
if the JSON value is a String
.public Iterator<JsonValue> iterator()
Map
, then the order of the
resulting child values is undefined. Calling the Iterator.remove()
method of the returned iterator will throw a UnsupportedOperationException
.public Set<String> keys()
Map
, 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.public JsonValue put(int index, Object object)
index
- the List
index identifying the child value to set.object
- the Java value to assign to the list element.JsonValueException
- if this JSON value is not a List
or index is out of
range.public JsonValue put(JsonPointer pointer, Object object)
JsonValueException
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".
pointer
- identifies the child value to set.object
- the Java object value to set.JsonValueException
- if the specified pointer is invalid.public JsonValue put(String key, Object object)
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.
key
- the Map
key or List
index identifying the
child value to set.object
- the object value to assign to the member.JsonValueException
- if this JSON value is not a Map
or List
.NullPointerException
- if key
is null
.public JsonValue putPermissive(JsonPointer pointer, Object object)
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".
pointer
- identifies the child value to set.object
- the Java object value to set.JsonValueException
- if the specified pointer is invalid.public void remove(int index)
List
, calling this method
has no effect.index
- the List
index identifying the child value to remove.public void remove(JsonPointer pointer)
pointer
- the JSON pointer identifying the child value to remove.public void remove(String key)
key
- the Map
key or List
index identifying the
child value to remove.public JsonValue required()
JsonValueException
if the JSON value is null
.JsonValueException
- if the JSON value is null
.public void setObject(Object object)
This method will automatically unwrap JsonValue
objects.
object
- the object to set.public int size()
public String toString()
public boolean isEqualTo(JsonValue other)
false
is returned as soon as a difference is detected.
Note: Only values recognisable as JSON primitives (Map
, List
, Number
,
Boolean
, String
and null
) are supported.
other
- another value.NullPointerException
- if other
is null
.IllegalArgumentException
- if this or the other
value contains non-JSON primitive values.public JsonValue diff(JsonValue target)
target
value.target
- the intended target value.NullPointerException
- if either of original
or target
are null
.public void patch(JsonValue patch)
patch
- the JSON Patch value, specifying the modifications to apply to the original value.JsonValueException
- if application of the patch failed.public JsonValue merge(JsonValue other)
If there are any matching pointers in the two JSON values, then the value in other
overrides the value in this
.
other
- Another JSON value which should be merged with this one.other
.Copyright © 2010-2018, ForgeRock All Rights Reserved.