Skip to content

Commit 826f437

Browse files
committed
Issue #178: extract filter evaluation helpers shared by both engines
JsonPathHelpers now owns resolvePropertyPath, toJsonComparable, and compareValues exactly as the interpreter evaluated them; the interpreted engine delegates to them and the bytecode-generated engine (next commit) calls the same methods, giving compiled/interpreted parity for filter semantics by construction. All 100 existing JsonPath tests pass. To verify: mvn -pl json-java21-jsonpath test -Djava.util.logging.ConsoleHandler.level=INFO (100 tests green).
1 parent 9d8a158 commit 826f437

2 files changed

Lines changed: 145 additions & 92 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package json.java21.jsonpath;
2+
3+
import jdk.incubator.java.util.json.JsonArray;
4+
import jdk.incubator.java.util.json.JsonBoolean;
5+
import jdk.incubator.java.util.json.JsonNull;
6+
import jdk.incubator.java.util.json.JsonNumber;
7+
import jdk.incubator.java.util.json.JsonObject;
8+
import jdk.incubator.java.util.json.JsonString;
9+
import jdk.incubator.java.util.json.JsonValue;
10+
11+
import java.util.List;
12+
import java.util.logging.Logger;
13+
14+
/// Evaluation helpers shared verbatim by both JsonPath engines.
15+
///
16+
/// The interpreted engine and the bytecode-generated engine (JDK 24+ `JsonPathCodegen`)
17+
/// both call these for filter-expression evaluation, so comparisons, path resolution,
18+
/// and diagnostics behave identically regardless of which engine runs a query.
19+
final class JsonPathHelpers {
20+
21+
private static final Logger LOG = Logger.getLogger(JsonPathHelpers.class.getName());
22+
23+
private JsonPathHelpers() {}
24+
25+
/// Resolves a property path against a JsonValue.
26+
/// @param current the JSON value to resolve from
27+
/// @param properties the property names, in order
28+
/// @return the resolved value, or null when any step is missing or the value is not an object
29+
static JsonValue resolvePropertyPath(JsonValue current, List<String> properties) {
30+
JsonValue value = current;
31+
for (final var prop : properties) {
32+
if (value instanceof JsonObject obj) {
33+
value = obj.asMap().get(prop);
34+
if (value == null) {
35+
return null;
36+
}
37+
} else {
38+
return null;
39+
}
40+
}
41+
return value;
42+
}
43+
44+
/// Converts a JsonValue to a comparable object for filter comparisons.
45+
/// @param value the JSON value to convert
46+
/// @return a String, Double, or Boolean; null for JSON null and other value kinds
47+
static Object toJsonComparable(JsonValue value) {
48+
if (value == null) return null;
49+
return switch (value) {
50+
case JsonString s -> s.asString();
51+
case JsonNumber n -> n.asDouble();
52+
case JsonBoolean b -> b.asBoolean();
53+
case JsonNull ignored -> null;
54+
default -> value;
55+
};
56+
}
57+
58+
/// Evaluates a filter comparison using the JsonPath engine's comparison rules.
59+
/// @param left the left operand (possibly null)
60+
/// @param op the comparison operator
61+
/// @param right the right operand (possibly null)
62+
/// @return true when the comparison is satisfied
63+
@SuppressWarnings("unchecked")
64+
static boolean compareValues(Object left, JsonPathAst.ComparisonOp op, Object right) {
65+
if (left == null || right == null) {
66+
return switch (op) {
67+
case EQ -> left == right;
68+
case NE -> left != right;
69+
default -> false;
70+
};
71+
}
72+
73+
// Try numeric comparison
74+
switch (left) {
75+
case Number leftNum when right instanceof Number rightNum -> {
76+
final double l = leftNum.doubleValue();
77+
final double r = rightNum.doubleValue();
78+
return switch (op) {
79+
case EQ -> l == r;
80+
case NE -> l != r;
81+
case LT -> l < r;
82+
case LE -> l <= r;
83+
case GT -> l > r;
84+
case GE -> l >= r;
85+
};
86+
}
87+
88+
// String comparison
89+
case String ignored when right instanceof String -> {
90+
@SuppressWarnings("rawtypes") final int cmp = ((Comparable) left).compareTo(right);
91+
return switch (op) {
92+
case EQ -> cmp == 0;
93+
case NE -> cmp != 0;
94+
case LT -> cmp < 0;
95+
case LE -> cmp <= 0;
96+
case GT -> cmp > 0;
97+
case GE -> cmp >= 0;
98+
};
99+
}
100+
101+
// Boolean comparison
102+
case Boolean ignored when right instanceof Boolean -> {
103+
return switch (op) {
104+
case EQ -> left.equals(right);
105+
case NE -> !left.equals(right);
106+
default -> false;
107+
};
108+
}
109+
default -> {
110+
}
111+
}
112+
113+
// Fallback equality
114+
return switch (op) {
115+
case EQ -> left.equals(right);
116+
case NE -> !left.equals(right);
117+
default -> false;
118+
};
119+
}
120+
121+
/// Logs an unsupported script expression in the same way the interpreter does.
122+
/// Generated executors call this so compiled queries surface the same diagnostics.
123+
/// @param script the unsupported script text
124+
static void unsupportedScript(String script) {
125+
LOG.warning(() -> "Unsupported script expression: " + script);
126+
}
127+
128+
/// Logs an unsupported recursive-descent target in the same way the interpreter does.
129+
/// @param target the unsupported target segment
130+
static void unsupportedRecursiveTarget(JsonPathAst.Segment target) {
131+
LOG.finer(() -> "Unsupported target in recursive descent: " + target);
132+
}
133+
134+
/// Logs an unsupported union selector in the same way the interpreter does.
135+
/// @param selector the unsupported selector segment
136+
static void unsupportedUnionSelector(JsonPathAst.Segment selector) {
137+
LOG.finer(() -> "Unsupported selector in union: " + selector);
138+
}
139+
}

‎json-java21-jsonpath/src/main/java/json/java21/jsonpath/JsonPathInterpreted.java‎

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ private static void evaluateFilter(
275275
private static boolean matchesFilter(JsonPathAst.FilterExpression expr, JsonValue current) {
276276
return switch (expr) {
277277
case JsonPathAst.ExistsFilter exists -> {
278-
final var value = resolvePropertyPath(exists.path(), current);
278+
final var value = JsonPathHelpers.resolvePropertyPath(current, exists.path().properties());
279279
yield value != null;
280280
}
281281
case JsonPathAst.ComparisonFilter comp -> {
282282
final var leftValue = resolveFilterExpression(comp.left(), current);
283283
final var rightValue = resolveFilterExpression(comp.right(), current);
284-
yield compareValues(leftValue, comp.op(), rightValue);
284+
yield JsonPathHelpers.compareValues(leftValue, comp.op(), rightValue);
285285
}
286286
case JsonPathAst.LogicalFilter logical -> {
287287
final var leftMatch = matchesFilter(logical.left(), current);
@@ -292,109 +292,23 @@ yield switch (logical.op()) {
292292
};
293293
}
294294
case JsonPathAst.CurrentNode ignored1 -> true;
295-
case JsonPathAst.PropertyPath path -> resolvePropertyPath(path, current) != null;
295+
case JsonPathAst.PropertyPath path -> JsonPathHelpers.resolvePropertyPath(current, path.properties()) != null;
296296
case JsonPathAst.LiteralValue ignored -> true;
297297
};
298298
}
299299

300300
private static Object resolveFilterExpression(JsonPathAst.FilterExpression expr, JsonValue current) {
301301
return switch (expr) {
302302
case JsonPathAst.PropertyPath path -> {
303-
final var value = resolvePropertyPath(path, current);
304-
yield jsonValueToComparable(value);
303+
final var value = JsonPathHelpers.resolvePropertyPath(current, path.properties());
304+
yield JsonPathHelpers.toJsonComparable(value);
305305
}
306306
case JsonPathAst.LiteralValue lit -> lit.value();
307-
case JsonPathAst.CurrentNode ignored -> jsonValueToComparable(current);
307+
case JsonPathAst.CurrentNode ignored -> JsonPathHelpers.toJsonComparable(current);
308308
default -> null;
309309
};
310310
}
311311

312-
private static JsonValue resolvePropertyPath(JsonPathAst.PropertyPath path, JsonValue current) {
313-
JsonValue value = current;
314-
for (final var prop : path.properties()) {
315-
if (value instanceof JsonObject obj) {
316-
value = obj.asMap().get(prop);
317-
if (value == null) {
318-
return null;
319-
}
320-
} else {
321-
return null;
322-
}
323-
}
324-
return value;
325-
}
326-
327-
private static Object jsonValueToComparable(JsonValue value) {
328-
if (value == null) return null;
329-
return switch (value) {
330-
case JsonString s -> s.asString();
331-
case JsonNumber n -> n.asDouble();
332-
case JsonBoolean b -> b.asBoolean();
333-
case JsonNull ignored -> null;
334-
default -> value;
335-
};
336-
}
337-
338-
@SuppressWarnings("unchecked")
339-
private static boolean compareValues(Object left, JsonPathAst.ComparisonOp op, Object right) {
340-
if (left == null || right == null) {
341-
return switch (op) {
342-
case EQ -> left == right;
343-
case NE -> left != right;
344-
default -> false;
345-
};
346-
}
347-
348-
// Try numeric comparison
349-
switch (left) {
350-
case Number leftNum when right instanceof Number rightNum -> {
351-
final double l = leftNum.doubleValue();
352-
final double r = rightNum.doubleValue();
353-
return switch (op) {
354-
case EQ -> l == r;
355-
case NE -> l != r;
356-
case LT -> l < r;
357-
case LE -> l <= r;
358-
case GT -> l > r;
359-
case GE -> l >= r;
360-
};
361-
}
362-
363-
364-
// String comparison
365-
case String ignored when right instanceof String -> {
366-
@SuppressWarnings("rawtypes") final int cmp = ((Comparable) left).compareTo(right);
367-
return switch (op) {
368-
case EQ -> cmp == 0;
369-
case NE -> cmp != 0;
370-
case LT -> cmp < 0;
371-
case LE -> cmp <= 0;
372-
case GT -> cmp > 0;
373-
case GE -> cmp >= 0;
374-
};
375-
}
376-
377-
378-
// Boolean comparison
379-
case Boolean ignored when right instanceof Boolean -> {
380-
return switch (op) {
381-
case EQ -> left.equals(right);
382-
case NE -> !left.equals(right);
383-
default -> false;
384-
};
385-
}
386-
default -> {
387-
}
388-
}
389-
390-
// Fallback equality
391-
return switch (op) {
392-
case EQ -> left.equals(right);
393-
case NE -> !left.equals(right);
394-
default -> false;
395-
};
396-
}
397-
398312
private static void evaluateUnion(
399313
JsonPathAst.Union union,
400314
List<JsonPathAst.Segment> segments,

0 commit comments

Comments
 (0)