Top Description Inners Fields Constructors Methods
com.fasterxml.jackson.core

public Class JsonPointer

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.*, com.fasterxml.jackson.core.io.NumberInput

Implementation of JSON Pointer specification. Pointer instances can be used to locate logical JSON nodes for things like tree traversal (see TreeNode#at). 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.

Author
Tatu Saloranta
Since
2.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
JsonPointer.PointerParent

Helper class used to replace call stack when parsing JsonPointer expressions.

private static class
JsonPointer.PointerSegment

Helper class used to contain a single segment when constructing JsonPointer from context.

pack-priv static class
JsonPointer.Serialization

This must only exist to allow both final properties and implementation of Externalizable/Serializable for JsonPointer.

Field Summary

Modifier and TypeField and Description
protected final String
_asString

We will retain representation of the pointer, as a String, so that toString should be as efficient as possible.

protected final int
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 volatile JsonPointer
_head

Reference from currently matching segment (if any) to node before leaf.

protected final int
protected final String
protected final 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 final JsonPointer
EMPTY

Marker instance used to represent segment that matches current node or position (that is, returns true for matches()).

public static final char
ESC

Escape character ~ per RFC6901.

public static final String
ESC_SLASH

Escaped slash string ~0 per RFC6901.

public static final String
ESC_TILDE

Escaped tilde string ~0 per RFC6901.

public static final char
SEPARATOR

Character used to separate segments.

private static final long

Constructor Summary

AccessConstructor 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, JsonPointer next)

protected
JsonPointer(String fullString, int fullStringOffset, String segment, int matchIndex, JsonPointer next)

Method Summary

Modifier and TypeMethod and Description
private static void
private static void
private static JsonPointer
_buildPath(final String fullPath, int fullPathOffset, String segment, JsonPointer.PointerParent parent)

private final boolean
_compare(String str1, int offset1, String str2, int offset2)

protected JsonPointer
protected JsonPointer
_constructHead(int suffixLength, JsonPointer last)

protected static int

Returns:

Offset at which slash was encountered, if any, or -1 if expression ended without seeing unescaped slash
_extractEscapedSegment
(String
Full input for the tail being parsed
input
,
int
Offset of the first character of segment (one after slash)
firstCharOffset
,
int
Offset to character after tilde
i
,
StringBuilder
StringBuilder into which unquoted segment is added
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.

private static final int
protected static JsonPointer
_parseTail(final String fullPath)

public JsonPointer

Returns:

Either `this` instance, `tail`, or a newly created combination, as per description above.
append
(JsonPointer
JsonPointer instance to append to this one, to create a new pointer instance
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`.
public JsonPointer

Returns:

Newly created combination, as per description above.
appendIndex
(int
new segment element index
index
)

ATTENTION!

public JsonPointer

Returns:

Either `this` instance, or a newly created combination, as per description above.
appendProperty
(String
new segment property name
property
)

ATTENTION!

public static JsonPointer

Returns:

Compiled JsonPointer path expression
compile
(String
Pointer expression to compile
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.

public static JsonPointer

Returns:

"Empty" pointer expression instance that matches given root value
empty
()

Accessor for an "empty" expression, that is, one you can get by calling compile with "" (empty String).

public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public static JsonPointer

Returns:

JsonPointer path to location of given context
forPath
(JsonStreamContext
Context to build pointer expression for
context
,
boolean
Whether to include number offset for virtual "root context" or not.
includeRoot
)

Factory method that will construct a pointer instance that describes path to location given JsonStreamContext points to.

public int
public String
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public JsonPointer

Returns:

Pointer expression that contains same segments as this one, except for the last segment.
head
()

Accessor for getting a pointer instance that is identical to this instance except that the last segment has been dropped.

public JsonPointer

Returns:

the leaf of current JSON Pointer expression: leaf is the last non-null segment of current JSON Pointer.
last
()

public int

Returns:

Length of String representation of this pointer instance
length
()

Functionally same as: toString().length() but more efficient as it avoids likely String allocation.

public JsonPointer

Returns:

Remaining path after matching specified index, if there is match; null otherwise
matchElement
(int
Index of Array element to match
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.

public boolean
public boolean

Returns:

True if the pointer head matches specified Array index
matchesElement
(int
Index of Array element to match
index
)

Method that may be called to see if the pointer would match Array element (of a JSON Array) with given index.

public boolean

Returns:

True if the pointer head matches specified property name
matchesProperty
(String
Name of Object property to match
name
)

Method that may be called to see if the pointer head (first segment) would match property (of a JSON Object) with given name.

public JsonPointer

Returns:

Remaining path after matching specified property, if there is match; null otherwise
matchProperty
(String
Name of Object property to match
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.

public boolean

Returns:

True if the root selector matches element index (that is, could match an element of JSON Array node)
mayMatchElement
()

public boolean

Returns:

True if the root selector matches property name (that is, could match field value of JSON Object node)
mayMatchProperty
()

public JsonPointer

Returns:

Tail of this pointer, if it has any; null if this pointer only has the current segment
tail
()

Accessor for getting a "sub-pointer" (or sub-path), instance where current segment has been removed and pointer includes rest of the segments.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

protected StringBuilder

Returns:

a new StringBuilder
toStringBuilder
(int
Number of characters to reserve in StringBuilder beyond minimum copied
slack
)

Functionally equivalent to:

   new StringBuilder(toString());
public static JsonPointer

Returns:

Compiled JsonPointer path expression
valueOf
(String
Pointer expression to compile
expr
)

Alias for compile; added to make instances automatically deserializable by Jackson databind.

private Object
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

_asStringback to summary
protected final String _asString

We will retain representation of the pointer, as a String, so that 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.

_asStringOffsetback to summary
protected final int _asStringOffset
Since
2.14
_hashCodeback to summary
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).

Since
2.14
_headback to summary
protected volatile JsonPointer _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
_matchingElementIndexback to summary
protected final int _matchingElementIndex
_matchingPropertyNameback to summary
protected final String _matchingPropertyName
_nextSegmentback to summary
protected final JsonPointer _nextSegment

Reference to rest of the pointer beyond currently matching segment (if any); null if this pointer refers to the matching segment.

EMPTYback to summary
protected static final JsonPointer EMPTY

Marker instance used to represent segment that matches current node or position (that is, returns true for matches()).

ESCback to summary
public static final char ESC

Escape character ~ per RFC6901.

escaped         = "~" ( "0" / "1" )
 ; representing '~' and '/', respectively
Since
2.17
ESC_SLASHback to summary
public static final String ESC_SLASH

Escaped slash string ~0 per RFC6901.

escaped         = "~" ( "0" / "1" )
 ; representing '~' and '/', respectively
Since
2.17
ESC_TILDEback to summary
public static final String ESC_TILDE

Escaped tilde string ~0 per RFC6901.

escaped         = "~" ( "0" / "1" )
 ; representing '~' and '/', respectively
Since
2.17
SEPARATORback to summary
public static final char SEPARATOR

Character used to separate segments.

json-pointer    = *( "/" reference-token )
Since
2.9
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

JsonPointerback to summary
protected JsonPointer()

Constructor used for creating "empty" instance, used to represent state that matches current node.

JsonPointerback to summary
protected JsonPointer(String fullString, int fullStringOffset, String segment, JsonPointer next)
JsonPointerback to summary
protected JsonPointer(String fullString, int fullStringOffset, String segment, int matchIndex, JsonPointer next)

Method Detail

_appendEscapeback to summary
private static void _appendEscape(StringBuilder sb, char c)
_appendEscapedback to summary
private static void _appendEscaped(StringBuilder sb, String segment)
_buildPathback to summary
private static JsonPointer _buildPath(final String fullPath, int fullPathOffset, String segment, JsonPointer.PointerParent parent)
_compareback to summary
private final boolean _compare(String str1, int offset1, String str2, int offset2)
_constructHeadback to summary
protected JsonPointer _constructHead()
_constructHeadback to summary
protected JsonPointer _constructHead(int suffixLength, JsonPointer last)
_extractEscapedSegmentback to summary
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:String

Full input for the tail being parsed

firstCharOffset:int

Offset of the first character of segment (one after slash)

i:int

Offset to character after tilde

sb:StringBuilder

StringBuilder into which unquoted segment is added

Returns:int

Offset at which slash was encountered, if any, or -1 if expression ended without seeing unescaped slash

_parseIndexback to summary
private static final int _parseIndex(String str)
_parseTailback to summary
protected static JsonPointer _parseTail(final String fullPath)
appendback to summary
public 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`.
Parameters
tail:JsonPointer

JsonPointer instance to append to this one, to create a new pointer instance

Returns:JsonPointer

Either `this` instance, `tail`, or a newly created combination, as per description above.

appendIndexback to summary
public JsonPointer appendIndex(int index)

ATTENTION! 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.

Parameters
index:int

new segment element index

Returns:JsonPointer

Newly created combination, as per description above.

Exceptions
IllegalArgumentException:
if element index is negative
appendPropertyback to summary
public JsonPointer appendProperty(String property)

ATTENTION! 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:

  • `this` instance if `property` is null, OR
  • Newly constructed JsonPointer instance that starts with all segments of `this`, followed by new segment of 'property' name.
'property' is name to match: value is escaped as necessary (for any contained slashes or tildes).

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).

Parameters
property:String

new segment property name

Returns:JsonPointer

Either `this` instance, or a newly created combination, as per description above.

compileback to summary
public static JsonPointer compile(String expr) throws IllegalArgumentException

Factory method that parses given input and construct matching pointer instance, if it represents a valid JSON Pointer: if not, a IllegalArgumentException is thrown.

Parameters
expr:String

Pointer expression to compile

Returns:JsonPointer

Compiled JsonPointer path expression

Exceptions
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 ('/').
emptyback to summary
public static JsonPointer empty()

Accessor for an "empty" expression, that is, one you can get by calling compile with "" (empty String).

Note

this is different from expression for "/" which would instead match Object node property with empty String ("") as name.

Returns:JsonPointer

"Empty" pointer expression instance that matches given root value

Since
2.10
equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
forPathback to summary
public static JsonPointer forPath(JsonStreamContext context, boolean includeRoot)

Factory method that will construct a pointer instance that describes path to location given JsonStreamContext points to.

Parameters
context:JsonStreamContext

Context to build pointer expression for

includeRoot:boolean

Whether to include number offset for virtual "root context" or not.

Returns:JsonPointer

JsonPointer path to location of given context

Since
2.9
getMatchingIndexback to summary
public int getMatchingIndex()
getMatchingPropertyback to summary
public String getMatchingProperty()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
headback to summary
public JsonPointer 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 to 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.

Returns:JsonPointer

Pointer expression that contains same segments as this one, except for the last segment.

Since
2.5
lastback to summary
public JsonPointer last()
Returns:JsonPointer

the leaf of current JSON Pointer expression: leaf is the last non-null segment of current JSON Pointer.

Since
2.5
lengthback to summary
public int length()

Functionally same as: toString().length() but more efficient as it avoids likely String allocation.

Returns:int

Length of String representation of this pointer instance

Since
2.14
matchElementback to summary
public 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. If there is no match, null is returned.

Parameters
index:int

Index of Array element to match

Returns:JsonPointer

Remaining path after matching specified index, if there is match; null otherwise

Since
2.6
matchesback to summary
public boolean matches()
matchesElementback to summary
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:int

Index of Array element to match

Returns:boolean

True if the pointer head matches specified Array index

Since
2.5
matchesPropertyback to summary
public 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.

Parameters
name:String

Name of Object property to match

Returns:boolean

True if the pointer head matches specified property name

Since
2.5
matchPropertyback to summary
public 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. If there is no match, null is returned.

Parameters
name:String

Name of Object property to match

Returns:JsonPointer

Remaining path after matching specified property, if there is match; null otherwise

mayMatchElementback to summary
public boolean mayMatchElement()
Returns:boolean

True if the root selector matches element index (that is, could match an element of JSON Array node)

mayMatchPropertyback to summary
public boolean mayMatchProperty()
Returns:boolean

True if the root selector matches property name (that is, could match field value of JSON Object node)

tailback to summary
public 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. For example, for JSON Pointer "/root/branch/leaf", this method would return pointer "/branch/leaf". For matching state (last segment), will return 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).

Returns:JsonPointer

Tail of this pointer, if it has any; null if this pointer only has the current segment

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
toStringBuilderback to summary
protected StringBuilder toStringBuilder(int slack)

Functionally equivalent to:

   new StringBuilder(toString());
but possibly more efficient
Parameters
slack:int

Number of characters to reserve in StringBuilder beyond minimum copied

Returns:StringBuilder

a new StringBuilder

Since
2.17
valueOfback to summary
public static JsonPointer valueOf(String expr)

Alias for compile; added to make instances automatically deserializable by Jackson databind.

Parameters
expr:String

Pointer expression to compile

Returns:JsonPointer

Compiled JsonPointer path expression

writeReplaceback to summary
private Object writeReplace()
com.fasterxml.jackson.core back to summary

private Class JsonPointer.PointerParent

extends Object
Class Inheritance

Helper class used to replace call stack when parsing JsonPointer expressions.

Field Summary

Modifier and TypeField and Description
public final int
public final JsonPointer.PointerParent
public final String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fullPathOffsetback to summary
public final int fullPathOffset
parentback to summary
public final JsonPointer.PointerParent parent
segmentback to summary
public final String segment

Constructor Detail

PointerParentback to summary
pack-priv PointerParent(JsonPointer.PointerParent pp, int fpo, String sgm)
com.fasterxml.jackson.core back to summary

private Class JsonPointer.PointerSegment

extends Object
Class Inheritance

Helper class used to contain a single segment when constructing JsonPointer from context.

Field Summary

Modifier and TypeField and Description
public final int
public final JsonPointer.PointerSegment
public int
public JsonPointer.PointerSegment
public final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

indexback to summary
public final int index
nextback to summary
public final JsonPointer.PointerSegment next
pathOffsetback to summary
public int pathOffset
prevback to summary
public JsonPointer.PointerSegment prev
propertyback to summary
public final String property

Constructor Detail

PointerSegmentback to summary
public PointerSegment(JsonPointer.PointerSegment next, String pn, int ix)
com.fasterxml.jackson.core back to summary

pack-priv Class JsonPointer.Serialization

extends Object
implements Externalizable
Class Inheritance
All Implemented Interfaces
java.io.Externalizable, java.io.Serializable

This must only exist to allow both final properties and implementation of Externalizable/Serializable for JsonPointer. Note that here we do not store offset but simply use (and expect use) full path, from which we need to decode actual structure.
Since
2.14

Field Summary

Modifier and TypeField and Description
private String

Constructor Summary

AccessConstructor and Description
public
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
readExternal(ObjectInput
the stream to read data from in order to restore the object
in
)

Implements java.io.Externalizable.readExternal.

The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays.

private Object
public void
writeExternal(ObjectOutput
the stream to write the object to
out
)

Implements java.io.Externalizable.writeExternal.

The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_fullPathback to summary
private String _fullPath

Constructor Detail

Serializationback to summary
public Serialization()
Serializationback to summary
pack-priv Serialization(String fullPath)

Method Detail

readExternalback to summary
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException

Implements java.io.Externalizable.readExternal.

Doc from java.io.Externalizable.readExternal.

The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.

Parameters
in:ObjectInput

the stream to read data from in order to restore the object

Annotations
@Override
Exceptions
IOException:
if I/O errors occur
ClassNotFoundException:
If the class for an object being restored cannot be found.
readResolveback to summary
private Object readResolve() throws ObjectStreamException
writeExternalback to summary
public void writeExternal(ObjectOutput out) throws IOException

Implements java.io.Externalizable.writeExternal.

Doc from java.io.Externalizable.writeExternal.

The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.

Parameters
out:ObjectOutput

the stream to write the object to

Annotations
@Override
Exceptions
IOException:
Includes any I/O exceptions that may occur