Package com.flipkart.zjsonpatch
Class JsonPointer
java.lang.Object
com.flipkart.zjsonpatch.JsonPointer
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
FieldsModifier and TypeFieldDescriptionstatic final JsonPointerA JSON pointer representing the root node of a JSON document -
Constructor Summary
ConstructorsConstructorDescriptionJsonPointer(List<com.flipkart.zjsonpatch.JsonPointer.RefToken> tokens) Constructs a new pointer from a list of reference tokens. -
Method Summary
Modifier and TypeMethodDescriptionList<com.flipkart.zjsonpatch.JsonPointer.RefToken>Decomposes this JSON pointer into its reference tokens.booleancom.fasterxml.jackson.databind.JsonNodeevaluate(com.fasterxml.jackson.databind.JsonNode document) Takes a target document and resolves the node represented by this instance.com.flipkart.zjsonpatch.JsonPointer.RefTokenget(int index) Retrieves the reference token at the specified index.Creates a JSON pointer to the parent of the node represented by this instance.inthashCode()booleanisRoot()Indicates whether or not this instance points to the root of a JSON document.com.flipkart.zjsonpatch.JsonPointer.RefTokenlast()Retrieves the last reference token for this JSON pointer.static JsonPointerParses a valid string representation of a JSON Pointer.toString()Returns a string representation of this instance
-
Field Details
-
ROOT
A JSON pointer representing the root node of a JSON document
-
-
Constructor Details
-
JsonPointer
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
Parses a valid string representation of a JSON Pointer.- Parameters:
path- The string representation to be parsed.- Returns:
- An instance of
JsonPointerconforming 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:
trueif this pointer represents the root node,falseotherwise.
-
toString
Returns a string representation of this instance- Overrides:
toStringin classObject- Returns:
- An RFC 6901 compliant string representation of this JSON pointer.
-
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
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.RefTokencomprising this instance. - Throws:
IllegalStateException- Last cannot be called onrootpointers.
-
getParent
Creates a JSON pointer to the parent of the node represented by this instance. The parent of therootpointer is the root pointer itself.- Returns:
- A
JsonPointerto 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
JsonNoderesolved by evaluating this JSON pointer. - Throws:
JsonPointerEvaluationException- The pointer could not be evaluated.
-
equals
-
hashCode
public int hashCode()
-