Class JsonPointerImpl
- java.lang.Object
-
- org.glassfish.json.JsonPointerImpl
-
- All Implemented Interfaces:
java.io.Serializable
,JsonPointer
public final class JsonPointerImpl extends java.lang.Object implements JsonPointer, java.io.Serializable
This class is an immutable representation of a JSON Pointer as specified in RFC 6901.
A JSON Pointer, when applied to a target
JsonValue
, defines a reference location in the target.An empty JSON Pointer string defines a reference to the target itself.
If the JSON Pointer string is non-empty, it must be a sequence of '/' prefixed tokens, and the target must either be a
JsonArray
orJsonObject
. If the target is aJsonArray
, the pointer defines a reference to an array element, and the last token specifies the index. If the target is aJsonObject
, the pointer defines a reference to a name/value pair, and the last token specifies the name.The method
getValue()
returns the referenced value. The methodsadd()
,replace()
, andremove()
executes the operations specified in RFC 6902.- Since:
- 1.1
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
jsonPointer
private static long
serialVersionUID
private java.lang.String[]
tokens
-
Constructor Summary
Constructors Constructor Description JsonPointerImpl(java.lang.String jsonPointer)
Constructs and initializes a JsonPointerImpl.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonStructure
add(JsonStructure target, JsonValue value)
Adds or replaces a value at the referenced location in the specifiedtarget
with the specifiedvalue
.boolean
containsValue(JsonStructure target)
Returnstrue
if there is a value at the referenced location in the specifiedtarget
.boolean
equals(java.lang.Object obj)
Compares thisJsonPointer
with another object.private JsonStructure
execute(java.util.function.BiFunction<NodeReference,JsonValue,JsonStructure> op, JsonStructure target, JsonValue value)
Executes the operationprivate static int
getIndex(java.lang.String token)
Computes the array indexprivate NodeReference[]
getReferences(JsonStructure target)
Computes theNodeReference
s for each node on the path of the JSON Pointer, in reverse order, starting from the leaf nodeJsonValue
getValue(JsonStructure target)
Returns the value at the referenced location in the specifiedtarget
int
hashCode()
Returns the hash code value for thisJsonPointer
object.JsonStructure
remove(JsonStructure target)
Removes the value at the reference location in the specifiedtarget
JsonStructure
replace(JsonStructure target, JsonValue value)
Replaces the value at the referenced location in the specifiedtarget
with the specifiedvalue
.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
tokens
private final java.lang.String[] tokens
-
jsonPointer
private final java.lang.String jsonPointer
-
-
Constructor Detail
-
JsonPointerImpl
public JsonPointerImpl(java.lang.String jsonPointer)
Constructs and initializes a JsonPointerImpl.- Parameters:
jsonPointer
- the JSON Pointer string- Throws:
java.lang.NullPointerException
- ifjsonPointer
isnull
JsonException
- ifjsonPointer
is not a valid JSON Pointer
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object obj)
Compares thisJsonPointer
with another object.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- the object to compare thisJsonPointer
against- Returns:
- true if the given object is a
JsonPointer
with the same reference tokens as this one, false otherwise.
-
hashCode
public int hashCode()
Returns the hash code value for thisJsonPointer
object. The hash code of this object is defined by the hash codes of it's reference tokens.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code value for this
JsonPointer
object
-
containsValue
public boolean containsValue(JsonStructure target)
Returnstrue
if there is a value at the referenced location in the specifiedtarget
.- Specified by:
containsValue
in interfaceJsonPointer
- Parameters:
target
- the target referenced by thisJsonPointer
- Returns:
true
if this pointer points to a value in a specifiedtarget
.
-
getValue
public JsonValue getValue(JsonStructure target)
Returns the value at the referenced location in the specifiedtarget
- Specified by:
getValue
in interfaceJsonPointer
- Parameters:
target
- the target referenced by thisJsonPointer
- Returns:
- the referenced value in the target.
- Throws:
java.lang.NullPointerException
- iftarget
is nullJsonException
- if the referenced value does not exist
-
add
public JsonStructure add(JsonStructure target, JsonValue value)
Adds or replaces a value at the referenced location in the specifiedtarget
with the specifiedvalue
.- If the reference is the target (empty JSON Pointer string),
the specified
value
, which must be the same type as specifiedtarget
, is returned. - If the reference is an array element, the specified
value
is inserted into the array, at the referenced index. The value currently at that location, and any subsequent values, are shifted to the right (adds one to the indices). Index starts with 0. If the reference is specified with a "-", or if the index is equal to the size of the array, the value is appended to the array. - If the reference is a name/value pair of a
JsonObject
, and the referenced value exists, the value is replaced by the specifiedvalue
. If the value does not exist, a new name/value pair is added to the object.
- Specified by:
add
in interfaceJsonPointer
- Parameters:
target
- the target referenced by thisJsonPointer
value
- the value to be added- Returns:
- the transformed
target
after the value is added. - Throws:
java.lang.NullPointerException
- iftarget
isnull
JsonException
- if the reference is an array element and the index is out of range (index < 0 || index > array size
), or if the pointer contains references to non-existing objects or arrays.
- If the reference is the target (empty JSON Pointer string),
the specified
-
replace
public JsonStructure replace(JsonStructure target, JsonValue value)
Replaces the value at the referenced location in the specifiedtarget
with the specifiedvalue
.- Specified by:
replace
in interfaceJsonPointer
- Parameters:
target
- the target referenced by thisJsonPointer
value
- the value to be stored at the referenced location- Returns:
- the transformed
target
after the value is replaced. - Throws:
java.lang.NullPointerException
- iftarget
isnull
JsonException
- if the referenced value does not exist, or if the reference is the target.
-
remove
public JsonStructure remove(JsonStructure target)
Removes the value at the reference location in the specifiedtarget
- Specified by:
remove
in interfaceJsonPointer
- Parameters:
target
- the target referenced by thisJsonPointer
- Returns:
- the transformed
target
after the value is removed. - Throws:
java.lang.NullPointerException
- iftarget
isnull
JsonException
- if the referenced value does not exist, or if the reference is the target.
-
execute
private JsonStructure execute(java.util.function.BiFunction<NodeReference,JsonValue,JsonStructure> op, JsonStructure target, JsonValue value)
Executes the operation- Parameters:
op
- a {code BiFunction} used to specify the operation to execute on the leaf node of the Json Pointertarget
- the target JsonStructure for this JsonPointervalue
- the JsonValue for add and replace, can be null for getvalue and remove
-
getReferences
private NodeReference[] getReferences(JsonStructure target)
Computes theNodeReference
s for each node on the path of the JSON Pointer, in reverse order, starting from the leaf node
-
getIndex
private static int getIndex(java.lang.String token)
Computes the array index- Parameters:
token
- the input string token- Returns:
- the array index. -1 if the token is "-"
- Throws:
JsonException
- if the string token is not in correct format
-
-