Skip to content

Commit 8399a59

Browse files
committed
docs: restructure README with standard usage examples at top and JSON Schema validator below the fold
1 parent dc9bade commit 8399a59

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

README.md

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,46 @@
22

33
Early access to the unstable `java.util.json` API - taken from OpenJDK sandbox July 2025.
44

5-
## JSON Schema Validator
5+
## Quick Start
66

7-
A simple JSON Schema (2020-12 subset) validator is included (module: json-java21-schema).
7+
### Parsing JSON to Maps and Objects
88

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
1022

1123
```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()
1836
);
19-
// result.valid() => true
20-
```
2137

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+
```
2345

2446
## Back Port Project Goals
2547

@@ -57,6 +79,25 @@ This is a simplified backport with the following changes from the original:
5779

5880
These vulnerabilities exist in the upstream OpenJDK sandbox implementation and are reported here for transparency.
5981

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+
60101
## Building
61102

62103
Requires JDK 21 or later. Build with Maven:

0 commit comments

Comments
 (0)