Skip to content

Commit 3aae5db

Browse files
committed
tidy
1 parent 33c39a1 commit 3aae5db

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// - Filter: filter by predicate (e.g., [?(@.isbn)] or [?(@.price<10)])
1717
/// - Union: multiple indices or names (e.g., [0,1] or ['a','b'])
1818
/// - ScriptExpression: computed index (e.g., [(@.length-1)])
19-
public sealed interface JsonPathAst {
19+
sealed interface JsonPathAst {
2020

2121
/// Root element ($) - the starting point of all JsonPath expressions
2222
record Root(List<Segment> segments) implements JsonPathAst {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
/// Exception thrown when a JsonPath expression cannot be parsed.
44
/// This is a runtime exception as JsonPath parsing failures are typically programming errors.
5-
@SuppressWarnings("serial")
65
public class JsonPathParseException extends RuntimeException {
76

7+
private static final long serialVersionUID = 1L;
8+
89
private final int position;
910
private final String path;
1011

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
/// Parser for JsonPath expressions into AST.
99
/// Implements a recursive descent parser for JsonPath syntax.
1010
///
11-
/// Supported syntax based on https://goessner.net/articles/JsonPath/:
11+
/// Supported syntax based on [...](https://goessner.net/articles/JsonPath/):
1212
/// - $ : root element
1313
/// - .name : child property access
14-
/// - ['name'] or ["name"] : bracket notation property access
15-
/// - [n] : array index (supports negative indices)
16-
/// - [start:end:step] : array slice
17-
/// - [*] or .* : wildcard
14+
/// - \['name'\] or \["name"\] : bracket notation property access
15+
/// - \[n\] : array index (supports negative indices)
16+
/// - \[start:end:step\] : array slice
17+
/// - \[*\] or .* : wildcard
1818
/// - .. : recursive descent
19-
/// - [n,m] : union of indices
20-
/// - ['a','b'] : union of properties
21-
/// - [?(@.prop)] : filter expression for existence
22-
/// - [?(@.prop op value)] : filter expression with comparison
23-
/// - [(@.length-1)] : script expression
19+
/// - \[n,m\] : union of indices
20+
/// - \['a','b'\] : union of properties
21+
/// - \[?(@.prop)\] : filter expression for existence
22+
/// - \[?(@.prop op value)\] : filter expression with comparison
23+
/// - \[(@.length-1)\] : script expression
2424
public final class JsonPathParser {
2525

2626
private static final Logger LOG = Logger.getLogger(JsonPathParser.class.getName());
@@ -335,10 +335,9 @@ private JsonPathAst.LiteralValue parseLiteralNumber() {
335335

336336
// Check for decimal point
337337
if (pos < path.length() && path.charAt(pos) == '.') {
338-
pos++;
339-
while (pos < path.length() && Character.isDigit(path.charAt(pos))) {
338+
do {
340339
pos++;
341-
}
340+
} while (pos < path.length() && Character.isDigit(path.charAt(pos)));
342341
}
343342

344343
final var numStr = path.substring(start, pos);
@@ -464,7 +463,7 @@ private JsonPathAst.Segment parseNumberOrSliceOrUnion() {
464463
boolean hasColon = false;
465464
boolean hasComma = false;
466465

467-
// Parse first element (may be empty for [:end])
466+
// Parse first element (maybe empty for [:end])
468467
if (path.charAt(pos) == ':') {
469468
elements.add(null); // empty start
470469
hasColon = true;
@@ -511,7 +510,7 @@ private JsonPathAst.Segment parseNumberOrSliceOrUnion() {
511510
// Determine what we parsed
512511
if (hasColon) {
513512
// It's a slice [start:end:step]
514-
final Integer start = elements.size() > 0 ? elements.get(0) : null;
513+
final Integer start = !elements.isEmpty() ? elements.get(0) : null;
515514
final Integer end = elements.size() > 1 ? elements.get(1) : null;
516515
final Integer step = elements.size() > 2 ? elements.get(2) : null;
517516
return new JsonPathAst.ArraySlice(start, end, step);

json-java21-jsonpath/src/test/java/json/java21/jsonpath/JsonPathGoessnerTest.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
import org.junit.jupiter.api.BeforeAll;
55
import org.junit.jupiter.api.Test;
66

7-
import java.util.List;
87
import java.util.logging.Logger;
98

109
import static org.assertj.core.api.Assertions.assertThat;
1110

12-
/// Tests for JsonPath based on examples from https://goessner.net/articles/JsonPath/
11+
/// Tests for JsonPath based on examples from [...](https://goessner.net/articles/JsonPath/)
1312
/// This test class uses the sample JSON document from the article.
1413
class JsonPathGoessnerTest extends JsonPathLoggingConfig {
1514

@@ -36,14 +35,14 @@ class JsonPathGoessnerTest extends JsonPathLoggingConfig {
3635
"category": "fiction",
3736
"author": "Herman Melville",
3837
"title": "Moby Dick",
39-
"isbn": "0-553-21311-3",
38+
"ISBN": "0-553-21311-3",
4039
"price": 8.99
4140
},
4241
{
4342
"category": "fiction",
4443
"author": "J. R. R. Tolkien",
4544
"title": "The Lord of the Rings",
46-
"isbn": "0-395-19395-8",
45+
"ISBN": "0-395-19395-8",
4746
"price": 22.99
4847
}
4948
],
@@ -89,7 +88,7 @@ void testNestedProperty() {
8988
assertThat(results.getFirst()).isInstanceOf(JsonObject.class);
9089
final var bicycle = (JsonObject) results.getFirst();
9190
assertThat(bicycle.members().get("color")).isInstanceOf(JsonString.class);
92-
assertThat(((JsonString) bicycle.members().get("color")).string()).isEqualTo("red");
91+
assertThat(bicycle.members().get("color").string()).isEqualTo("red");
9392
}
9493

9594
// ========== Goessner Article Examples ==========
@@ -100,7 +99,7 @@ void testAuthorsOfAllBooks() {
10099
final var results = JsonPath.query("$.store.book[*].author", storeJson);
101100
assertThat(results).hasSize(4);
102101
final var authors = results.stream()
103-
.map(v -> ((JsonString) v).string())
102+
.map(JsonValue::string)
104103
.toList();
105104
assertThat(authors).containsExactly(
106105
"Nigel Rees",
@@ -116,7 +115,7 @@ void testAllAuthorsRecursive() {
116115
final var results = JsonPath.query("$..author", storeJson);
117116
assertThat(results).hasSize(4);
118117
final var authors = results.stream()
119-
.map(v -> ((JsonString) v).string())
118+
.map(JsonValue::string)
120119
.toList();
121120
assertThat(authors).containsExactlyInAnyOrder(
122121
"Nigel Rees",
@@ -139,7 +138,7 @@ void testAllPricesInStore() {
139138
final var results = JsonPath.query("$.store..price", storeJson);
140139
assertThat(results).hasSize(5); // 4 book prices + 1 bicycle price
141140
final var prices = results.stream()
142-
.map(v -> ((JsonNumber) v).toDouble())
141+
.map(JsonValue::toDouble)
143142
.toList();
144143
assertThat(prices).containsExactlyInAnyOrder(8.95, 12.99, 8.99, 22.99, 19.95);
145144
}
@@ -150,7 +149,7 @@ void testThirdBook() {
150149
final var results = JsonPath.query("$..book[2]", storeJson);
151150
assertThat(results).hasSize(1);
152151
final var book = (JsonObject) results.getFirst();
153-
assertThat(((JsonString) book.members().get("title")).string()).isEqualTo("Moby Dick");
152+
assertThat(book.members().get("title").string()).isEqualTo("Moby Dick");
154153
}
155154

156155
@Test
@@ -159,7 +158,7 @@ void testLastBookScriptExpression() {
159158
final var results = JsonPath.query("$..book[(@.length-1)]", storeJson);
160159
assertThat(results).hasSize(1);
161160
final var book = (JsonObject) results.getFirst();
162-
assertThat(((JsonString) book.members().get("title")).string()).isEqualTo("The Lord of the Rings");
161+
assertThat(book.members().get("title").string()).isEqualTo("The Lord of the Rings");
163162
}
164163

165164
@Test
@@ -168,7 +167,7 @@ void testLastBookSlice() {
168167
final var results = JsonPath.query("$..book[-1:]", storeJson);
169168
assertThat(results).hasSize(1);
170169
final var book = (JsonObject) results.getFirst();
171-
assertThat(((JsonString) book.members().get("title")).string()).isEqualTo("The Lord of the Rings");
170+
assertThat(book.members().get("title").string()).isEqualTo("The Lord of the Rings");
172171
}
173172

174173
@Test
@@ -177,7 +176,7 @@ void testFirstTwoBooksUnion() {
177176
final var results = JsonPath.query("$..book[0,1]", storeJson);
178177
assertThat(results).hasSize(2);
179178
final var titles = results.stream()
180-
.map(v -> ((JsonString) ((JsonObject) v).members().get("title")).string())
179+
.map(v -> v.members().get("title").string())
181180
.toList();
182181
assertThat(titles).containsExactly("Sayings of the Century", "Sword of Honour");
183182
}
@@ -188,7 +187,7 @@ void testFirstTwoBooksSlice() {
188187
final var results = JsonPath.query("$..book[:2]", storeJson);
189188
assertThat(results).hasSize(2);
190189
final var titles = results.stream()
191-
.map(v -> ((JsonString) ((JsonObject) v).members().get("title")).string())
190+
.map(v -> v.members().get("title").string())
192191
.toList();
193192
assertThat(titles).containsExactly("Sayings of the Century", "Sword of Honour");
194193
}
@@ -199,7 +198,7 @@ void testBooksWithIsbn() {
199198
final var results = JsonPath.query("$..book[?(@.isbn)]", storeJson);
200199
assertThat(results).hasSize(2);
201200
final var titles = results.stream()
202-
.map(v -> ((JsonString) ((JsonObject) v).members().get("title")).string())
201+
.map(v -> v.members().get("title").string())
203202
.toList();
204203
assertThat(titles).containsExactlyInAnyOrder("Moby Dick", "The Lord of the Rings");
205204
}
@@ -210,7 +209,7 @@ void testBooksCheaperThan10() {
210209
final var results = JsonPath.query("$..book[?(@.price<10)]", storeJson);
211210
assertThat(results).hasSize(2);
212211
final var titles = results.stream()
213-
.map(v -> ((JsonString) ((JsonObject) v).members().get("title")).string())
212+
.map(v -> v.members().get("title").string())
214213
.toList();
215214
assertThat(titles).containsExactlyInAnyOrder("Sayings of the Century", "Moby Dick");
216215
}
@@ -232,7 +231,7 @@ void testArrayIndexFirst() {
232231
final var results = JsonPath.query("$.store.book[0]", storeJson);
233232
assertThat(results).hasSize(1);
234233
final var book = (JsonObject) results.getFirst();
235-
assertThat(((JsonString) book.members().get("author")).string()).isEqualTo("Nigel Rees");
234+
assertThat(book.members().get("author").string()).isEqualTo("Nigel Rees");
236235
}
237236

238237
@Test
@@ -241,7 +240,7 @@ void testArrayIndexNegative() {
241240
final var results = JsonPath.query("$.store.book[-1]", storeJson);
242241
assertThat(results).hasSize(1);
243242
final var book = (JsonObject) results.getFirst();
244-
assertThat(((JsonString) book.members().get("author")).string()).isEqualTo("J. R. R. Tolkien");
243+
assertThat(book.members().get("author").string()).isEqualTo("J. R. R. Tolkien");
245244
}
246245

247246
@Test
@@ -250,7 +249,7 @@ void testBracketNotationProperty() {
250249
final var results = JsonPath.query("$['store']['book'][0]", storeJson);
251250
assertThat(results).hasSize(1);
252251
final var book = (JsonObject) results.getFirst();
253-
assertThat(((JsonString) book.members().get("author")).string()).isEqualTo("Nigel Rees");
252+
assertThat(book.members().get("author").string()).isEqualTo("Nigel Rees");
254253
}
255254

256255
@Test
@@ -280,7 +279,7 @@ void testSliceWithStep() {
280279
final var results = JsonPath.query("$.store.book[0:4:2]", storeJson);
281280
assertThat(results).hasSize(2); // books at index 0 and 2
282281
final var titles = results.stream()
283-
.map(v -> ((JsonString) ((JsonObject) v).members().get("title")).string())
282+
.map(v -> v.members().get("title").string())
284283
.toList();
285284
assertThat(titles).containsExactly("Sayings of the Century", "Moby Dick");
286285
}
@@ -290,7 +289,7 @@ void testDeepNestedAccess() {
290289
LOG.info(() -> "TEST: testDeepNestedAccess - $.store.book[0].title");
291290
final var results = JsonPath.query("$.store.book[0].title", storeJson);
292291
assertThat(results).hasSize(1);
293-
assertThat(((JsonString) results.getFirst()).string()).isEqualTo("Sayings of the Century");
292+
assertThat(results.getFirst().string()).isEqualTo("Sayings of the Century");
294293
}
295294

296295
@Test
@@ -314,7 +313,7 @@ void testFilterGreaterThan() {
314313
final var results = JsonPath.query("$..book[?(@.price>20)]", storeJson);
315314
assertThat(results).hasSize(1);
316315
final var book = (JsonObject) results.getFirst();
317-
assertThat(((JsonString) book.members().get("title")).string()).isEqualTo("The Lord of the Rings");
316+
assertThat(book.members().get("title").string()).isEqualTo("The Lord of the Rings");
318317
}
319318

320319
@Test
@@ -351,7 +350,7 @@ void testFluentApiReusable() {
351350
""");
352351
final var simpleResults = compiledPath.select(simpleDoc);
353352
assertThat(simpleResults).hasSize(1);
354-
assertThat(((JsonNumber) simpleResults.getFirst()).toDouble()).isEqualTo(99.99);
353+
assertThat(simpleResults.getFirst().toDouble()).isEqualTo(99.99);
355354
}
356355

357356
@Test

json-java21-jsonpath/src/test/java/json/java21/jsonpath/JsonPathParserTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import org.junit.jupiter.params.ParameterizedTest;
77
import org.junit.jupiter.params.provider.ValueSource;
88

9-
import java.util.List;
109
import java.util.logging.Logger;
1110

1211
import static org.assertj.core.api.Assertions.assertThat;
1312
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1413

1514
/// Unit tests for JsonPathParser - tests parsing of JsonPath strings to AST
16-
/// Based on examples from https://goessner.net/articles/JsonPath/
15+
/// Based on examples from [...](https://goessner.net/articles/JsonPath/)
1716
class JsonPathParserTest extends JsonPathLoggingConfig {
1817

1918
private static final Logger LOG = Logger.getLogger(JsonPathParserTest.class.getName());
@@ -228,7 +227,7 @@ void testParseUnionProperties() {
228227

229228
@Test
230229
void testParseFilterExists() {
231-
LOG.info(() -> "TEST: testParseFilterExists - parse $..book[?(@.isbn)] (books with isbn)");
230+
LOG.info(() -> "TEST: testParseFilterExists - parse $..book[?(@.isbn)] (books with ISBN)");
232231
final var ast = JsonPathParser.parse("$..book[?(@.isbn)]");
233232
assertThat(ast.segments()).hasSize(2);
234233
assertThat(ast.segments().get(1)).isInstanceOf(JsonPathAst.Filter.class);
@@ -335,13 +334,13 @@ public static void main(String[] args) {
335334
{ "category": "fiction",
336335
"author": "Herman Melville",
337336
"title": "Moby Dick",
338-
"isbn": "0-553-21311-3",
337+
"ISBN": "0-553-21311-3",
339338
"price": 8.99
340339
},
341340
{ "category": "fiction",
342341
"author": "J. R. R. Tolkien",
343342
"title": "The Lord of the Rings",
344-
"isbn": "0-395-19395-8",
343+
"ISBN": "0-395-19395-8",
345344
"price": 22.99
346345
}
347346
],

0 commit comments

Comments
 (0)