public final class JsonPointer extends JsonFragment implements Comparable<JsonPointer>
JSON Pointer is an IETF draft defining a way to address paths within JSON documents. Paths apply to container objects, ie arrays or nodes. For objects, path elements are property names. For arrays, they are the index in the array (starting from 0).
The general syntax is #/path/elements/here. A path element is
referred to as a "reference token" in the specification.
The difficulty with JSON Pointer is that any JSON String is valid as an object's property name. These are all valid:
"" -- 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 (therefore thread safe).
| Modifier and Type | Field and Description |
|---|---|
private List<String> |
elements
The list of individual elements in the pointer.
|
private static CharMatcher |
ESCAPE_CHAR |
private static BiMap<Character,Character> |
ESCAPE_REPLACEMENT_MAP |
private static CharMatcher |
ESCAPED |
private String |
fullPointer
The pointer in a raw, but JSON Pointer-escaped, string.
|
private static CharMatcher |
SLASH |
private static CharMatcher |
SPECIAL |
| Modifier | Constructor and Description |
|---|---|
|
JsonPointer(String input)
Constructor
|
private |
JsonPointer(String fullPointer,
List<String> elements) |
| Modifier and Type | Method and Description |
|---|---|
JsonPointer |
append(int index)
Append an array index to this pointer.
|
JsonPointer |
append(String element)
Append a path element to this pointer.
|
int |
compareTo(JsonPointer other) |
private static void |
decode(String input,
ImmutableList.Builder<String> builder)
Initialize the object
|
boolean |
equals(Object obj) |
private static String |
getNextRefToken(String input)
Grab a (cooked) reference token from an input string
|
int |
hashCode() |
private static JsonSchemaException |
illegalPointer(String message) |
private static String |
refTokenDecode(String cooked)
Turn a cooked reference token into a raw reference token
|
private static String |
refTokenEncode(String raw)
Make a cooked reference token out of a raw element token
|
JsonNode |
resolve(JsonNode node)
Resolve this fragment against a given node
|
String |
toString() |
fromFragment, isEmptyprivate static final CharMatcher SLASH
private static final CharMatcher ESCAPE_CHAR
private static final CharMatcher ESCAPED
private static final CharMatcher SPECIAL
private final String fullPointer
public JsonPointer(String input) throws JsonSchemaException
input - The input string, guaranteed not to be JSON encodedJsonSchemaException - Illegal JSON 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)
JsonFragmentresolve in class JsonFragmentnode - the nodeMissingNode if the fragment is not
found)private static void decode(String input, ImmutableList.Builder<String> builder) throws JsonSchemaException
We read the string sequentially, a slash, then a reference token, then a slash, etc. Bail out if the string is malformed.
input - Input string, guaranteed not to be JSON/URI encodedbuilder - the list builderJsonSchemaException - the input is not a valid JSON Pointerprivate static String getNextRefToken(String input) throws JsonSchemaException
This method is only called from
decode(String, ImmutableList.Builder), after a delimiter
(/) has been swallowed up. The input string is therefore
guaranteed to start with a reference token, which may be empty.
input - the input stringJsonSchemaException - the string is malformedprivate static String refTokenDecode(String cooked)
This means we replace all occurrences of ~0 with ~,
and all occurrences of ~1 with /.
It is called from decode(java.lang.String, com.google.common.collect.ImmutableList.Builder<java.lang.String>), in order to push a token into
elements.
cooked - the cooked tokenprivate static String refTokenEncode(String raw)
Used to build new pointer instances when called from (String) or append(int), in order to build fullPointer.
raw - the raw tokenprivate static JsonSchemaException illegalPointer(String message)
public int compareTo(JsonPointer other)
compareTo in interface Comparable<JsonPointer>public String toString()
toString in class JsonFragmentCopyright © 2012. All Rights Reserved.