Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
for k in totals: totals[k]+=int(r.get(k,'0'))
except Exception:
pass
exp_tests=2038
exp_tests=2108
exp_skipped=0
if totals['tests']!=exp_tests or totals['skipped']!=exp_skipped:
print(f"Unexpected test totals: {totals} != expected tests={exp_tests}, skipped={exp_skipped}")
Expand Down
14 changes: 12 additions & 2 deletions json-java21-jsonpath/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ This file is for contributor/agent operational notes. Read `json-java21-jsonpath
- 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.

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

Compiled engine (JDK 24+ only): on JDK 21, the Maven profile `jsonpath-codegen` does not activate and `JsonPathCodegen`/`JsonPathBytecodeEmitter` plus the parity tests are excluded from compilation; on JDK 24+ they are compiled at release 24 (mirroring the jtd-codegen parent profile):
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathCodegen.java` (entry point; ClassFile API, JEP 484; emits `ClassFile.JAVA_21_VERSION` / version 65 bytecode)
- `json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathBytecodeEmitter.java` (AST-to-bytecode emitter)
- `json-java21-jsonpath/src/test/java/json/java21/jsonpath/JsonPathCompilerParityTest.java` (interpreted == generated parity over the existing test corpus)
- `json-java21-jsonpath/src/test/java/json/java21/jsonpath/JsonPathCodegenTest.java` (codegen mechanics: facade, idempotence, classfile version)

When changing syntax/behavior:
- Update `JsonPathAst` + `JsonPathParser` + `JsonPath` together.
- Add parser + evaluation tests; new tests should extend `JsonPathLoggingConfig`.
- Mirror the change in `JsonPathBytecodeEmitter` (call sites of shared `JsonPathHelpers` evaluation) (and `JsonPathHelpers` when shared evaluation logic moves); the parity test suite guards the interpreted/generated contract.
- Add parser + evaluation tests; new tests should extend `JsonPathLoggingConfig`; add a matching parity entry for any new operator.
44 changes: 44 additions & 0 deletions json-java21-jsonpath/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,50 @@ This implementation follows Goessner-style JSONPath operators, including:
- `[?(@.prop)]` and `[?(@.prop op value)]` basic filters
- `[(@.length-1)]` limited script support

## Two Engines: Interpreted and Compiled

This module ships the same JsonPath language with two interchangeable engines behind one API:

| Engine | Class | Availability | Mechanism |
|---|---|---|---|
| Interpreted | `JsonPath.parse(...)` result | JDK 21+ (default) | walks the `JsonPathAst` tree per query |
| Compiled | `JsonPathCodegen.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) |

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`).

### Using the Compiled Engine

```java
// Parse once, compile for the best repeated-query performance (JDK 24+)
JsonPath path = JsonPathCodegen.compile(JsonPath.parse("$.store.book[*].author"));

for (JsonValue doc : documents) {
List<JsonValue> results = path.query(doc);
// process results...
}
```

`JsonPathCodegen.compile(...)` mirrors `JtdValidator.compileGenerated(...)`: it accepts any `JsonPath`, returns a `JsonPath` implementing the same public surface, and is idempotent (compiling a compiled path returns it unchanged).

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.

### Runtime Requirements

- **Interpreted engine**: JDK 21+; no dependency on the ClassFile API; unchanged semantics.
- **Compiled engine**: generating classes requires a JDK 24+ runtime (`java.lang.classfile`, JEP 484) because the generator is compiled into the artifact only on JDK 24+. The generated classes are Java 21 bytecode and only need Java 21 API surface at query time.
- **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. Generated executor classes are always emitted into the `json.java21.jsonpath` package so they can implement the package-private executor interface.
- **Diagnostics**: semantics and result order are identical between the engines. One deliberate, exact difference: when an expression carries an unsupported recursive-descent target or union selector, the interpreted engine logs it at `FINER` for every visited node, while the compiled engine logs it once at compile time; unsupported script expressions log `WARNING` at query time in both engines.

### Parity Guarantee

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.

### Supported Features in Compiled Mode

All supported operators are supported by the compiled engine:
- property access (dot and bracket), array indices (including negative), slices
- wildcards, recursive descent (`$..name`, `$..*`), unions, filters with comparisons and logical operators, and the limited `[(@.length-1)]` script expression

## Stream-Based Functions (Aggregations)

Some JsonPath implementations include aggregation functions such as `$.numbers.avg()`.
Expand Down
104 changes: 104 additions & 0 deletions json-java21-jsonpath/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@
<arg>-Xdiags:verbose</arg>
</compilerArgs>
</configuration>
<executions>
<!-- The compiled-engine sources require the JDK 24+ ClassFile API
(JEP 484); the jsonpath-codegen profile compiles them at
release 24. Keep them out of the default release-21 rounds. -->
<execution>
<id>default-compile</id>
<configuration>
<excludes>
<exclude>json/java21/jsonpath/JsonPathCodegen.java</exclude>
<exclude>json/java21/jsonpath/JsonPathBytecodeEmitter.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<configuration>
<testExcludes>
<testExclude>json/java21/jsonpath/JsonPathCodegenTest.java</testExclude>
<testExclude>json/java21/jsonpath/JsonPathCompilerParityTest.java</testExclude>
</testExcludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -80,6 +103,87 @@
<argLine>-ea</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<!-- The compiled-engine sources require the JDK 24+ ClassFile API
and are only on JDK 21 via the sourceFileExcludes list AND an
empty sourcepath: javadoc completes each documented package by
re-scanning -sourcepath, which would otherwise pick up the
excluded files again. The jsonpath-codegen profile (JDK 24+)
overrides both settings and documents the codegen classes
with release 24 instead. -->
<sourcepath>${project.build.directory}/javadoc21-sourcepath-placeholder</sourcepath>
<sourceFileExcludes>
<sourceFileExclude>json/java21/jsonpath/JsonPathCodegen.java</sourceFileExclude>
<sourceFileExclude>json/java21/jsonpath/JsonPathBytecodeEmitter.java</sourceFileExclude>
</sourceFileExcludes>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<!-- JsonPath compiled-engine sources require JDK 24+ (ClassFile API, JEP 484).
On JDK 21 the module builds the interpreted engine only. On JDK 24+ the
bytecode generator (JsonPathCodegen, JsonPathBytecodeEmitter) and the
parity tests are compiled at release 24, mirroring the jtd-codegen
parent profile; the interpreted engine stays at release 21. -->
<profile>
<id>jsonpath-codegen</id>
<activation>
<jdk>[24,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<!-- Keep the interpreted engine and its tests at Java 21. -->
<!-- Compile the ClassFile API codegen and the parity tests at release 24. -->
<execution>
<id>compile-codegen</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>24</release>
<includes>
<include>json/java21/jsonpath/JsonPathCodegen.java</include>
<include>json/java21/jsonpath/JsonPathBytecodeEmitter.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>compile-codegen-tests</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<release>24</release>
<testIncludes>
<testInclude>json/java21/jsonpath/JsonPathCodegenTest.java</testInclude>
<testInclude>json/java21/jsonpath/JsonPathCompilerParityTest.java</testInclude>
</testIncludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<!-- JDK 24+: document the compiled-engine classes too; the
ClassFile API exists at release 24, so no sourcepath
placeholder or source file excludes are needed here. -->
<release>24</release>
<sourceFileExcludes combine.self="override"/>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Loading
Loading