Class JsonPointer
- All Implemented Interfaces:
Serializable
TreeNode.at(com.fasterxml.jackson.core.JsonPointer)).
It may be used in future for filtering of streaming JSON content
as well (not implemented yet for 2.3).
Note that the implementation was largely rewritten for Jackson 2.14 to reduce memory usage by sharing backing "full path" representation for nested instances.
Instances are fully immutable and can be cached, shared between threads.
- Since:
- 2.3
- Author:
- Tatu Saloranta
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final StringWe will retain representation of the pointer, as a String, so thattoString()should be as efficient as possible.protected final intprotected intLazily-calculated hash code: need to retain hash code now that we can no longer rely on_asStringbeing the exact full representation (it is often "more", including parent path).protected JsonPointerReference from currently matching segment (if any) to node before leaf.protected final intprotected final Stringprotected final JsonPointerReference to rest of the pointer beyond currently matching segment (if any); null if this pointer refers to the matching segment.protected static final JsonPointerMarker instance used to represent segment that matches current node or position (that is, returns true formatches()).static final charEscape character '~' per RFC6901.static final Stringstatic final StringEscaped tilde string "~0" per RFC6901.static final charCharacter used to separate segments. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor used for creating "empty" instance, used to represent state that matches current node.protectedJsonPointer(String fullString, int fullStringOffset, String segment, int matchIndex, JsonPointer next) protectedJsonPointer(String fullString, int fullStringOffset, String segment, JsonPointer next) -
Method Summary
Modifier and TypeMethodDescriptionprotected JsonPointerprotected JsonPointer_constructHead(int suffixLength, JsonPointer last) protected static int_extractEscapedSegment(String input, int firstCharOffset, int i, StringBuilder sb) Method called to extract the next segment of the path, in case where we seem to have encountered a (tilde-)escaped character within segment.protected static JsonPointer_parseTail(String fullPath) append(JsonPointer tail) Mutant factory method that will return `tail` if `this` instance is "empty" pointer, OR `this` instance if `tail` is "empty" pointer, OR Newly constructedJsonPointerinstance that starts with all segments of `this`, followed by all segments of `tail`.appendIndex(int index) ATTENTION!appendProperty(String property) ATTENTION!static JsonPointerFactory method that parses given input and construct matching pointer instance, if it represents a valid JSON Pointer: if not, aIllegalArgumentExceptionis thrown.static JsonPointerempty()Accessor for an "empty" expression, that is, one you can get by callingcompile(java.lang.String)with "" (empty String).booleanstatic JsonPointerforPath(JsonStreamContext context, boolean includeRoot) Factory method that will construct a pointer instance that describes path to location givenJsonStreamContextpoints to.intinthashCode()head()Accessor for getting a pointer instance that is identical to this instance except that the last segment has been dropped.last()intlength()Functionally same as:toString().length()but more efficient as it avoids likely String allocation.matchElement(int index) Method that may be called to check whether the pointer head (first segment) matches specified Array index and if so, returnJsonPointerthat represents rest of the path after match.booleanmatches()booleanmatchesElement(int index) Method that may be called to see if the pointer would match Array element (of a JSON Array) with given index.booleanmatchesProperty(String name) Method that may be called to see if the pointer head (first segment) would match property (of a JSON Object) with given name.matchProperty(String name) Method that may be called to check whether the pointer head (first segment) matches specified Object property (by name) and if so, returnJsonPointerthat represents rest of the path after match.booleanbooleantail()Accessor for getting a "sub-pointer" (or sub-path), instance where current segment has been removed and pointer includes rest of the segments.toString()protected StringBuildertoStringBuilder(int slack) Functionally equivalent to:static JsonPointerAlias forcompile(java.lang.String); added to make instances automatically deserializable by Jackson databind.
-
Field Details
-
ESC
public static final char ESCEscape character '~' per RFC6901.escaped = "~" ( "0" / "1" ) ; representing '~' and '/', respectively
- Since:
- 2.17
- See Also:
-
ESC_SLASH
Escaped slash string "~0" per RFC6901.escaped = "~" ( "0" / "1" ) ; representing '~' and '/', respectively
- Since:
- 2.17
- See Also:
-
ESC_TILDE
Escaped tilde string "~0" per RFC6901.escaped = "~" ( "0" / "1" ) ; representing '~' and '/', respectively
- Since:
- 2.17
- See Also:
-
SEPARATOR
public static final char SEPARATORCharacter used to separate segments.json-pointer = *( "/" reference-token )
- Since:
- 2.9
- See Also:
-
EMPTY
Marker instance used to represent segment that matches current node or position (that is, returns true formatches()). -
_nextSegment
Reference to rest of the pointer beyond currently matching segment (if any); null if this pointer refers to the matching segment. -
_head
Reference from currently matching segment (if any) to node before leaf. Lazily constructed if/as needed.NOTE: we'll use `volatile` here assuming that this is unlikely to become a performance bottleneck. If it becomes one we can probably just drop it and things still should work (despite warnings as per JMM regarding visibility (and lack thereof) of unguarded changes).
- Since:
- 2.5
-
_asString
We will retain representation of the pointer, as a String, so thattoString()should be as efficient as possible.NOTE: starting with 2.14, there is no accompanying
_asStringOffsetthat MUST be considered with this String; thisStringmay contain preceding path, as it is now full path of parent pointer, except for the outermost pointer instance. -
_asStringOffset
protected final int _asStringOffset- Since:
- 2.14
-
_matchingPropertyName
-
_matchingElementIndex
protected final int _matchingElementIndex -
_hashCode
protected int _hashCodeLazily-calculated hash code: need to retain hash code now that we can no longer rely on_asStringbeing the exact full representation (it is often "more", including parent path).- Since:
- 2.14
-
-
Constructor Details
-
JsonPointer
protected JsonPointer()Constructor used for creating "empty" instance, used to represent state that matches current node. -
JsonPointer
-
JsonPointer
protected JsonPointer(String fullString, int fullStringOffset, String segment, int matchIndex, JsonPointer next)
-
-
Method Details
-
compile
Factory method that parses given input and construct matching pointer instance, if it represents a valid JSON Pointer: if not, aIllegalArgumentExceptionis thrown.- Parameters:
expr- Pointer expression to compile- Returns:
- Compiled
JsonPointerpath expression - Throws:
IllegalArgumentException- Thrown if the input does not present a valid JSON Pointer expression: currently the only such expression is one that does NOT start with a slash ('/').
-
valueOf
Alias forcompile(java.lang.String); added to make instances automatically deserializable by Jackson databind.- Parameters:
expr- Pointer expression to compile- Returns:
- Compiled
JsonPointerpath expression
-
empty
Accessor for an "empty" expression, that is, one you can get by callingcompile(java.lang.String)with "" (empty String).NOTE: this is different from expression for
"/"which would instead match Object node property with empty String ("") as name.- Returns:
- "Empty" pointer expression instance that matches given root value
- Since:
- 2.10
-
forPath
Factory method that will construct a pointer instance that describes path to location givenJsonStreamContextpoints to.- Parameters:
context- Context to build pointer expression forincludeRoot- Whether to include number offset for virtual "root context" or not.- Returns:
JsonPointerpath to location of given context- Since:
- 2.9
-
length
public int length()Functionally same as:toString().length()but more efficient as it avoids likely String allocation.- Returns:
- Length of String representation of this pointer instance
- Since:
- 2.14
-
matches
public boolean matches() -
getMatchingProperty
-
getMatchingIndex
public int getMatchingIndex() -
mayMatchProperty
public boolean mayMatchProperty()- Returns:
- True if the root selector matches property name (that is, could match field value of JSON Object node)
-
mayMatchElement
public boolean mayMatchElement()- Returns:
- True if the root selector matches element index (that is, could match an element of JSON Array node)
-
last
- Returns:
- the leaf of current JSON Pointer expression: leaf is the last non-null segment of current JSON Pointer.
- Since:
- 2.5
-
append
Mutant factory method that will return- `tail` if `this` instance is "empty" pointer, OR
- `this` instance if `tail` is "empty" pointer, OR
- Newly constructed
JsonPointerinstance that starts with all segments of `this`, followed by all segments of `tail`.
- Parameters:
tail-JsonPointerinstance to append to this one, to create a new pointer instance- Returns:
- Either `this` instance, `tail`, or a newly created combination, as per description above.
-
appendProperty
ATTENTION!JsonPointeris head-centric, tail appending is much costlier than head appending. It is recommended that this method is used sparingly due to possible sub-par performance. Mutant factory method that will return:- `this` instance if `property` is null, OR
- Newly constructed
JsonPointerinstance that starts with all segments of `this`, followed by new segment of 'property' name.
NOTE! Before Jackson 2.17, no escaping was performed, and leading slash was dropped if passed. This was incorrect implementation. Empty
propertywas also ignored (similar tonull).- Parameters:
property- new segment property name- Returns:
- Either `this` instance, or a newly created combination, as per description above.
-
appendIndex
ATTENTION!JsonPointeris head-centric, tail appending is much costlier than head appending. It is recommended that this method is used sparingly due to possible sub-par performance. Mutant factory method that will return newly constructedJsonPointerinstance that starts with all segments of `this`, followed by new segment of element 'index'. Element 'index' should be non-negative.- Parameters:
index- new segment element index- Returns:
- Newly created combination, as per description above.
- Throws:
IllegalArgumentException- if element index is negative
-
matchesProperty
Method that may be called to see if the pointer head (first segment) would match property (of a JSON Object) with given name.- Parameters:
name- Name of Object property to match- Returns:
Trueif the pointer head matches specified property name- Since:
- 2.5
-
matchProperty
Method that may be called to check whether the pointer head (first segment) matches specified Object property (by name) and if so, returnJsonPointerthat represents rest of the path after match. If there is no match,nullis returned.- Parameters:
name- Name of Object property to match- Returns:
- Remaining path after matching specified property, if there is match;
nullotherwise
-
matchesElement
public boolean matchesElement(int index) Method that may be called to see if the pointer would match Array element (of a JSON Array) with given index.- Parameters:
index- Index of Array element to match- Returns:
Trueif the pointer head matches specified Array index- Since:
- 2.5
-
matchElement
Method that may be called to check whether the pointer head (first segment) matches specified Array index and if so, returnJsonPointerthat represents rest of the path after match. If there is no match,nullis returned.- Parameters:
index- Index of Array element to match- Returns:
- Remaining path after matching specified index, if there is match;
nullotherwise - Since:
- 2.6
-
tail
Accessor for getting a "sub-pointer" (or sub-path), instance where current segment has been removed and pointer includes rest of the segments. For example, for JSON Pointer "/root/branch/leaf", this method would return pointer "/branch/leaf". For matching state (last segment), will returnnull.Note that this is a very cheap method to call as it simply returns "next" segment (which has been constructed when pointer instance was constructed).
- Returns:
- Tail of this pointer, if it has any;
nullif this pointer only has the current segment
-
head
Accessor for getting a pointer instance that is identical to this instance except that the last segment has been dropped. For example, for JSON Pointer "/root/branch/leaf", this method would return pointer "/root/branch" (compared totail()that would return "/branch/leaf").Note that whereas
tail()is a very cheap operation to call (as "tail" already exists for single-linked forward direction), this method has to fully construct a new instance by traversing the chain of segments.- Returns:
- Pointer expression that contains same segments as this one, except for the last segment.
- Since:
- 2.5
-
toString
-
toStringBuilder
Functionally equivalent to:new StringBuilder(toString());
but possibly more efficient- Parameters:
slack- Number of characters to reserve in StringBuilder beyond minimum copied- Returns:
- a new StringBuilder
- Since:
- 2.17
-
hashCode
public int hashCode() -
equals
-
_parseTail
-
_extractEscapedSegment
protected static int _extractEscapedSegment(String input, int firstCharOffset, int i, StringBuilder sb) Method called to extract the next segment of the path, in case where we seem to have encountered a (tilde-)escaped character within segment.- Parameters:
input- Full input for the tail being parsedfirstCharOffset- Offset of the first character of segment (one after slash)i- Offset to character after tildesb- StringBuilder into which unquoted segment is added- Returns:
- Offset at which slash was encountered, if any, or -1 if expression ended without seeing unescaped slash
-
_constructHead
-
_constructHead
-