Class JsonPointer

java.lang.Object
com.flipkart.zjsonpatch.JsonPointer

public class JsonPointer extends Object
Implements RFC 6901 (JSON Pointer)

For full details, please refer to RFC 6901.

Generally, a JSON Pointer is a string representation of a path into a JSON document. This class implements the RFC as closely as possible, and offers several helpers and utility methods on top of it:
      // Parse, build or render a JSON pointer
      String path = "/a/0/b/1";
      JsonPointer ptr1 = JsonPointer.parse(java.lang.String)(path);
      JsonPointer ptr2 = JsonPointer.ROOT.append("a").append(0).append("b").append(1);
      assert(ptr1.equals(ptr2));
      assert(path.equals(ptr1.toString()));
      assert(path.equals(ptr2.toString()));

      // Evaluate a JSON pointer against a live document
      ObjectMapper om = new ObjectMapper();
      JsonNode doc = om.readTree("{\"foo\":[\"bar\", \"baz\"]}");
      JsonNode baz = JsonPointer.parse("/foo/1").evaluate(doc);
      assert(baz.textValue().equals("baz"));
 

Instances of JsonPointer and its constituent JsonPointer.RefTokens are immutable.

Since:
0.4.8
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final JsonPointer
    A JSON pointer representing the root node of a JSON document
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonPointer(List<com.flipkart.zjsonpatch.JsonPointer.RefToken> tokens)
    Constructs a new pointer from a list of reference tokens.
  • Method Summary

    Modifier and Type
    Method
    Description
    List<com.flipkart.zjsonpatch.JsonPointer.RefToken>
    Decomposes this JSON pointer into its reference tokens.
    boolean
     
    com.fasterxml.jackson.databind.JsonNode
    evaluate(com.fasterxml.jackson.databind.JsonNode document)
    Takes a target document and resolves the node represented by this instance.
    com.flipkart.zjsonpatch.JsonPointer.RefToken
    get(int index)
    Retrieves the reference token at the specified index.
    Creates a JSON pointer to the parent of the node represented by this instance.
    int
     
    boolean
    Indicates whether or not this instance points to the root of a JSON document.
    com.flipkart.zjsonpatch.JsonPointer.RefToken
    Retrieves the last reference token for this JSON pointer.
    parse(String path)
    Parses a valid string representation of a JSON Pointer.
    Returns a string representation of this instance

    Methods inherited from class java.lang.Object

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

    • ROOT

      public static final JsonPointer ROOT
      A JSON pointer representing the root node of a JSON document
  • Constructor Details

    • JsonPointer

      public JsonPointer(List<com.flipkart.zjsonpatch.JsonPointer.RefToken> tokens)
      Constructs a new pointer from a list of reference tokens.
      Parameters:
      tokens - The list of reference tokens from which to construct the new pointer. This list is not modified.
  • Method Details

    • parse

      public static JsonPointer parse(String path) throws IllegalArgumentException
      Parses a valid string representation of a JSON Pointer.
      Parameters:
      path - The string representation to be parsed.
      Returns:
      An instance of JsonPointer conforming to the specified string representation.
      Throws:
      IllegalArgumentException - The specified JSON Pointer is invalid.
    • isRoot

      public boolean isRoot()
      Indicates whether or not this instance points to the root of a JSON document.
      Returns:
      true if this pointer represents the root node, false otherwise.
    • toString

      public String toString()
      Returns a string representation of this instance
      Overrides:
      toString in class Object
      Returns:
      An RFC 6901 compliant string representation of this JSON pointer.
    • decompose

      public List<com.flipkart.zjsonpatch.JsonPointer.RefToken> decompose()
      Decomposes this JSON pointer into its reference tokens.
      Returns:
      A list of JsonPointer.RefTokens. Modifications to this list do not affect this instance.
    • get

      public com.flipkart.zjsonpatch.JsonPointer.RefToken get(int index) throws IndexOutOfBoundsException
      Retrieves the reference token at the specified index.
      Parameters:
      index - The desired reference token index.
      Returns:
      The specified instance of JsonPointer.RefToken.
      Throws:
      IndexOutOfBoundsException - The specified index is illegal.
    • last

      public com.flipkart.zjsonpatch.JsonPointer.RefToken last()
      Retrieves the last reference token for this JSON pointer.
      Returns:
      The last JsonPointer.RefToken comprising this instance.
      Throws:
      IllegalStateException - Last cannot be called on root pointers.
    • getParent

      public JsonPointer getParent()
      Creates a JSON pointer to the parent of the node represented by this instance. The parent of the root pointer is the root pointer itself.
      Returns:
      A JsonPointer to the parent node.
    • evaluate

      public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode document) throws JsonPointerEvaluationException
      Takes a target document and resolves the node represented by this instance. The evaluation semantics are described in RFC 6901 sectino 4.
      Parameters:
      document - The target document against which to evaluate the JSON pointer.
      Returns:
      The JsonNode resolved by evaluating this JSON pointer.
      Throws:
      JsonPointerEvaluationException - The pointer could not be evaluated.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object