1010/// JsonPath query evaluator for JSON documents.
1111/// Parses JsonPath expressions and evaluates them against JsonValue instances.
1212///
13- /// Usage example :
13+ /// Usage examples :
1414/// ```java
15+ /// // Fluent API (preferred)
1516/// JsonValue json = Json.parse(jsonString);
17+ /// List<JsonValue> results = JsonPath.parse("$.store.book[*].author").select(json);
18+ ///
19+ /// // Static query API
1620/// List<JsonValue> results = JsonPath.query("$.store.book[*].author", json);
1721/// ```
1822///
@@ -21,31 +25,61 @@ public final class JsonPath {
2125
2226 private static final Logger LOG = Logger .getLogger (JsonPath .class .getName ());
2327
24- private JsonPath () {
25- // No instantiation
28+ private final JsonPathAst .Root ast ;
29+ private final String pathExpression ;
30+
31+ private JsonPath (String pathExpression , JsonPathAst .Root ast ) {
32+ this .pathExpression = pathExpression ;
33+ this .ast = ast ;
2634 }
2735
28- /// Evaluates a JsonPath expression against a JSON document .
36+ /// Parses a JsonPath expression and returns a compiled JsonPath for reuse .
2937 /// @param path the JsonPath expression
30- /// @param json the JSON document to query
31- /// @return a list of matching JsonValue instances (may be empty)
32- /// @throws NullPointerException if path or json is null
38+ /// @return a compiled JsonPath that can be used to select from multiple documents
39+ /// @throws NullPointerException if path is null
3340 /// @throws JsonPathParseException if the path is invalid
34- public static List < JsonValue > query (String path , JsonValue json ) {
41+ public static JsonPath parse (String path ) {
3542 Objects .requireNonNull (path , "path must not be null" );
43+ LOG .fine (() -> "Parsing path: " + path );
44+ final var ast = JsonPathParser .parse (path );
45+ return new JsonPath (path , ast );
46+ }
47+
48+ /// Selects matching values from a JSON document.
49+ /// @param json the JSON document to query
50+ /// @return a list of matching JsonValue instances (may be empty)
51+ /// @throws NullPointerException if json is null
52+ public List <JsonValue > select (JsonValue json ) {
3653 Objects .requireNonNull (json , "json must not be null" );
54+ LOG .fine (() -> "Selecting from document with path: " + pathExpression );
55+ return evaluate (ast , json );
56+ }
3757
38- LOG .fine (() -> "Querying path: " + path );
58+ /// Returns the original path expression.
59+ public String expression () {
60+ return pathExpression ;
61+ }
3962
40- final var ast = JsonPathParser .parse (path );
41- return evaluate (ast , json );
63+ /// Returns the parsed AST.
64+ public JsonPathAst .Root ast () {
65+ return ast ;
66+ }
67+
68+ /// Evaluates a JsonPath expression against a JSON document (convenience method).
69+ /// @param path the JsonPath expression
70+ /// @param json the JSON document to query
71+ /// @return a list of matching JsonValue instances (may be empty)
72+ /// @throws NullPointerException if path or json is null
73+ /// @throws JsonPathParseException if the path is invalid
74+ public static List <JsonValue > query (String path , JsonValue json ) {
75+ return parse (path ).select (json );
4276 }
4377
4478 /// Evaluates a pre-parsed JsonPath AST against a JSON document.
4579 /// @param ast the parsed JsonPath AST
4680 /// @param json the JSON document to query
4781 /// @return a list of matching JsonValue instances (may be empty)
48- public static List <JsonValue > evaluate (JsonPathAst .Root ast , JsonValue json ) {
82+ static List <JsonValue > evaluate (JsonPathAst .Root ast , JsonValue json ) {
4983 Objects .requireNonNull (ast , "ast must not be null" );
5084 Objects .requireNonNull (json , "json must not be null" );
5185
0 commit comments