Package ca.marcusdunn.jsonlens.path.parser
@NullMarked
package ca.marcusdunn.jsonlens.path.parser
The RFC 9535 JSONPath parser: query text to a validated
JsonPathQuery.
Quick start
JsonPathParser parser = JsonPathParser.standard(); // immutable and thread-safe
Result<JsonPathQuery, ParseError> result = parser.parse("$.store.book[0].title");
String text = result.map(JsonPathQuery::toString).orElse("not valid");
// text: "$['store']['book'][0]['title']" (shorthand forms become bracket notation)
Errors
The parser does not throw exceptions. A query that is not well-formed or not valid gives a
ParseError. Match the errors that need special handling, and use ParseError.message() for
all others:
String describe = switch (JsonPathParser.standard().parse("$[?length(@.*) > 3]")) {
case Result.Ok(JsonPathQuery query) -> "valid: " + query;
case Result.Err(ParseError.NonSingularQuery(int position)) ->
"@.* can select more than one node, at position " + position;
case Result.Err(ParseError.UnknownFunction(int position, String name)) ->
"no function " + name;
case Result.Err(ParseError error) -> error.message(); // all other errors
};
// describe: "@.* can select more than one node, at position 10"
Types
| Type | Purpose |
|---|---|
JsonPathParser |
parses query text or UTF-8 bytes |
ParseError |
the reason why a query is not well-formed or not valid |
FunctionRegistrationError |
the reason why a set of function extensions is not valid |
-
ClassDescriptionAn error in the function signatures given to
JsonPathParser.withFunctions(List).Two functions have the same name.The name does not agree with the grammar rulefunction-name.Makes aJsonPathQueryfrom query text (RFC 9535).The reason why a query is not well-formed or not valid (RFC 9535, Section 2.1).An argument does not agree with the declared type of its parameter (Section 2.4.3).A string literal contains an unescaped control character, U+0000 to U+001F (Section 2.3.1.1).An index or a slice parameter is outside the I-JSON range (Section 2.1).A string literal contains an escape that is not permitted (Section 2.3.1.1).An integer has a leading zero, or is-0where only an int is permitted.The bytes are not well-formed UTF-8 (Section 2.1).A literal is a test expression.The query has too many nested levels (Section 4.1).A query that is not singular is a side of a comparison, or a ValueType argument (Sections 2.3.5.1 and 2.4.3).A function with the result type LogicalType or NodesType is a side of a comparison (Section 2.4.3).A number literal has an exponent that is too large to represent.The grammar does not permit the character at this position.The query ends before it is complete.The query calls a function that is not registered (Section 2.4).The query contains an unpaired surrogate, or a backslash-u escape for one (Sections 1.1 and 2.3.1.1).A function with the result type ValueType is a test expression (Section 2.4.3).A function call has the wrong number of arguments (Section 2.4.3).