Package org.apache.johnzon.core
Class JsonPointerImpl
- java.lang.Object
-
- org.apache.johnzon.core.JsonPointerImpl
-
- All Implemented Interfaces:
javax.json.JsonPointer
public class JsonPointerImpl extends java.lang.Object implements javax.json.JsonPointer
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.regex.Pattern
IS_NUMBER
private java.lang.String
jsonPointer
private javax.json.spi.JsonProvider
provider
private java.util.List<java.lang.String>
referenceTokens
-
Constructor Summary
Constructors Constructor Description JsonPointerImpl(javax.json.spi.JsonProvider provider, java.lang.String jsonPointer)
Constructs and initializes a JsonPointer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description javax.json.JsonArray
add(javax.json.JsonArray target, javax.json.JsonValue value)
Adds or replaces a value at the referenced location in the specifiedtarget
with the specifiedvalue
.javax.json.JsonObject
add(javax.json.JsonObject target, javax.json.JsonValue value)
Adds or replaces a value at the referenced location in the specifiedtarget
with the specifiedvalue
.javax.json.JsonStructure
add(javax.json.JsonStructure target, javax.json.JsonValue value)
Adds or replaces a value at the referenced location in the specifiedtarget
with the specifiedvalue
.private javax.json.JsonValue
addInternal(javax.json.JsonValue jsonValue, javax.json.JsonValue newValue, java.util.List<java.lang.String> currentPath, boolean check)
private <T extends javax.json.JsonStructure>
TaddInternal(T jsonValue, javax.json.JsonValue newValue)
private boolean
canMatch(java.util.List<java.lang.String> currentPath)
boolean
containsValue(javax.json.JsonStructure target)
boolean
equals(java.lang.Object obj)
Compares thisJsonPointer
with another object.private int
getArrayIndex(java.lang.String referenceToken, javax.json.JsonArray jsonArray, boolean addOperation)
(package private) java.lang.String
getJsonPointer()
javax.json.JsonValue
getValue(javax.json.JsonStructure target)
Returns the value at the referenced location in the specifiedtarget
private javax.json.JsonValue
getValue(javax.json.JsonValue jsonValue, java.lang.String referenceToken, int currentPosition, int referencePosition)
int
hashCode()
Returns the hash code value for thisJsonPointer
object.private boolean
isEmptyJsonPointer()
private boolean
isPositionToAdd(java.util.List<java.lang.String> currentPath)
protected int
minusShift()
This method can be overridden in sub classes.javax.json.JsonArray
remove(javax.json.JsonArray target)
Removes the value at the reference location in the specifiedtarget
javax.json.JsonObject
remove(javax.json.JsonObject target)
Removes the value at the reference location in the specifiedtarget
javax.json.JsonStructure
remove(javax.json.JsonStructure target)
Removes the value at the reference location in the specifiedtarget
private javax.json.JsonValue
remove(javax.json.JsonValue jsonValue, int currentPosition)
javax.json.JsonArray
replace(javax.json.JsonArray target, javax.json.JsonValue value)
Replaces the value at the referenced location in the specifiedjavax.json.JsonObject
replace(javax.json.JsonObject target, javax.json.JsonValue value)
Replaces the value at the referenced location in the specifiedjavax.json.JsonStructure
replace(javax.json.JsonStructure target, javax.json.JsonValue value)
Replaces the value at the referenced location in the specifiedtarget
with the specifiedvalue
.java.lang.String
toString()
private void
validateAdd(javax.json.JsonValue target)
private void
validateArrayIndex(java.lang.String referenceToken)
private void
validateArraySize(java.lang.String referenceToken, javax.json.JsonArray jsonArray, int arrayIndex, int arraySize)
private void
validateJsonPointer(javax.json.JsonValue target, int size)
private void
validateRemove(javax.json.JsonValue target)
-
-
-
Constructor Detail
-
JsonPointerImpl
public JsonPointerImpl(javax.json.spi.JsonProvider provider, java.lang.String jsonPointer)
Constructs and initializes a JsonPointer.- Parameters:
provider
- the JSON provider used to create this pointerjsonPointer
- the JSON Pointer string- Throws:
java.lang.NullPointerException
- ifjsonPointer
isnull
javax.json.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
-
getValue
public javax.json.JsonValue getValue(javax.json.JsonStructure target)
Returns the value at the referenced location in the specifiedtarget
- Specified by:
getValue
in interfacejavax.json.JsonPointer
- Parameters:
target
- the target referenced by thisJsonPointer
- Returns:
- the referenced value in the target.
- Throws:
java.lang.NullPointerException
- iftarget
is nulljavax.json.JsonException
- if the referenced value does not exist
-
containsValue
public boolean containsValue(javax.json.JsonStructure target)
- Specified by:
containsValue
in interfacejavax.json.JsonPointer
-
add
public javax.json.JsonStructure add(javax.json.JsonStructure target, javax.json.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 interfacejavax.json.JsonPointer
- 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
javax.json.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
-
add
public javax.json.JsonObject add(javax.json.JsonObject target, javax.json.JsonValue value)
Adds or replaces a value at the referenced location in the specifiedtarget
with the specifiedvalue
.- 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
javax.json.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.- See Also:
add(JsonStructure, JsonValue)
-
add
public javax.json.JsonArray add(javax.json.JsonArray target, javax.json.JsonValue value)
Adds or replaces a value at the referenced location in the specifiedtarget
with the specifiedvalue
.- 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
javax.json.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.- See Also:
add(JsonStructure, JsonValue)
-
replace
public javax.json.JsonStructure replace(javax.json.JsonStructure target, javax.json.JsonValue value)
Replaces the value at the referenced location in the specifiedtarget
with the specifiedvalue
.- Specified by:
replace
in interfacejavax.json.JsonPointer
- 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
javax.json.JsonException
- if the referenced value does not exist, or if the reference is the target.
-
replace
public javax.json.JsonObject replace(javax.json.JsonObject target, javax.json.JsonValue value)
Replaces the value at the referenced location in the specified- 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
javax.json.JsonException
- if the referenced value does not exist, or if the reference is the target.- See Also:
replace(JsonStructure, JsonValue)
-
replace
public javax.json.JsonArray replace(javax.json.JsonArray target, javax.json.JsonValue value)
Replaces the value at the referenced location in the specified- 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
javax.json.JsonException
- if the referenced value does not exist, or if the reference is the target.- See Also:
replace(JsonStructure, JsonValue)
-
remove
public javax.json.JsonStructure remove(javax.json.JsonStructure target)
Removes the value at the reference location in the specifiedtarget
- Specified by:
remove
in interfacejavax.json.JsonPointer
- Parameters:
target
- the target referenced by thisJsonPointer
- Returns:
- the transformed
target
after the value is removed. - Throws:
java.lang.NullPointerException
- iftarget
isnull
javax.json.JsonException
- if the referenced value does not exist, or if the reference is the target.
-
remove
public javax.json.JsonObject remove(javax.json.JsonObject target)
Removes the value at the reference location in the specifiedtarget
- Parameters:
target
- the target referenced by thisJsonPointer
- Returns:
- the transformed
target
after the value is removed. - Throws:
java.lang.NullPointerException
- iftarget
isnull
javax.json.JsonException
- if the referenced value does not exist, or if the reference is the target.- See Also:
remove(JsonStructure)
-
remove
public javax.json.JsonArray remove(javax.json.JsonArray target)
Removes the value at the reference location in the specifiedtarget
- Parameters:
target
- the target referenced by thisJsonPointer
- Returns:
- the transformed
target
after the value is removed. - Throws:
java.lang.NullPointerException
- iftarget
isnull
javax.json.JsonException
- if the referenced value does not exist, or if the reference is the target.- See Also:
remove(JsonStructure)
-
getJsonPointer
java.lang.String getJsonPointer()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
validateAdd
private void validateAdd(javax.json.JsonValue target)
-
validateRemove
private void validateRemove(javax.json.JsonValue target)
-
isEmptyJsonPointer
private boolean isEmptyJsonPointer()
-
getValue
private javax.json.JsonValue getValue(javax.json.JsonValue jsonValue, java.lang.String referenceToken, int currentPosition, int referencePosition)
-
addInternal
private <T extends javax.json.JsonStructure> T addInternal(T jsonValue, javax.json.JsonValue newValue)
-
addInternal
private javax.json.JsonValue addInternal(javax.json.JsonValue jsonValue, javax.json.JsonValue newValue, java.util.List<java.lang.String> currentPath, boolean check)
-
isPositionToAdd
private boolean isPositionToAdd(java.util.List<java.lang.String> currentPath)
-
canMatch
private boolean canMatch(java.util.List<java.lang.String> currentPath)
-
remove
private javax.json.JsonValue remove(javax.json.JsonValue jsonValue, int currentPosition)
-
getArrayIndex
private int getArrayIndex(java.lang.String referenceToken, javax.json.JsonArray jsonArray, boolean addOperation)
-
minusShift
protected int minusShift()
This method can be overridden in sub classes. It's main goal is to support patch operation using "-" to replace, remove last element which is forbidden in JsonPointer- Returns:
- the shift to apply on minus. For backward compatibility it's 1. We can have a strict JSONP compliancy with the dedicated module.
-
validateJsonPointer
private void validateJsonPointer(javax.json.JsonValue target, int size) throws java.lang.NullPointerException, javax.json.JsonException
- Throws:
java.lang.NullPointerException
javax.json.JsonException
-
validateArrayIndex
private void validateArrayIndex(java.lang.String referenceToken) throws javax.json.JsonException
- Throws:
javax.json.JsonException
-
validateArraySize
private void validateArraySize(java.lang.String referenceToken, javax.json.JsonArray jsonArray, int arrayIndex, int arraySize) throws javax.json.JsonException
- Throws:
javax.json.JsonException
-
-