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