Class JsonPointer
java.lang.Object
com.flipkart.zjsonpatch.AbstractJsonPointer
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 RefTokens are immutable.
- Since:
- 0.4.8
- Author:
- Mariusz Sondecki
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final JsonPointerA JSON pointer representing the root node of a JSON document -
Constructor Summary
ConstructorsConstructorDescriptionJsonPointer(List<RefToken> tokens) Constructs a new pointer from a list of reference tokens. -
Method Summary
Modifier and TypeMethodDescriptionprotected AbstractJsonPointercreateInstance(RefToken[] tokens) com.fasterxml.jackson.databind.JsonNodeevaluate(com.fasterxml.jackson.databind.JsonNode document) Takes a target document and resolves the node represented by this instance.Creates a JSON pointer to the parent of the node represented by this instance.static JsonPointerParses a valid string representation of a JSON Pointer.Methods inherited from class com.flipkart.zjsonpatch.AbstractJsonPointer
decompose, equals, get, getTokens, hashCode, isRoot, last, parseTokens, toString
-
Field Details
-
ROOT
A JSON pointer representing the root node of a JSON document
-
-
Constructor Details
-
JsonPointer
-
-
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.
-
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 section 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.
-
createInstance
- Specified by:
createInstancein classAbstractJsonPointer
-