Class JsonPointer


public final class JsonPointer extends AbstractJsonPointer
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 RefTokens are immutable.

Since:
0.4.8
Author:
Mariusz Sondecki
  • 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<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.
    • 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 section 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.
    • createInstance

      protected AbstractJsonPointer createInstance(RefToken[] tokens)
      Specified by:
      createInstance in class AbstractJsonPointer