|
2 | 2 |
|
3 | 3 | Early access to the unstable `java.util.json` API - taken from OpenJDK sandbox July 2025. |
4 | 4 |
|
5 | | -## JSON Schema Validator |
| 5 | +## Quick Start |
6 | 6 |
|
7 | | -A simple JSON Schema (2020-12 subset) validator is included (module: json-java21-schema). |
| 7 | +### Parsing JSON to Maps and Objects |
8 | 8 |
|
9 | | -Quick example: |
| 9 | +```java |
| 10 | +// Parse JSON string to generic structure |
| 11 | +String json = "{\"name\":\"Alice\",\"age\":30,\"active\":true}"; |
| 12 | +JsonValue value = Json.parse(json); |
| 13 | + |
| 14 | +// Access as map-like structure |
| 15 | +JsonObject obj = (JsonObject) value; |
| 16 | +String name = ((JsonString) obj.members().get("name")).value(); |
| 17 | +int age = ((JsonNumber) obj.members().get("age")).intValue(); |
| 18 | +boolean active = ((JsonBoolean) obj.members().get("active")).value(); |
| 19 | +``` |
| 20 | + |
| 21 | +### Record Mapping |
10 | 22 |
|
11 | 23 | ```java |
12 | | -var schema = io.github.simbo1905.json.schema.JsonSchema.compile( |
13 | | - jdk.sandbox.java.util.json.Json.parse(""" |
14 | | - {"type":"object","properties":{"name":{"type":"string"}},"required":["name"]} |
15 | | - """)); |
16 | | -var result = schema.validate( |
17 | | - jdk.sandbox.java.util.json.Json.parse("{\"name\":\"Alice\"}") |
| 24 | +// Define records for structured data |
| 25 | +record User(String name, int age, boolean active) {} |
| 26 | + |
| 27 | +// Parse JSON directly to records |
| 28 | +String userJson = "{\"name\":\"Bob\",\"age\":25,\"active\":false}"; |
| 29 | +JsonObject jsonObj = (JsonObject) Json.parse(userJson); |
| 30 | + |
| 31 | +// Map to record |
| 32 | +User user = new User( |
| 33 | + ((JsonString) jsonObj.members().get("name")).value(), |
| 34 | + ((JsonNumber) jsonObj.members().get("age")).intValue(), |
| 35 | + ((JsonBoolean) jsonObj.members().get("active")).value() |
18 | 36 | ); |
19 | | -// result.valid() => true |
20 | | -``` |
21 | 37 |
|
22 | | -Compatibility: we run the official JSON Schema Test Suite on verify; in strict mode it currently passes about 71% of applicable cases. |
| 38 | +// Convert records back to JSON |
| 39 | +JsonValue backToJson = Json.fromUntyped(Map.of( |
| 40 | + "name", user.name(), |
| 41 | + "age", user.age(), |
| 42 | + "active", user.active() |
| 43 | +)); |
| 44 | +``` |
23 | 45 |
|
24 | 46 | ## Back Port Project Goals |
25 | 47 |
|
@@ -57,6 +79,25 @@ This is a simplified backport with the following changes from the original: |
57 | 79 |
|
58 | 80 | These vulnerabilities exist in the upstream OpenJDK sandbox implementation and are reported here for transparency. |
59 | 81 |
|
| 82 | +## JSON Schema Validator |
| 83 | + |
| 84 | +By including a basic schema validator good enough to validate simple JSON RPC 2.0 for MCP communication, this demonstrates how to build a realistic feature out of the core API. To demonstrate the power of the core API, it follows Data Oriented Programming principles: it parses JSON Schema into an immutable structure of records, then for validation it parses the JSON to the generic structure and uses the thread-safe parsed schema as the model to validate the JSON being checked. |
| 85 | + |
| 86 | +A simple JSON Schema (2020-12 subset) validator is included (module: json-java21-schema). |
| 87 | + |
| 88 | +```java |
| 89 | +var schema = io.github.simbo1905.json.schema.JsonSchema.compile( |
| 90 | + jdk.sandbox.java.util.json.Json.parse(""" |
| 91 | + {"type":"object","properties":{"name":{"type":"string"}},"required":["name"]} |
| 92 | + """)); |
| 93 | +var result = schema.validate( |
| 94 | + jdk.sandbox.java.util.json.Json.parse("{\"name\":\"Alice\"}") |
| 95 | +); |
| 96 | +// result.valid() => true |
| 97 | +``` |
| 98 | + |
| 99 | +Compatibility: we run the official JSON Schema Test Suite on verify; in strict mode it currently passes about 71% of applicable cases. |
| 100 | + |
60 | 101 | ## Building |
61 | 102 |
|
62 | 103 | Requires JDK 21 or later. Build with Maven: |
|
0 commit comments