public class JsonPointer extends Object implements 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.
Modifier and Type | Field and Description |
---|---|
protected String |
_asString
We will retain representation of the pointer, as a String,
so that
toString() should be as efficient as possible. |
protected int |
_asStringOffset |
protected int |
_hashCode
Lazily-calculated hash code: need to retain hash code now that we can no
longer rely on
_asString being the exact full representation (it
is often "more", including parent path). |
protected JsonPointer |
_head
Reference from currently matching segment (if any) to node
before leaf.
|
protected int |
_matchingElementIndex |
protected String |
_matchingPropertyName |
protected JsonPointer |
_nextSegment
Reference to rest of the pointer beyond currently matching
segment (if any); null if this pointer refers to the matching
segment.
|
protected static JsonPointer |
EMPTY
Marker instance used to represent segment that matches current
node or position (that is, returns true for
matches() ). |
static char |
ESC
Escape character 126 per RFC6901.
|
static String |
ESC_SLASH
|
static String |
ESC_TILDE
Escaped tilde string "~0" per RFC6901.
|
static char |
SEPARATOR
Character used to separate segments.
|
Modifier | Constructor and Description |
---|---|
protected |
JsonPointer()
Constructor used for creating "empty" instance, used to represent
state that matches current node.
|
protected |
JsonPointer(String fullString,
int fullStringOffset,
String segment,
int matchIndex,
JsonPointer next) |
protected |
JsonPointer(String fullString,
int fullStringOffset,
String segment,
JsonPointer next) |
Modifier and Type | Method and Description |
---|---|
protected JsonPointer |
_constructHead() |
protected 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) |
JsonPointer |
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 constructed
JsonPointer instance that starts with all segments
of `this`, followed by all segments of `tail`. |
JsonPointer |
appendIndex(int index)
ATTENTION!
|
JsonPointer |
appendProperty(String property)
ATTENTION!
|
static JsonPointer |
compile(String expr)
Factory method that parses given input and construct matching pointer
instance, if it represents a valid JSON Pointer: if not, a
IllegalArgumentException is thrown. |
static JsonPointer |
empty()
Accessor for an "empty" expression, that is, one you can get by
calling
compile(java.lang.String) with "" (empty String). |
boolean |
equals(Object o) |
static JsonPointer |
forPath(JsonStreamContext context,
boolean includeRoot)
Factory method that will construct a pointer instance that describes
path to location given
JsonStreamContext points to. |
int |
getMatchingIndex() |
String |
getMatchingProperty() |
int |
hashCode() |
JsonPointer |
head()
Accessor for getting a pointer instance that is identical to this
instance except that the last segment has been dropped.
|
JsonPointer |
last() |
int |
length()
Functionally same as:
toString().length()
but more efficient as it avoids likely String allocation. |
JsonPointer |
matchElement(int index)
Method that may be called to check whether the pointer head (first segment)
matches specified Array index and if so, return
JsonPointer that represents rest of the path after match. |
boolean |
matches() |
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.
|
boolean |
matchesProperty(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.
|
JsonPointer |
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, return
JsonPointer that represents rest of the path after match. |
boolean |
mayMatchElement() |
boolean |
mayMatchProperty() |
JsonPointer |
tail()
Accessor for getting a "sub-pointer" (or sub-path), instance where current segment
has been removed and pointer includes rest of the segments.
|
String |
toString() |
protected StringBuilder |
toStringBuilder(int slack)
Functionally equivalent to:
new StringBuilder(toString());
but possibly more efficient
|
static JsonPointer |
valueOf(String expr)
Alias for
compile(java.lang.String) ; added to make instances automatically
deserializable by Jackson databind. |
public static final char ESC
escaped = "~" ( "0" / "1" ) ; representing '~' and '/', respectively
public static final String ESC_SLASH
escaped = "~" ( "0" / "1" ) ; representing '~' and '/', respectively
public static final String ESC_TILDE
escaped = "~" ( "0" / "1" ) ; representing '~' and '/', respectively
public static final char SEPARATOR
json-pointer = *( "/" reference-token )
protected static final JsonPointer EMPTY
matches()
).protected final JsonPointer _nextSegment
protected volatile JsonPointer _head
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).
protected final String _asString
toString()
should be as efficient as possible.
NOTE: starting with 2.14, there is no accompanying
_asStringOffset
that MUST be considered with this String;
this String
may contain preceding path, as it is now full path
of parent pointer, except for the outermost pointer instance.
protected final int _asStringOffset
protected final String _matchingPropertyName
protected final int _matchingElementIndex
protected int _hashCode
_asString
being the exact full representation (it
is often "more", including parent path).protected JsonPointer()
protected JsonPointer(String fullString, int fullStringOffset, String segment, JsonPointer next)
protected JsonPointer(String fullString, int fullStringOffset, String segment, int matchIndex, JsonPointer next)
public static JsonPointer compile(String expr) throws IllegalArgumentException
IllegalArgumentException
is thrown.expr
- Pointer expression to compileJsonPointer
path expressionIllegalArgumentException
- 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 ('/').public static JsonPointer valueOf(String expr)
compile(java.lang.String)
; added to make instances automatically
deserializable by Jackson databind.expr
- Pointer expression to compileJsonPointer
path expressionpublic static JsonPointer empty()
compile(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.
public static JsonPointer forPath(JsonStreamContext context, boolean includeRoot)
JsonStreamContext
points to.context
- Context to build pointer expression forincludeRoot
- Whether to include number offset for virtual "root context"
or not.JsonPointer
path to location of given contextpublic int length()
toString().length()
but more efficient as it avoids likely String allocation.public boolean matches()
public String getMatchingProperty()
public int getMatchingIndex()
public boolean mayMatchProperty()
public boolean mayMatchElement()
public JsonPointer last()
public JsonPointer append(JsonPointer tail)
JsonPointer
instance that starts with all segments
of `this`, followed by all segments of `tail`.
tail
- JsonPointer
instance to append to this one, to create a new pointer instancepublic JsonPointer appendProperty(String property)
JsonPointer
is 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:
JsonPointer
instance 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 property
was also ignored (similar to null
).
property
- new segment property namepublic JsonPointer appendIndex(int index)
JsonPointer
is 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 constructed JsonPointer
instance that starts with all
segments of `this`, followed by new segment of element 'index'. Element 'index' should be non-negative.index
- new segment element indexIllegalArgumentException
- if element index is negativepublic boolean matchesProperty(String name)
name
- Name of Object property to matchTrue
if the pointer head matches specified property namepublic JsonPointer matchProperty(String name)
JsonPointer
that represents rest of the path after match.
If there is no match, null
is returned.name
- Name of Object property to matchnull
otherwisepublic boolean matchesElement(int index)
index
- Index of Array element to matchTrue
if the pointer head matches specified Array indexpublic JsonPointer matchElement(int index)
JsonPointer
that represents rest of the path after match.
If there is no match, null
is returned.index
- Index of Array element to matchnull
otherwisepublic JsonPointer tail()
null
.
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).
null
if this pointer only
has the current segmentpublic JsonPointer head()
tail()
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.
protected StringBuilder toStringBuilder(int slack)
new StringBuilder(toString());but possibly more efficient
slack
- Number of characters to reserve in StringBuilder beyond
minimum copiedprotected static JsonPointer _parseTail(String fullPath)
protected static int _extractEscapedSegment(String input, int firstCharOffset, int i, StringBuilder sb)
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 addedprotected JsonPointer _constructHead()
protected JsonPointer _constructHead(int suffixLength, JsonPointer last)
Copyright © 2008–2024 FasterXML. All rights reserved.