public final class JsPath extends Object implements Comparable<JsPath>
Represents the full path location of an element in a json. It's a list ofPosition
. Exists two different ways to create a JsPath: - From a path-like string using the static factory methodpath(String)
, where the path follows the Json Pointer specification RFC 6901. In order to be able to use paths to put data in a Json, keys which name are numbers have to to be single-quoted:a={"0": true} b=[false]
According to the rfc 6901: the pointer /0 points to true in a, and to false in b. In json-values it's slightly different: /0 points to false in b, and /'0' points to true in a. It's necessary to make that distinction because otherwise, there are scenarios when there is no way to know if the user wants to insert an array or an object:<script>
JsObj obj = empty.put("/a/0/0",true) obj = {"a":[[true]]} //valid result according to the rfc and json-values obj = {"a":{"0":{"0":true} //valid result according to the rfc<script>
- By the API, using the methodsfromKey(String)
andfromIndex(int)
to create a JsPath and then the methodsindex(int)
andkey(String)
to append keys or indexes:<script>
JsPath a = JsPath.fromKey("a").index(0).key("b") = /a/0/b JsPath b = JsPath.fromIndex(0).key("a").index(0) = /0/a/0<script>
For example, given the following Json object: { "a": { "x":[ {"c": [1,2, { "": { "1" : true, " ": false, "'": 4} }]} ]}} "1": null, "": "" } / = "" //an empty string is a valid name for a key /'1' = null //numeric keys have to be single-quoted /a/x/0/c/0 = 1 /a/x/0/c/1 = 2 /a/x/0/c/2//'1' = true // single quotes are only mandatory when the key is a number according to the rfc, # at the beginning indicates that the path is a fragment of an url and therefore the keys have to be url-encoded: #/a/x/0/c/2//+" = false // + is url-decoded to the white-space #/a/x/0/c/2//%27" = 4 // %27 is url-decoded to ' and using the API:<script>
fromKey("") = "" fromKey("1") = null fromKey("a").key("x").index(0).key("c").index(0) = 1 fromKey("a").key("x").index(0).key("c").index(1) = 2 fromKey("a").key("x").index(0).key("c").index(2).key("").key("1") = true fromKey("a").key("x").index(0).key("c").index(2).key("").key(" ") = false fromKey("a").key("x").index(0).key("c").index(2).key("").key("'") = 4<script>
Modifier and Type | Method and Description |
---|---|
JsPath |
append(JsPath path)
Creates a new JsPath appending the given path to this path.
|
int |
compareTo(JsPath that)
Compares this path with another given as a parameter by comparing in order each of their positions,
one by one, until a result different from zero is returned or all the positions of any path are
consumed.
|
boolean |
contains(JsPath path)
returns true if this path contains the given path
|
boolean |
containsKey(String name)
returns true if this path contains the given key
|
JsPath |
dec()
Returns a new path decrementing the last index by one, throwing an UserError
if the last Position is not an index
|
static JsPath |
empty()
Returns the singleton empty path.
|
boolean |
endsWith(JsPath path)
returns true if this path ends with the given path.
|
boolean |
endsWithKey(String key) |
boolean |
equals(Object that)
Indicates whether some other object is "equal to" this path
|
static JsPath |
fromIndex(int i)
returns a path from an index
|
static JsPath |
fromKey(String key)
returns a path from a key
|
int |
hashCode()
Returns the hashcode of this path
|
Position |
head()
Returns the head of this path if it's not empty, throwing an exception otherwise.
|
JsPath |
inc()
Returns a new path incrementing the last index by one, throwing an UserError
if the last Position is not an index
|
JsPath |
index(int i)
Returns a new path appending an index with the given value to the back of this path.
|
JsPath |
init()
Returns a new path without the last Position of this path.
|
boolean |
isEmpty()
Returns true if the path is empty.
|
JsPath |
key(String key)
creates a new JsPath appending the key to
this JsPath. |
Position |
last()
returns the last position
this JsPath if it's not empty or an exception otherwise. |
JsPath |
mapKeys(UnaryOperator<String> map)
Creates a new JsPath applying the given map function to every key of this path.
|
static JsPath |
path(String path) |
JsPath |
prepend(JsPath path)
Creates a new JsPath prepending the given path to this path.
|
int |
size()
Returns the number of Position (keys and indexes) of
this JsPath |
boolean |
startsWith(JsPath path)
returns true if this path starts with the given path.
|
boolean |
startsWithKey(String key) |
Stream<Position> |
stream()
Returns a sequential
Stream of Positions with this path as its source. |
JsPath |
tail()
Returns a JsPath without the head of this JsPath
|
String |
toString()
Returns a string representation of this path following the format defined in the RFC 6901 with
the exception that keys which names are numbers are single-quoted.
|
public static JsPath empty()
public static JsPath fromIndex(int i)
i
- the indexpublic static JsPath fromKey(String key)
key
- the name of the keypublic JsPath key(String key)
this
JsPath.key
- the key name to be appended in raw, without encoding nor single-quoting like in path(String)
)}public int compareTo(JsPath that)
compareTo
in interface Comparable<JsPath>
that
- the given pathindex.compareTo(position)
,
key.compareTo(position)
public Position head()
UserError
- if the path is emptypublic Stream<Position> stream()
Stream
of Positions with this path as its source.public JsPath inc()
UserError
- if the last position is not an Indexpublic Position last()
this
JsPath if it's not empty or an exception otherwise.UserError
- if the JsPath is emptypublic JsPath index(int i)
i
- the value of the index to be appendedpublic JsPath init()
UserError
- if the JsPath is emptypublic boolean isEmpty()
public JsPath dec()
UserError
- if the last position is not an Indexpublic int size()
this
JsPaththis
JsPathpublic JsPath tail()
UserError
- if the JsPath is emptypublic JsPath mapKeys(UnaryOperator<String> map)
map
- the given map functionpublic JsPath append(JsPath path)
path
- the given JsPath to be appendedthis
JsPathpublic JsPath prepend(JsPath path)
path
- the given path to be prependedthis
JsPathpublic boolean startsWith(JsPath path)
path
- the given pathpublic boolean startsWithKey(String key)
public boolean endsWithKey(String key)
public boolean endsWith(JsPath path)
path
- the given pathpublic int hashCode()
public boolean equals(Object that)
public String toString()
public boolean containsKey(String name)
name
- the name of the keypublic boolean contains(JsPath path)
path
- the pathCopyright © 2022. All rights reserved.