Skip to content

Commit 9bf8d3f

Browse files
committed
Issue #178: document interpreted and compiled JsonPath engines (docs first)
Markdown-Driven-Development: describes the two-engine contract (interpreted = JDK 21+ AST walker, compiled = JDK 24+ ClassFile API emitting Java 21 bytecode in the jtd-codegen style) before implementation, including the ahead-of-time compileBytes tooling path and the parity guarantee backed by JsonPathCompilerParityTest. To verify: read json-java21-jsonpath/README.md section 'Two Engines'.
1 parent 2e5cf37 commit 9bf8d3f

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎json-java21-jsonpath/AGENTS.md‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ This file is for contributor/agent operational notes. Read `json-java21-jsonpath
66
- The `$(command -v mvnd || command -v mvn || command -v ./mvnw)` wrapper is for local developer speed only; do not put it in user-facing docs.
77

88
Stable code entry points:
9-
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPath.java`
9+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPath.java` (public sealed interface; `parse` = interpreted engine, `compile` = compiled engine)
10+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathInterpreted.java` (AST-walking interpreter)
11+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathCompiled.java` (compiled engine facade)
12+
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathExecutor.java` (package-private functional interface implemented by generated classes)
1013
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathParser.java`
1114
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathAst.java`
1215
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathParseException.java`
1316
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathStreams.java`
1417

18+
Compiled engine (JDK 24+ only; the Maven profile `jsonpath-codegen` activates on JDK 24+ and adds `src/main/codegen/java` plus `src/test/codegen/java`):
19+
- `json-java21-jsonpath/src/main/codegen/java/json/java21/jsonpath/JsonPathCodegen.java` (ClassFile API, JEP 484; emits `ClassFile.JAVA_21_VERSION` / version 65 bytecode)
20+
- `json-java21-jsonpath/src/main/codegen/java/json/java21/jsonpath/JsonPathBytecodeEmitter.java` (AST-to-bytecode emitter)
21+
- `json-java21-jsonpath/src/test/codegen/java/json/java21/jsonpath/JsonPathCompilerParityTest.java` (interpreted == generated parity over the existing test corpus)
22+
1523
When changing syntax/behavior:
1624
- Update `JsonPathAst` + `JsonPathParser` + `JsonPath` together.
17-
- Add parser + evaluation tests; new tests should extend `JsonPathLoggingConfig`.
25+
- Mirror the change in `JsonPathBytecodeEmitter` (and `JsonPathHelpers` when shared evaluation logic moves); the parity test suite guards the interpreted/generated contract.
26+
- Add parser + evaluation tests; new tests should extend `JsonPathLoggingConfig`; add a matching parity entry for any new operator.

‎json-java21-jsonpath/README.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,47 @@ This implementation follows Goessner-style JSONPath operators, including:
6060
- `[?(@.prop)]` and `[?(@.prop op value)]` basic filters
6161
- `[(@.length-1)]` limited script support
6262

63+
## Two Engines: Interpreted and Compiled
64+
65+
This module ships the same JsonPath language with two interchangeable engines behind one API:
66+
67+
| Engine | Class | Availability | Mechanism |
68+
|---|---|---|---|
69+
| Interpreted | `JsonPath.parse(...)` result | JDK 21+ (default) | walks the `JsonPathAst` tree per query |
70+
| Compiled | `JsonPath.compile(...)` result | JDK 24+ runtime | bytecode generated with the JDK 24 ClassFile API (JEP 484), targeting Java 21 bytecode (`ClassFile.JAVA_21_VERSION`, class file version 65) |
71+
72+
Both engines implement the same `JsonPath` public surface, so callers can hand either one to the same API. The compiled engine mirrors the approach already shipped for JTD validation in [`json-java21-jtd-codegen`](../json-java21-jtd-codegen/README.md) (`JtdCodegen` / `JtdValidator.compileGenerated`).
73+
74+
### Using the Compiled Engine
75+
76+
```java
77+
// Parse once, compile for the best repeated-query performance
78+
JsonPath path = JsonPath.parse("$.store.book[*].author");
79+
80+
for (JsonValue doc : documents) {
81+
List<JsonValue> results = JsonPath.compile(path).query(doc);
82+
// process results...
83+
}
84+
```
85+
86+
Compiling an already-compiled path returns the same instance (compilation is idempotent). The compiled engine keeps the original path text (`toString()`) and the AST (`ast()`), so a compiled path is a drop-in replacement anywhere an interpreted path is accepted.
87+
88+
### Runtime Requirements
89+
90+
- **Interpreted engine**: JDK 21+; no dependency on the ClassFile API; unchanged semantics.
91+
- **Compiled engine**: generating classes requires a JDK 24+ runtime (`java.lang.classfile`, JEP 484). Calling `compile()` on JDK 21 or earlier fails with a `NoClassDefFoundError`; use the interpreted engine there. The generated classes are Java 21 bytecode and only need Java 21 API surface at query time.
92+
- **Ahead-of-time (build-time) tooling**: `JsonPathCodegen.compileBytes(...)` returns the raw generated classfile bytes without loading them, in the same style as `JtdCodegen.compileBytes(...)` for `jtd2jar`-style standalone JAR builds.
93+
94+
### Parity Guarantee
95+
96+
Compiled and interpreted engines produce identical results, in the same order, for every expression in the supported-syntax table above. The module's parity test suite (`JsonPathCompilerParityTest`) reuses the existing test corpus and asserts `compiled.query(doc)` equals `interpreted.query(doc)` element-for-element for both engines.
97+
98+
### Supported Features in Compiled Mode
99+
100+
All supported operators are supported by the compiled engine:
101+
- property access (dot and bracket), array indices (including negative), slices
102+
- wildcards, recursive descent (`$..name`, `$..*`), unions, filters with comparisons and logical operators, and the limited `[(@.length-1)]` script expression
103+
63104
## Stream-Based Functions (Aggregations)
64105

65106
Some JsonPath implementations include aggregation functions such as `$.numbers.avg()`.

‎json-java21-jtd-codegen/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ For **infrequent validation** (config loading, startup checks, one-off validatio
1010

1111
> **Future note**: `java.util.json` has entered the JDK incubator (`jdk.incubator.json`). Once the API stabilises in the JDK itself, generated bytecode validators can depend directly on future JDK classes rather than this backport, making them even more efficient with zero library overhead.
1212
13+
## Same Pattern for Other Engines
14+
15+
The compiled-engine blueprint in this module (emitters + `Descriptors` + `ClassFile.JAVA_21_VERSION` output + `Lookup.defineClass`) is reused by the JsonPath module's compiled engine: `JsonPath.parse(...)` gives the interpreted engine (always available, JDK 21+) and `JsonPath.compile(...)` gives the bytecode-generated engine (JDK 24+ runtime, Java 21 bytecode). See [`json-java21-jsonpath`](../json-java21-jsonpath/README.md#two-engines-interpreted-and-compiled) for the two-engine contract and its parity test suite.
16+
1317
## Requirements
1418

1519
- **Build**: JDK 24+ (uses `ClassFile` API, JEP 484)

0 commit comments

Comments
 (0)