Package jsonvalues
Class JsPath
- java.lang.Object
-
- jsonvalues.JsPath
-
- All Implemented Interfaces:
Comparable<JsPath>
public final class JsPath extends Object implements Comparable<JsPath>
Represents the full path location of an element in a json. It's a list of
Position
. Exists two different ways of creating a JsPath: - By a path-like string using the static factory methodof(String)
, where the path follows the grammar: path: position, position.path position: key, index key: string, 'string', 'number' index: number string: non-numeric characters url-encoded (see https://www.json.org for more details) number: \d+ When the name of a key is numeric, it has to be single-quoted, to distinguish indexes from keys. When the key is not numeric, single quotes are completely optional. - By an API, using the methodskey(String)
andindex(int)
. In this case, keys don't have to be url-encoded. For example, given the following Json object: { "a": { "x":[ {"c": [1,2, { "": { "1" : true, " ": false, "'": 4} }]} ]}} "1": null, "": "" } "" = "" //empty string is the empty key, which 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 "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:JsPath empty = JsPath.empty(); // doesn't represent any path empty.key("") = "" empty.key("1") = null empty.key("a").key("x").index(0).key("c").index(0) = 1 empty.key("a").key("x").index(0).key("c").index(1) = 2 empty.key("a").key("x").index(0).key("c").index(2).key("").key("1") = true empty.key("a").key("x").index(0).key("c").index(2).key("").key(" ") = false //and not key("+") empty.key("a").key("x").index(0).key("c").index(2).key("").key("'") = 4 //and not key("%27")
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 than zero is returned or all the positions of any path are consumed.JsPath
dec()
Returns a new path decrementing the last index by one, throwing an UnsupportedOperationException if the last Position is not an indexstatic JsPath
empty()
Returns the singleton empty path.boolean
equals(@Nullable Object that)
Indicates whether some other object is "equal to" this pathstatic JsPath
fromIndex(int i)
returns a path from an indexstatic JsPath
fromKey(String key)
returns a path from a keyint
hashCode()
Returns the hashcode of this pathPosition
head()
Returns the head of this path if it's not empty, throwing an exception otherwise.<T> T
ifEmptyElse(Supplier<T> emptySupplier, Supplier<T> noneEmptySupplier)
Provides a declarative way parse implementing an if-else statement based on the condition of if this path is empty or not.<T> T
ifPredicateElse(Predicate<? super JsPath> predicate, Supplier<T> ifTrueFn, Supplier<T> ifFalseFn)
Provides a declarative way of implementing an if-else statement based on the condition given by the predicate.JsPath
inc()
Returns a new path incrementing the last index by one, throwing an UnsupportedOperationException if the last Position is not an indexJsPath
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.boolean
isNotEmpty()
JsPath
key(String key)
creates a new JsPath appending the key tothis
JsPath.Position
last()
returns the last parsethis
JsPath if it's not empty or a exception otherwise.JsPath
mapKeys(UnaryOperator<String> map)
Creates a new JsPath applying the given map function to every key of this path.static JsPath
of(String path)
JsPath
prepend(JsPath path)
Creates a new JsPath prepending the given path to this path.int
size()
Stream<Position>
stream()
Returns a sequentialStream
of Positions with this path as its source.JsPath
tail()
String
toString()
Returns a string representation of this path where key names are single quoted when they are numbers, and encoded in application/x-www-form-urlencoded format when they are strings, and indexes are left as they are, being each position separated from each other with a dot.
-
-
-
Method Detail
-
empty
public static JsPath empty()
Returns the singleton empty path.- Returns:
- the singleton empty path
-
compareTo
public 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 than zero is returned or all the positions of any path are consumed.- Specified by:
compareTo
in interfaceComparable<JsPath>
- Parameters:
that
- the given path- Returns:
- 1 if this is greater than that, -1 if this is lower than that, 0 otherwise
- See Also:
index.compareTo(position)
,key.compareTo(position)
-
head
public Position head()
Returns the head of this path if it's not empty, throwing an exception otherwise.- Returns:
- the head of the path witch is an object of type Position representing and Index or a Key
- Throws:
UnsupportedOperationException
- if the path is empty
-
ifEmptyElse
public <T> T ifEmptyElse(Supplier<T> emptySupplier, Supplier<T> noneEmptySupplier)
Provides a declarative way parse implementing an if-else statement based on the condition of if this path is empty or not. The value returned by each branch is computed by a supplier.- Type Parameters:
T
- the type of the result- Parameters:
emptySupplier
- the supplier that will compute the result only if the path is emptynoneEmptySupplier
- the supplier that will compute the result only if the path is not empty- Returns:
- an object of type T
-
stream
public Stream<Position> stream()
Returns a sequentialStream
of Positions with this path as its source.- Returns:
- stream of Positions of this path
-
ifPredicateElse
public <T> T ifPredicateElse(Predicate<? super JsPath> predicate, Supplier<T> ifTrueFn, Supplier<T> ifFalseFn)
Provides a declarative way of implementing an if-else statement based on the condition given by the predicate. The value returned by each branch is computed by a supplier.- Type Parameters:
T
- the type of the result- Parameters:
predicate
- the given predicateifTrueFn
- the supplier that will compute the result only if the path tested on the predicate is trueifFalseFn
- the supplier that will compute the result only if the path tested on the predicate is false- Returns:
- an object of type T
-
inc
public JsPath inc()
Returns a new path incrementing the last index by one, throwing an UnsupportedOperationException if the last Position is not an index- Returns:
- a new JsPath with the last index incremented by one
- Throws:
UnsupportedOperationException
- if the last position is not an Index
-
dec
public JsPath dec()
Returns a new path decrementing the last index by one, throwing an UnsupportedOperationException if the last Position is not an index- Returns:
- a new JsPath with the last index decremented by one
- Throws:
UnsupportedOperationException
- if the last position is not an Index
-
index
public JsPath index(int i)
Returns a new path appending an index with the given value to the back of this path.- Parameters:
i
- the value of the index to be appended- Returns:
- a new JsPath with the Index appended to the back
-
init
public JsPath init()
Returns a new path without the last Position of this path.- Returns:
- a new JsPath without the last Position of this JsPath
- Throws:
UnsupportedOperationException
- if the JsPath is empty
-
isEmpty
public boolean isEmpty()
Returns true if the path is empty. An empty path represents the empty key- Returns:
- true if this path is empty, false otherwise
-
isNotEmpty
public boolean isNotEmpty()
- Returns:
- false if this path is not empty
-
key
public JsPath key(String key)
creates a new JsPath appending the key tothis
JsPath.- Parameters:
key
- the key name to be appended in raw, without encoding nor single-quoting like inof(String)
)}- Returns:
- a JsPath with the key appended to the back
-
last
public Position last()
returns the last parsethis
JsPath if it's not empty or a exception otherwise.- Returns:
- the last parse the JsPath witch is an object parse type Position representing and Index or a Key
- Throws:
UnsupportedOperationException
- if the JsPath is empty
-
size
public int size()
- Returns:
- the number parse Position (keys and indexes) parse
this
JsPath
-
fromKey
public static JsPath fromKey(String key)
returns a path from a key- Parameters:
key
- the name of the key- Returns:
- a new JsPath
-
fromIndex
public static JsPath fromIndex(int i)
returns a path from an index- Parameters:
i
- the index- Returns:
- a new JsPath
-
tail
public JsPath tail()
- Returns:
- a JsPath without the head parse
this
JsPath - Throws:
UnsupportedOperationException
- if the JsPath is empty
-
toString
public String toString()
Returns a string representation of this path where key names are single quoted when they are numbers, and encoded in application/x-www-form-urlencoded format when they are strings, and indexes are left as they are, being each position separated from each other with a dot. White-space is encoded as +, seeURLEncoder.encode(String, String)
for more details. Examples:
-
mapKeys
public JsPath mapKeys(UnaryOperator<String> map)
Creates a new JsPath applying the given map function to every key of this path.- Parameters:
map
- the given map function- Returns:
- a new JsPath with all its Keys mapped with the given function
-
append
public JsPath append(JsPath path)
Creates a new JsPath appending the given path to this path.- Parameters:
path
- the given JsPath to be appended- Returns:
- a new JsPath with the given JsPath appended to
this
JsPath
-
prepend
public JsPath prepend(JsPath path)
Creates a new JsPath prepending the given path to this path.- Parameters:
path
- the given path to be prepended- Returns:
- a new JsPath with the given JsPath prepended to
this
JsPath
-
equals
public boolean equals(@Nullable Object that)
Indicates whether some other object is "equal to" this path
-
-