You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9bf8d3f
Browse filesBrowse the repository at this point in the historyBrowse files
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'.
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)
-`json-java21-jsonpath/src/test/codegen/java/json/java21/jsonpath/JsonPathCompilerParityTest.java` (interpreted == generated parity over the existing test corpus)
- 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.
-`[?(@.prop)]` and `[?(@.prop op value)]` basic filters
61
61
-`[(@.length-1)]` limited script support
62
62
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
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
+
63
104
## Stream-Based Functions (Aggregations)
64
105
65
106
Some JsonPath implementations include aggregation functions such as `$.numbers.avg()`.
> **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.
12
12
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
+
13
17
## Requirements
14
18
15
19
-**Build**: JDK 24+ (uses `ClassFile` API, JEP 484)
0 commit comments