public final class JsonPointer extends JsonFragment
JSON Pointer is an IETF draft defining a way to address paths within JSON values (including non container values).
An individual entry of a JSON Pointer is called a reference token. For
JSON Objects, a reference token is a member name. For arrays, it is an index.
Indices start at 0. Note that array indices written with a leading 0 are
considered to be failing (ie, 0
is OK but 00
is not).
The general syntax is /reference/tokens/here
. A JSON Pointer
may be empty, in which case this refers to the JSON value itself.
The difficulty solved by JSON Pointer is that any JSON String is valid as an object member name. These are all valid object member names, and all of them can be addressed by using JSON Pointer:
""
-- the empty string;"/"
;"0"
;"-1"
;"."
, ".."
, "../.."
.The latter example is the reason why a JSON Pointer is always absolute.
All instances of this class are immutable (and therefore thread safe).
asString
Constructor and Description |
---|
JsonPointer(String input)
Constructor
|
Modifier and Type | Method and Description |
---|---|
JsonPointer |
append(int index)
Append an array index to this pointer.
|
JsonPointer |
append(JsonPointer other)
Append a pointer to the current pointer
|
JsonPointer |
append(String element)
Append a reference token as a string to this pointer.
|
List<JsonPointer> |
asElements()
Return this pointer as a series of JSON Pointers starting from the
beginning
|
static JsonPointer |
empty()
Return an empty pointer
|
boolean |
isEmpty()
Tell whether this fragment is empty
|
boolean |
isParentOf(JsonPointer other)
Return true if this JSON Pointer is "parent" of another one
|
boolean |
isPointer()
Tell whether this fragment is a valid JSON Pointer
|
JsonPointer |
relativize(JsonPointer other)
Relativize a pointer to the current pointer
|
JsonNode |
resolve(JsonNode node)
Apply the pointer to a JSON value and return the result
|
compareTo, equals, fromFragment, hashCode, toString
public JsonPointer(String input) throws JsonSchemaException
input
- The input string, guaranteed not to be JSON encodedJsonSchemaException
- illegal JSON Pointerpublic static JsonPointer empty()
public JsonPointer append(JsonPointer other)
other
- the other pointerpublic JsonPointer append(String element)
element
- the element to appendpublic JsonPointer append(int index)
Note that the index validity is NOT checked for (ie, you can append
-1
if you want to -- don't do that)
index
- the index to addpublic JsonNode resolve(JsonNode node)
If the pointer fails to look up a value, a MissingNode
is
returned.
resolve
in class JsonFragment
node
- the node to apply the pointer topublic boolean isEmpty()
JsonFragment
isEmpty
in class JsonFragment
JsonRef.isAbsolute()
public boolean isPointer()
JsonFragment
isPointer
in class JsonFragment
JsonPointer
public List<JsonPointer> asElements()
public boolean isParentOf(JsonPointer other)
That is, its number of reference tokens is less than, or equal to, the other pointer's, and its first reference tokens are the same.
This means that this will also return true if the pointers are equal.
other
- the other pointerpublic JsonPointer relativize(JsonPointer other)
If isParentOf(JsonPointer)
returns false, this will return
the other pointer.
Otherwise, it will return a pointer containing all reference tokens
following this pointer's reference tokens. For instance, relativizing
/a/b
against /a/b/c
gives /c
.
If the pointers are the same, it will return an empty pointer.
other
- the pointer to relativize this pointer toCopyright © 2013. All Rights Reserved.