compile

fun compile(query: String, limits: JsonPathLimits = JsonPathLimits.DEFAULT): JsonPathQuery

Compiles query, or reports where it is wrong.

compile rather than parse, because the name says the object is worth keeping: the whole cost of a JSONPath query is here, and applying a compiled one to a document touches no grammar at all.

Strict, on purpose. RFC 9535's 247 invalid-selector cases are each rejected with a position — $.a[], $[01], $[?@.a]], $['a'"b'], length(@.a, @.b). A lenient reader is what makes two implementations disagree later, and this one is read by nobody but the caller who wrote the query, so there is no compatibility to buy with leniency.

Two limits apply to the query itself, and neither can cost an answer because both are bounds on what the caller wrote: at most 1024 selectors, and at most 64 levels of nested filters, parentheses and function calls.

limits is a different kind of bound and bounds a different thing. Those two say how large the expression may be; JsonPathLimits says how much one evaluation of it may cost, which is the gap a small valid query over a large document walks straight through — $..*..* is eleven characters and quadratic. Exceeding it raises JsonPathLimitExceededException rather than returning a short nodelist. The defaults are a backstop sized so no honest query meets them; a caller compiling expressions it does not trust should set its own and set them far lower.

A regular expression is not one of the things this refuses. match and search take an RFC 9485 I-Regexp, and §2.4.6 rules that a second argument which is not one makes the result LogicalFalse — so $[?match(@.a, '[')] compiles, and selects nothing. A literal pattern is compiled here all the same, so that applying the query touches no grammar.

Parameters

limits

what one evaluation of the compiled query may cost. See JsonPathLimits.

Throws

if query is not a valid JSONPath query, with the offending position, or if it exceeds either limit.