Package org.eclipse.ditto.json
Interface JsonPointer
- All Superinterfaces:
CharSequence
,Iterable<JsonKey>
A JSON pointer defines a path within a JSON object. This path is hierarchical like a file tree. Thus each element of
this path is a sub level of it predecessor beginning at the root and ending at the leaf. For example, given the
following JSON object
{ "thingId": "myThing", "attributes": { "location": { "latitude": 47.003215, "longitude": 9.0815 } }, "features": {} }The root of the JSON pointer
"/attributes/location/longitude"
is "/attributes"
and the leaf is
"longitude"
. This pointer points to the field with key "longitude"
and value 9.0815
of the
aforementioned JSON object.
Implementations of this interface are required to be immutable!
-
Method Summary
Modifier and TypeMethodDescriptionCreates a new JSON pointer by adding a level to this JSON pointer.append
(JsonPointer pointer) Creates a new JSON pointer by appending the given pointer to this pointer.cutLeaf()
Creates a new pointer by removing the leaf element of this pointer.static JsonPointer
empty()
Returns an empty JSON pointer.get
(int level) Returns the JSON key at the specified level within this JSON pointer.getLeaf()
Returns the leaf JSON key of this pointer.int
Returns the number of levels of this JSON pointer.getPrefixPointer
(int level) Returns a new JSON pointer including all JSON keys from the root until the passed in level.getRoot()
Returns the root JSON key of this pointer.getSubPointer
(int level) Returns a new JSON pointer including all JSON keys from to the passed level and upwards.boolean
isEmpty()
Indicates whether this pointer does contain any elements or not.Goes to the next sub level of this pointer.static JsonPointer
of
(CharSequence slashDelimitedCharSequence) Parses the given string to obtain a new JSON pointer.Returns a newJsonFieldSelector
containing this pointer as its only element.toString()
The string representation of this pointer, i.Methods inherited from interface java.lang.CharSequence
charAt, chars, codePoints, length, subSequence
Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Method Details
-
empty
Returns an empty JSON pointer. An empty pointer is represented by string"/"
.- Returns:
- the pointer.
-
of
Parses the given string to obtain a new JSON pointer. This method is the inverse oftoString()
with one exception: both strings"/"
and""
lead to an empty pointer while the string representation of an empty string is always"/"
.To support tildes in JsonKeys, they have to be escaped with
"~0"
. For example, parsing the string"/foo/~0dum~0die~0dum/baz"
would result in a JsonPointer consisting of the JsonKeys"foo"
,"~dum~die~dum"
and"baz"
.
- Parameters:
slashDelimitedCharSequence
- slash-delimited string representing of a JSON pointer. The leading slash may be omitted.- Returns:
- a new JSON pointer consisting of the JSON keys which were extracted from
slashDelimitedCharSequence
. - Throws:
NullPointerException
- ifslashDelimitedCharSequence
isnull
.JsonPointerInvalidException
- if the passedslashDelimitedCharSequence
contained double slashes.
-
addLeaf
Creates a new JSON pointer by adding a level to this JSON pointer. For example, if this pointer is"/foo/bar"
andaddLevel()
is called with a JSON key"baz"
then the new JSON pointer is"/foo/bar/baz"
.- Parameters:
key
- the new level JSON key.- Returns:
- a new JSON pointer consisting of the old pointer extended by
jsonField
. - Throws:
NullPointerException
- ifkey
isnull
.
-
append
Creates a new JSON pointer by appending the given pointer to this pointer.- Parameters:
pointer
- the pointer to be appended to this pointer.- Returns:
- a new JSON pointer with this pointer as root followed by
pointer
. - Throws:
NullPointerException
- ifpointer
isnull
.
-
getLevelCount
int getLevelCount()Returns the number of levels of this JSON pointer. For example, if the pointer is"foo/bar/baz"
this method will return the value 3.- Returns:
- the number of levels of this pointer.
-
isEmpty
boolean isEmpty()Indicates whether this pointer does contain any elements or not. If the level count is zero the pointer is regarded to be empty.- Specified by:
isEmpty
in interfaceCharSequence
- Returns:
true
if this pointer does not contain any elements,false
else.
-
get
Returns the JSON key at the specified level within this JSON pointer.- Parameters:
level
- the level of the JSON key to return.- Returns:
- the JSON key at the specified level within this JSON pointer. If the level is outside the bounds of this pointer the result is empty.
-
getRoot
Returns the root JSON key of this pointer. If the level count of this pointer isone
the root is the same as the leaf.- Returns:
- the root key if this pointer is not empty.
-
getLeaf
Returns the leaf JSON key of this pointer. If the level count of this pointer isone
the leaf is the same as the root.- Returns:
- the leaf key if this pointer is not empty.
-
cutLeaf
JsonPointer cutLeaf()Creates a new pointer by removing the leaf element of this pointer.- Returns:
- a new pointer which does not contain the leaf element of this pointer. If this pointer is empty this pointer instance is returned.
-
nextLevel
JsonPointer nextLevel()Goes to the next sub level of this pointer.- Returns:
- a new pointer beginning with the element after the root of this pointer. If this pointer is empty this pointer instance is returned.
-
getSubPointer
Returns a new JSON pointer including all JSON keys from to the passed level and upwards. For example, if this pointer is/foo/bar/baz
then calling this method with level 1 returns the pointer/bar/baz
.- Parameters:
level
- the level from which (upwards) JSON keys should be included in the result. The key at the specified level is part of the result.- Returns:
- the sub pointer starting from
level
. If the level is outside the bounds of this pointer the result is empty.
-
getPrefixPointer
Returns a new JSON pointer including all JSON keys from the root until the passed in level. For example, if this pointer is/foo/bar/baz
then calling this method with level 1 returns the pointer/foo/bar
.- Parameters:
level
- the level from which (downwards) JSON keys should be included in the result. The key at the specified level is part of the result.- Returns:
- the sub pointer starting from the root until
level
. Iflevel
is outside the bounds of this pointer the result is empty.
-
toFieldSelector
JsonFieldSelector toFieldSelector()Returns a newJsonFieldSelector
containing this pointer as its only element.- Returns:
- a new JSON field selector containing only this pointer.
-
toString
String toString()The string representation of this pointer, i. e. all of its levels concatenated by"/"
. For example, if this pointer consists of the three levels"foo"
,"bar"
and"baz"
, this method will return the string"/foo/bar/baz"
.- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
- Returns:
- the string representation of this pointer.
-