Skip to content

Commit 3eadf51

Browse files
committed
api changes
1 parent 3aae5db commit 3eadf51

File tree

7 files changed

+163
-48
lines changed

7 files changed

+163
-48
lines changed

json-java21-jsonpath/AGENTS.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,15 @@ import jdk.sandbox.java.util.json.*;
6363
import json.java21.jsonpath.JsonPath;
6464

6565
JsonValue json = Json.parse(jsonString);
66-
List<JsonValue> results = JsonPath.query("$.store.book[*].author", json);
66+
67+
// Preferred: parse once (cache) and reuse
68+
JsonPath path = JsonPath.parse("$.store.book[*].author");
69+
List<JsonValue> results = path.query(json);
70+
71+
// If you want a static call site, pass the compiled JsonPath
72+
List<JsonValue> sameResults = JsonPath.query(path, json);
6773
```
74+
75+
Notes:
76+
- Parsing a JsonPath expression is relatively expensive compared to evaluation; cache compiled `JsonPath` instances in hot code paths.
77+
- `JsonPath.query(String, JsonValue)` is intended for one-off usage only.

json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPath.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
/// ```java
1515
/// // Fluent API (preferred)
1616
/// JsonValue json = Json.parse(jsonString);
17-
/// List<JsonValue> results = JsonPath.parse("$.store.book[*].author").select(json);
17+
/// List<JsonValue> results = JsonPath.parse("$.store.book[*].author").query(json);
1818
///
19-
/// // Static query API
20-
/// List<JsonValue> results = JsonPath.query("$.store.book[*].author", json);
19+
/// // Compiled + static call site (also reusable)
20+
/// JsonPath path = JsonPath.parse("$.store.book[*].author");
21+
/// List<JsonValue> results = JsonPath.query(path, json);
2122
/// ```
2223
///
23-
/// Based on the JSONPath specification from https://goessner.net/articles/JsonPath/
24+
/// Based on the JSONPath specification from [...](https://goessner.net/articles/JsonPath/)
2425
public final class JsonPath {
2526

2627
private static final Logger LOG = Logger.getLogger(JsonPath.class.getName());
@@ -49,9 +50,23 @@ public static JsonPath parse(String path) {
4950
/// @param json the JSON document to query
5051
/// @return a list of matching JsonValue instances (may be empty)
5152
/// @throws NullPointerException if json is null
53+
/// @deprecated Use `query(JsonValue)` (aligns with Goessner JSONPath terminology).
54+
@Deprecated(forRemoval = false)
5255
public List<JsonValue> select(JsonValue json) {
56+
return query(json);
57+
}
58+
59+
/// Queries matching values from a JSON document.
60+
///
61+
/// This is the preferred instance API: compile once via `parse(String)`, then call `query(JsonValue)`
62+
/// for each already-parsed JSON document.
63+
///
64+
/// @param json the JSON document to query
65+
/// @return a list of matching JsonValue instances (may be empty)
66+
/// @throws NullPointerException if json is null
67+
public List<JsonValue> query(JsonValue json) {
5368
Objects.requireNonNull(json, "json must not be null");
54-
LOG.fine(() -> "Selecting from document with path: " + pathExpression);
69+
LOG.fine(() -> "Querying document with path: " + pathExpression);
5570
return evaluate(ast, json);
5671
}
5772

@@ -65,14 +80,14 @@ public JsonPathAst.Root ast() {
6580
return ast;
6681
}
6782

68-
/// Evaluates a JsonPath expression against a JSON document (convenience method).
69-
/// @param path the JsonPath expression
83+
/// Evaluates a compiled JsonPath against a JSON document.
84+
/// @param path a compiled JsonPath (typically cached)
7085
/// @param json the JSON document to query
7186
/// @return a list of matching JsonValue instances (may be empty)
7287
/// @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);
88+
public static List<JsonValue> query(JsonPath path, JsonValue json) {
89+
Objects.requireNonNull(path, "path must not be null");
90+
return path.query(json);
7691
}
7792

7893
/// Evaluates a pre-parsed JsonPath AST against a JSON document.

json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathAst.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ record PropertyAccess(String name) implements Segment {
4444
}
4545
}
4646

47-
/// Access array element by index: [n] where n can be negative for reverse indexing
47+
/// Access array element by index: \[n\] where n can be negative for reverse indexing
4848
record ArrayIndex(int index) implements Segment {}
4949

50-
/// Slice array: [start:end:step]
50+
/// Slice array: \[start:end:step\]
5151
/// All fields are optional (null means not specified)
5252
record ArraySlice(Integer start, Integer end, Integer step) implements Segment {}
5353

json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/// - \[?(@.prop)\] : filter expression for existence
2222
/// - \[?(@.prop op value)\] : filter expression with comparison
2323
/// - \[(@.length-1)\] : script expression
24-
public final class JsonPathParser {
24+
final class JsonPathParser {
2525

2626
private static final Logger LOG = Logger.getLogger(JsonPathParser.class.getName());
2727

0 commit comments

Comments
 (0)