public class JsonValue extends Object implements Cloneable, Iterable<JsonValue>
String
, Number
, Map
, List
,
Boolean
and null
.
A JSON value may have one or more transformers associated with it. Transformers apply transformations to the JSON value upon construction, and upon members as they are retrieved. Transformers are applied iteratively, in the sequence they appear within the list. If a transformer affects the value, then all transformers are re-applied, in sequence. This repeats until the value is no longer affected. Transformers are inherited by and applied to member values.
Constructor and Description |
---|
JsonValue(Object object)
Constructs a JSON value object with a given object.
|
JsonValue(Object object,
Collection<? extends JsonTransformer> transformers)
Constructs a JSON value object with a given object and transformers.
|
JsonValue(Object object,
JsonPointer pointer)
Constructs a JSON value object with a given object and pointer.
|
JsonValue(Object object,
JsonPointer pointer,
Collection<? extends JsonTransformer> transformers)
Constructs a JSON value object with given object, pointer and transformers.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
Object object)
Adds the specified value to the list.
|
JsonValue |
add(String key,
Object object)
Adds the specified value.
|
void |
applyTransformers()
Applies all of the transformations to the value.
|
Boolean |
asBoolean()
Returns the JSON value as a
Boolean object. |
Charset |
asCharset()
Returns the JSON string value as a character set used for byte encoding/decoding.
|
Double |
asDouble()
Returns the JSON value as a
Double object. |
<T extends Enum<T>> |
asEnum(Class<T> type)
Returns the JSON string value as an enum constant of the specified enum type.
|
File |
asFile()
Returns the JSON string value as a
File 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. |
Number |
asNumber()
Returns the JSON value as a
Number object. |
Pattern |
asPattern()
Returns the JSON string value as a regular expression pattern.
|
JsonPointer |
asPointer()
Returns the JSON string value as a JSON pointer.
|
String |
asString()
Returns the JSON value as a
String object. |
URI |
asURI()
Returns the JSON string value as a uniform resource identifier.
|
UUID |
asUUID()
Returns the JSON string value as a universally unique identifier (UUID).
|
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 |
expect(Class<?> type)
Called to enforce that the JSON value is of a particular type.
|
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.
|
List<JsonTransformer> |
getTransformers()
Returns the JSON value's list of transformers.
|
Object |
getWrappedObject()
Returns a Java object representing this JSON value.
|
boolean |
isBoolean()
Returns
true if the JSON value is a Boolean . |
boolean |
isDefined(String key)
Returns
true if this JSON value contains the specified item. |
boolean |
isList()
Returns
true if the JSON value is a List . |
boolean |
isMap()
Returns
true if the JSON value is a Map . |
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.
|
Set<String> |
keys()
Returns the set of keys for this JSON value's child values.
|
void |
put(int index,
Object object)
Sets the value of the specified child list element.
|
void |
put(JsonPointer pointer,
Object object)
Sets the value of the value identified by the specified pointer, relative to this value
as root.
|
void |
put(String key,
Object object)
Sets the value of the specified member.
|
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.
|
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.
|
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, JsonPointer pointer, Collection<? extends JsonTransformer> transformers) throws JsonException
JsonValueWrapper
and/or
JsonValue
objects. The pointer is inherited from the wrapped value, except
if pointer
is not null
. The transformers are inherited from the
wrapped value, except if transformers
is not null
.object
- the Java object representing the JSON value.pointer
- the pointer to the value in a JSON structure.transformers
- a list of transformers to apply the value and its members.JsonException
- if a transformer failed during value initialization.public JsonValue(Object object, Collection<? extends JsonTransformer> transformers)
JsonValueWrapper
and/or
JsonValue
objects.object
- the Java object representing the JSON value.transformers
- a list of transformers to apply the value and its members.JsonException
- if a transformer failed during value initialization.public JsonValue(Object object, JsonPointer pointer)
JsonValueWrapper
and/or
JsonValue
objects.object
- the Java object representing the JSON value.pointer
- the pointer to the value in a JSON structure.public JsonValue(Object object)
JsonValueWrapper
and/or
JsonValue
objects.object
- the Java object representing JSON value.public Object getObject()
public Object getWrappedObject()
Map
or
List
, it is wrapped with a JsonValueMap
or JsonValueList
object respectively. This maintains and applies transformations as these objects
(and their children) are accessed.public void setObject(Object object)
This method will automatically unwrap any JsonValueWrapper
and/or
JsonValue
objects. Transformers are inherited from the wrapped value.
This value's pointer remains unaffected.
object
- the object to set.public JsonPointer getPointer()
public List<JsonTransformer> getTransformers()
applyTransformers()
method to apply them to the current value.public void applyTransformers() throws JsonException
This method has an absurdly high upper-limit of Integer.MAX_VALUE
iterations,
beyond which a JsonException
will be thrown.
JsonException
- if there was a failure applying transformation(s)public JsonValue required() throws JsonValueException
JsonValueException
if the JSON value is null
.JsonValueException
- if the JSON value is null
.public JsonValue expect(Class<?> type) throws JsonValueException
null
is allowed.type
- the class that the underlying value must have.JsonValueException
- if the value is not the specified type.public boolean isMap()
true
if the JSON value is a Map
.public Map<String,Object> asMap() throws JsonValueException
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 boolean isList()
true
if the JSON value is a List
.public List<Object> asList() throws JsonValueException
JsonValueException
- if the JSON value is not a List
.public <E> List<E> asList(Class<E> type) throws JsonValueException
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.type
- the type of object that all elements are expected to be.JsonVaueException
- if the JSON value is not a List
or contains an unexpected type.NullPointerException
- if type
is null
.JsonValueException
public boolean isString()
true
if the JSON value is a String
.public String asString() throws JsonValueException
String
object. If the JSON value is null
,
this method returns null
.JsonValueException
- if the JSON value is not a string.public boolean isNumber()
true
if the JSON value is a Number
.public Number asNumber() throws JsonValueException
Number
object. If the JSON value is null
,
this method returns null
.JsonValueException
- if the JSON value is not a number.public Integer asInteger() throws JsonValueException
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 Double asDouble() throws JsonValueException
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 Long asLong() throws JsonValueException
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 boolean isBoolean()
true
if the JSON value is a Boolean
.public Boolean asBoolean() throws JsonValueException
JsonValueException
- if the JSON value is not a boolean type.public boolean isNull()
true
if the value is null
.public <T extends Enum<T>> T asEnum(Class<T> type) throws JsonValueException
null
, this method returns null
.type
- the enum type to match constants with the value.IllegalArgumentException
- if type
does not represent an enum type.JsonValueException
- if the JSON value does not match any of the enum's constants.NullPointerException
- if type
is null
.public File asFile() throws JsonValueException
File
object. If the JSON value is
null
, this method returns null
.JsonValueException
- if the JSON value is not a string.public Charset asCharset() throws JsonValueException
null
, this method returns null
.JsonValueException
- if the JSON value is not a string or the character set specified is invalid.public Pattern asPattern() throws JsonValueException
null
, this method returns null
.JsonValueException
- if the pattern is not a string or the value is not a valid regular expression pattern.public URI asURI() throws JsonValueException
null
, this method returns null
.JsonValueException
- if the given string violates URI syntax.public JsonPointer asPointer() throws JsonValueException
null
,
this method returns null
.JsonValueException
- if the JSON value is not a string or valid JSON pointer.public UUID asUUID() throws JsonValueException
null
, this method returns null
.JsonValueException
- if the JSON value is not a string or valid UUID.public JsonValue defaultTo(Object object)
null
.object
- the object to default to.public int size()
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 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 get(String key) throws JsonException
null
is returned.key
- the Map
key or List
index identifying the item to return.null
.JsonException
- if a transformer failed to transform the child value.public JsonValue get(int index) throws JsonException
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.JsonException
- if a transformer failed to transform the child value.public JsonValue get(JsonPointer pointer) throws JsonException
null
is returned.pointer
- the JSON pointer identifying the child value to return.null
if no such value exists.JsonException
- if a transformer failed to transform the resulting value.public void put(String key, Object object) throws JsonValueException
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 void put(int index, Object object) throws JsonValueException
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 void put(JsonPointer pointer, Object object) throws JsonValueException
JsonValueException
will be thrown.pointer
- identifies the child value to set.object
- the Java object value to set.JsonValueException
- if the specified pointer is invalid.public void remove(String key)
key
- the Map
key or List
index identifying the child value to remove.public void remove(int index)
List
, calling this method has no effect.index
- the List
index identifying the child value to remove.public void clear() throws JsonValueException
JsonValueException
public JsonValue add(String key, Object object) throws JsonValueException
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 void add(int index, Object object) throws JsonValueException
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 Set<String> keys()
Map
, then the order of the resulting keys is undefined. If there are no child
values, this method returns an empty set.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
.
Note: calls to the next()
method may throw the runtime JsonException
if any transformers fail to execute.
public JsonValue copy()
This method applies all transformations while traversing the values's members and their members, and so on. Consequently, the returned copy does not include the transformers from this 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.
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 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.
Copyright © 2025 Open Identity Platform Community. All rights reserved.