Class JsonPointerImpl

java.lang.Object
org.eclipse.parsson.JsonPointerImpl
All Implemented Interfaces:
jakarta.json.JsonPointer, Serializable

public final class JsonPointerImpl extends Object implements jakarta.json.JsonPointer, 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 or JsonObject. If the target is a JsonArray, the pointer defines a reference to an array element, and the last token specifies the index. If the target is a JsonObject, 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 methods add(), replace(), and remove() executes the operations specified in RFC 6902.

Since:
1.1
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final JsonContext
     
    private final String
     
    private static final long
     
    private final String[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonPointerImpl(String jsonPointer, JsonContext jsonContext)
    Constructs and initializes a JsonPointerImpl.
  • Method Summary

    Modifier and Type
    Method
    Description
    jakarta.json.JsonStructure
    add(jakarta.json.JsonStructure target, jakarta.json.JsonValue value)
    Adds or replaces a value at the referenced location in the specified target with the specified value.
    boolean
    containsValue(jakarta.json.JsonStructure target)
    Returns true if there is a value at the referenced location in the specified target.
    boolean
    Compares this JsonPointer with another object.
    private jakarta.json.JsonStructure
    execute(BiFunction<NodeReference,jakarta.json.JsonValue,jakarta.json.JsonStructure> op, jakarta.json.JsonStructure target, jakarta.json.JsonValue value)
    Executes the operation
    private static int
    Computes the array index
    private NodeReference[]
    getReferences(jakarta.json.JsonStructure target)
    Computes the NodeReferences for each node on the path of the JSON Pointer, in reverse order, starting from the leaf node
    jakarta.json.JsonValue
    getValue(jakarta.json.JsonStructure target)
    Returns the value at the referenced location in the specified target
    int
    Returns the hash code value for this JsonPointer object.
    jakarta.json.JsonStructure
    remove(jakarta.json.JsonStructure target)
    Removes the value at the reference location in the specified target
    jakarta.json.JsonStructure
    replace(jakarta.json.JsonStructure target, jakarta.json.JsonValue value)
    Replaces the value at the referenced location in the specified target with the specified value.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • tokens

      private final String[] tokens
    • jsonPointer

      private final String jsonPointer
    • jsonContext

      private final JsonContext jsonContext
  • Constructor Details

    • JsonPointerImpl

      public JsonPointerImpl(String jsonPointer, JsonContext jsonContext)
      Constructs and initializes a JsonPointerImpl.
      Parameters:
      jsonPointer - the JSON Pointer string
      Throws:
      NullPointerException - if jsonPointer is null
      jakarta.json.JsonException - if jsonPointer is not a valid JSON Pointer
  • Method Details

    • equals

      public boolean equals(Object obj)
      Compares this JsonPointer with another object.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare this JsonPointer 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 this JsonPointer object. The hash code of this object is defined by the hash codes of it's reference tokens.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this JsonPointer object
    • containsValue

      public boolean containsValue(jakarta.json.JsonStructure target)
      Returns true if there is a value at the referenced location in the specified target.
      Specified by:
      containsValue in interface jakarta.json.JsonPointer
      Parameters:
      target - the target referenced by this JsonPointer
      Returns:
      true if this pointer points to a value in a specified target.
    • getValue

      public jakarta.json.JsonValue getValue(jakarta.json.JsonStructure target)
      Returns the value at the referenced location in the specified target
      Specified by:
      getValue in interface jakarta.json.JsonPointer
      Parameters:
      target - the target referenced by this JsonPointer
      Returns:
      the referenced value in the target.
      Throws:
      NullPointerException - if target is null
      jakarta.json.JsonException - if the referenced value does not exist
    • add

      public jakarta.json.JsonStructure add(jakarta.json.JsonStructure target, jakarta.json.JsonValue value)
      Adds or replaces a value at the referenced location in the specified target with the specified value.
      1. If the reference is the target (empty JSON Pointer string), the specified value, which must be the same type as specified target, is returned.
      2. 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.
      3. If the reference is a name/value pair of a JsonObject, and the referenced value exists, the value is replaced by the specified value. If the value does not exist, a new name/value pair is added to the object.
      Specified by:
      add in interface jakarta.json.JsonPointer
      Parameters:
      target - the target referenced by this JsonPointer
      value - the value to be added
      Returns:
      the transformed target after the value is added.
      Throws:
      NullPointerException - if target is null
      jakarta.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.
    • replace

      public jakarta.json.JsonStructure replace(jakarta.json.JsonStructure target, jakarta.json.JsonValue value)
      Replaces the value at the referenced location in the specified target with the specified value.
      Specified by:
      replace in interface jakarta.json.JsonPointer
      Parameters:
      target - the target referenced by this JsonPointer
      value - the value to be stored at the referenced location
      Returns:
      the transformed target after the value is replaced.
      Throws:
      NullPointerException - if target is null
      jakarta.json.JsonException - if the referenced value does not exist, or if the reference is the target.
    • remove

      public jakarta.json.JsonStructure remove(jakarta.json.JsonStructure target)
      Removes the value at the reference location in the specified target
      Specified by:
      remove in interface jakarta.json.JsonPointer
      Parameters:
      target - the target referenced by this JsonPointer
      Returns:
      the transformed target after the value is removed.
      Throws:
      NullPointerException - if target is null
      jakarta.json.JsonException - if the referenced value does not exist, or if the reference is the target.
    • toString

      public String toString()
      Specified by:
      toString in interface jakarta.json.JsonPointer
      Overrides:
      toString in class Object
    • execute

      private jakarta.json.JsonStructure execute(BiFunction<NodeReference,jakarta.json.JsonValue,jakarta.json.JsonStructure> op, jakarta.json.JsonStructure target, jakarta.json.JsonValue value)
      Executes the operation
      Parameters:
      op - a {code BiFunction} used to specify the operation to execute on the leaf node of the Json Pointer
      target - the target JsonStructure for this JsonPointer
      value - the JsonValue for add and replace, can be null for getvalue and remove
    • getReferences

      private NodeReference[] getReferences(jakarta.json.JsonStructure target)
      Computes the NodeReferences for each node on the path of the JSON Pointer, in reverse order, starting from the leaf node
    • getIndex

      private static int getIndex(String token)
      Computes the array index
      Parameters:
      token - the input string token
      Returns:
      the array index. -1 if the token is "-"
      Throws:
      jakarta.json.JsonException - if the string token is not in correct format