Skip to content

Commit 22a6396

Browse files
authored
tidy up of docs (#101)
1 parent 2478f20 commit 22a6396

File tree

3 files changed

+495
-615
lines changed

3 files changed

+495
-615
lines changed

json-java21-jtd/ARCHITECTURE.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,53 +46,54 @@ flowchart TD
4646

4747
## Core API Design
4848

49-
Following modern Java patterns, we use a single public sealed interface with package-private record implementations:
49+
Following modern Java patterns, we use a package-private sealed interface with record implementations and a public facade class:
5050

5151
```java
52-
package io.github.simbo1905.json.jtd;
52+
package json.java21.jtd;
5353

5454
import jdk.sandbox.java.util.json.*;
5555

56-
public sealed interface JTDSchema
57-
permits JTDSchema.EmptySchema,
58-
JTDSchema.RefSchema,
59-
JTDSchema.TypeSchema,
60-
JTDSchema.EnumSchema,
61-
JTDSchema.ElementsSchema,
62-
JTDSchema.PropertiesSchema,
63-
JTDSchema.ValuesSchema,
64-
JTDSchema.DiscriminatorSchema {
65-
66-
/// Compile JTD schema from JSON
67-
static JTDSchema compile(JsonValue schemaJson) {
68-
// Parse and build immutable schema hierarchy
69-
}
70-
71-
/// Validate JSON document against schema
72-
default ValidationResult validate(JsonValue json) {
73-
// Stack-based validation
74-
}
75-
56+
/// Package-private sealed interface for schema types
57+
sealed interface JtdSchema
58+
permits JtdSchema.EmptySchema,
59+
JtdSchema.RefSchema,
60+
JtdSchema.TypeSchema,
61+
JtdSchema.EnumSchema,
62+
JtdSchema.ElementsSchema,
63+
JtdSchema.PropertiesSchema,
64+
JtdSchema.ValuesSchema,
65+
JtdSchema.DiscriminatorSchema,
66+
JtdSchema.NullableSchema {
67+
7668
/// Schema type records (package-private)
77-
record EmptySchema() implements JTDSchema {}
78-
record RefSchema(String ref) implements JTDSchema {}
79-
record TypeSchema(PrimitiveType type) implements JTDSchema {}
80-
record EnumSchema(Set<String> values) implements JTDSchema {}
81-
record ElementsSchema(JTDSchema elements) implements JTDSchema {}
69+
record EmptySchema() implements JtdSchema {}
70+
record RefSchema(String ref, Map<String, JtdSchema> definitions) implements JtdSchema {}
71+
record TypeSchema(PrimitiveType type) implements JtdSchema {}
72+
record EnumSchema(Set<String> values) implements JtdSchema {}
73+
record ElementsSchema(JtdSchema elements) implements JtdSchema {}
8274
record PropertiesSchema(
83-
Map<String, JTDSchema> properties,
84-
Map<String, JTDSchema> optionalProperties,
75+
Map<String, JtdSchema> properties,
76+
Map<String, JtdSchema> optionalProperties,
8577
boolean additionalProperties
86-
) implements JTDSchema {}
87-
record ValuesSchema(JTDSchema values) implements JTDSchema {}
78+
) implements JtdSchema {}
79+
record ValuesSchema(JtdSchema values) implements JtdSchema {}
8880
record DiscriminatorSchema(
8981
String discriminator,
90-
Map<String, JTDSchema> mapping
91-
) implements JTDSchema {}
82+
Map<String, JtdSchema> mapping
83+
) implements JtdSchema {}
84+
record NullableSchema(JtdSchema nullable) implements JtdSchema {}
85+
}
86+
87+
/// Public facade class for JTD operations
88+
public class Jtd {
89+
/// Compile and validate JSON against JTD schema
90+
public Result validate(JsonValue schema, JsonValue instance) {
91+
JtdSchema jtdSchema = compileSchema(schema);
92+
return validateWithStack(jtdSchema, instance);
93+
}
9294

9395
/// Validation result
94-
record ValidationResult(boolean valid, List<ValidationError> errors) {}
95-
record ValidationError(String instancePath, String schemaPath, String message) {}
96+
public record Result(boolean isValid, List<String> errors) {}
9697
}
9798
```
9899

@@ -232,7 +233,10 @@ record CompiledSchema(
232233

233234
```java
234235
import jdk.sandbox.java.util.json.*;
235-
import io.github.simbo1905.json.jtd.JTDSchema;
236+
import json.java21.jtd.Jtd;
237+
238+
// Create JTD validator
239+
Jtd jtd = new Jtd();
236240

237241
// Compile JTD schema
238242
String schemaJson = """
@@ -248,18 +252,16 @@ String schemaJson = """
248252
}
249253
""";
250254

251-
JTDSchema schema = JTDSchema.compile(Json.parse(schemaJson));
252-
253255
// Validate JSON
254256
String json = """
255257
{"id": "123", "name": "Alice", "age": 30, "email": "alice@example.com"}
256258
""";
257259

258-
JTDSchema.ValidationResult result = schema.validate(Json.parse(json));
260+
Jtd.Result result = jtd.validate(Json.parse(schemaJson), Json.parse(json));
259261

260-
if (!result.valid()) {
262+
if (!result.isValid()) {
261263
for (var error : result.errors()) {
262-
System.out.println(error.instancePath() + ": " + error.message());
264+
System.out.println(error);
263265
}
264266
}
265267
```

0 commit comments

Comments
 (0)