Skip to content

Commit e79fdad

Browse files
committed
Rename JsonNodeReader to NodeReader
1 parent cfca9b1 commit e79fdad

File tree

6 files changed

+87
-43
lines changed

6 files changed

+87
-43
lines changed

src/main/java/com/networknt/schema/SchemaRegistry.java

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
import com.networknt.schema.resource.ResourceLoaders;
2727
import com.networknt.schema.resource.SchemaIdResolvers;
2828
import com.networknt.schema.resource.SchemaLoader;
29-
import com.networknt.schema.serialization.BasicJsonNodeReader;
30-
import com.networknt.schema.serialization.JsonNodeReader;
29+
import com.networknt.schema.serialization.BasicNodeReader;
30+
import com.networknt.schema.serialization.DefaultNodeReader;
31+
import com.networknt.schema.serialization.NodeReader;
3132

3233
import org.slf4j.Logger;
3334
import org.slf4j.LoggerFactory;
@@ -57,24 +58,46 @@ public class SchemaRegistry {
5758
public static class Builder {
5859
private String defaultDialectId;
5960
private DialectRegistry dialectRegistry = null;
60-
private JsonNodeReader jsonNodeReader = null;
61+
private NodeReader nodeReader = null;
6162
private SchemaLoader schemaLoader = null;
6263
private boolean enableSchemaCache = true;
6364
private SchemaRegistryConfig schemaRegistryConfig = null;
6465

6566
/**
6667
* Sets the json node reader to read the data.
6768
* <p>
68-
* If set this takes precedence over the configured json mapper and yaml mapper.
69-
* <p>
7069
* A location aware object reader can be created using
71-
* JsonNodeReader.builder().locationAware().build().
70+
* NodeReader.builder().locationAware().build().
7271
*
73-
* @param jsonNodeReader the object reader
72+
* @param nodeReader the object reader
7473
* @return the builder
7574
*/
76-
public Builder jsonNodeReader(JsonNodeReader jsonNodeReader) {
77-
this.jsonNodeReader = jsonNodeReader;
75+
public Builder nodeReader(NodeReader nodeReader) {
76+
this.nodeReader = nodeReader;
77+
return this;
78+
}
79+
80+
/**
81+
* Sets the json node reader to read the data.
82+
*
83+
* <pre>
84+
* A location aware object reader can be created using
85+
* schemaRegistryBuilder.nodeReader(nodeReader -&gt; nodeReader.locationAware()).
86+
* </pre>
87+
*
88+
* A json ObjectMapper can be set using
89+
*
90+
* <pre>
91+
* schemaRegistryBuilder.nodeReader(nodeReader -&gt; nodeReader.jsonMapper(objectMapper))
92+
* </pre>
93+
*
94+
* @param customizer
95+
* @return the builder
96+
*/
97+
public Builder nodeReader(Consumer<DefaultNodeReader.Builder> customizer) {
98+
DefaultNodeReader.Builder builder = NodeReader.builder();
99+
customizer.accept(builder);
100+
this.nodeReader = builder.build();
78101
return this;
79102
}
80103

@@ -140,25 +163,25 @@ public Builder schemaRegistryConfig(SchemaRegistryConfig schemaRegistryConfig) {
140163
}
141164

142165
public SchemaRegistry build() {
143-
return new SchemaRegistry(jsonNodeReader, defaultDialectId, schemaLoader, enableSchemaCache,
166+
return new SchemaRegistry(nodeReader, defaultDialectId, schemaLoader, enableSchemaCache,
144167
dialectRegistry, schemaRegistryConfig);
145168
}
146169
}
147170

148-
private final JsonNodeReader jsonNodeReader;
171+
private final NodeReader nodeReader;
149172
private final String defaultDialectId;
150173
private final SchemaLoader schemaLoader;
151174
private final ConcurrentMap<SchemaLocation, Schema> schemaCache = new ConcurrentHashMap<>();
152175
private final boolean enableSchemaCache;
153176
private final DialectRegistry dialectRegistry;
154177
private final SchemaRegistryConfig schemaRegistryConfig;
155178

156-
private SchemaRegistry(JsonNodeReader jsonNodeReader, String defaultDialectId, SchemaLoader schemaLoader,
179+
private SchemaRegistry(NodeReader nodeReader, String defaultDialectId, SchemaLoader schemaLoader,
157180
boolean enableSchemaCache, DialectRegistry dialectRegistry, SchemaRegistryConfig schemaRegistryConfig) {
158181
if (defaultDialectId == null || defaultDialectId.trim().isEmpty()) {
159182
throw new IllegalArgumentException("defaultDialectId must not be null or empty");
160183
}
161-
this.jsonNodeReader = jsonNodeReader != null ? jsonNodeReader : BasicJsonNodeReader.getInstance();
184+
this.nodeReader = nodeReader != null ? nodeReader : BasicNodeReader.getInstance();
162185
this.defaultDialectId = defaultDialectId;
163186
this.schemaLoader = schemaLoader != null ? schemaLoader : SchemaLoader.getDefault();
164187
this.enableSchemaCache = enableSchemaCache;
@@ -184,6 +207,10 @@ public static Builder builder() {
184207
/**
185208
* Creates a new schema registry with a default schema dialect. The schema
186209
* dialect will only be used if the input does not specify a $schema.
210+
* <p>
211+
* This uses a dialect registry that contains all the supported standard
212+
* specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft
213+
* 2020-12.
187214
*
188215
* @param specificationVersion the default dialect id corresponding to the
189216
* specification version used when the schema does
@@ -197,6 +224,10 @@ public static SchemaRegistry withDefaultDialect(SpecificationVersion specificati
197224
/**
198225
* Creates a new schema registry with a default schema dialect. The schema
199226
* dialect will only be used if the input does not specify a $schema.
227+
* <p>
228+
* This uses a dialect registry that contains all the supported standard
229+
* specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft
230+
* 2020-12.
200231
*
201232
* @param specificationVersion the default dialect id corresponding to the
202233
* specification version used when the schema does
@@ -213,6 +244,10 @@ public static SchemaRegistry withDefaultDialect(SpecificationVersion specificati
213244
/**
214245
* Creates a new schema registry with a default schema dialect. The schema
215246
* dialect will only be used if the input does not specify a $schema.
247+
* <p>
248+
* This uses a dialect registry that contains all the supported standard
249+
* specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft
250+
* 2020-12.
216251
*
217252
* @param dialectId the default dialect id used when the schema does not
218253
* specify the $schema keyword
@@ -231,6 +266,9 @@ public static SchemaRegistry withDefaultDialectId(String dialectId, Consumer<Sch
231266
* Gets a new schema registry that supports a specific dialect only.
232267
* <p>
233268
* Schemas that do not specify dialect using $schema will use the dialect.
269+
* <p>
270+
* This uses a dialect registry that only contains this dialect and will throw
271+
* an exception for unknown dialects.
234272
*
235273
* @param dialect the dialect
236274
* @return the schema registry
@@ -243,6 +281,9 @@ public static SchemaRegistry withDialect(Dialect dialect) {
243281
* Gets a new schema registry that supports a specific dialect only.
244282
* <p>
245283
* Schemas that do not specify dialect using $schema will use the dialect.
284+
* <p>
285+
* This uses a dialect registry that only contains this dialect and will throw
286+
* an exception for unknown dialects.
246287
*
247288
* @param dialect the dialect
248289
* @param customizer to customize the registry
@@ -261,15 +302,18 @@ public static SchemaRegistry withDialect(Dialect dialect, Consumer<SchemaRegistr
261302
* Builder from an existing {@link SchemaRegistry}.
262303
* <p>
263304
* <code>
264-
* SchemaRegistry.builder(SchemaRegistry.withDefaultDialect(Specification.Version.DRAFT_2019_09));
305+
* SchemaRegistry.builder(SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2019_09));
265306
* </code>
266307
*
267308
* @param blueprint the existing factory
268309
* @return the builder
269310
*/
270311
public static Builder builder(SchemaRegistry blueprint) {
271-
Builder builder = builder().schemaLoader(blueprint.schemaLoader).defaultDialectId(blueprint.defaultDialectId)
272-
.jsonNodeReader(blueprint.jsonNodeReader);
312+
Builder builder = builder().schemaLoader(blueprint.schemaLoader)
313+
.defaultDialectId(blueprint.defaultDialectId)
314+
.nodeReader(blueprint.nodeReader)
315+
.dialectRegistry(blueprint.dialectRegistry)
316+
.schemaRegistryConfig(blueprint.schemaRegistryConfig);
273317
return builder;
274318
}
275319

@@ -402,11 +446,11 @@ public Dialect getDialect(String dialectId) {
402446
}
403447

404448
JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
405-
return this.jsonNodeReader.readTree(content, inputFormat);
449+
return this.nodeReader.readTree(content, inputFormat);
406450
}
407451

408452
JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
409-
return this.jsonNodeReader.readTree(content, inputFormat);
453+
return this.nodeReader.readTree(content, inputFormat);
410454
}
411455

412456
/**

src/main/java/com/networknt/schema/serialization/BasicJsonNodeReader.java renamed to src/main/java/com/networknt/schema/serialization/BasicNodeReader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@
2424
import com.networknt.schema.InputFormat;
2525

2626
/**
27-
* BasicJsonNodeReader.
27+
* Basic implementation of {@link NodeReader}.
2828
*/
29-
public class BasicJsonNodeReader implements JsonNodeReader {
29+
public class BasicNodeReader implements NodeReader {
3030
private static class Holder {
31-
private static final BasicJsonNodeReader INSTANCE = new BasicJsonNodeReader();
31+
private static final BasicNodeReader INSTANCE = new BasicNodeReader();
3232
}
3333

34-
public static BasicJsonNodeReader getInstance() {
34+
public static BasicNodeReader getInstance() {
3535
return Holder.INSTANCE;
3636
}
3737

38-
protected BasicJsonNodeReader() {
38+
protected BasicNodeReader() {
3939
}
4040

4141
@Override

src/main/java/com/networknt/schema/serialization/DefaultJsonNodeReader.java renamed to src/main/java/com/networknt/schema/serialization/DefaultNodeReader.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import com.networknt.schema.utils.JsonNodes;
1212

1313
/**
14-
* Default {@link JsonNodeReader}.
14+
* Default {@link NodeReader}.
1515
*/
16-
public class DefaultJsonNodeReader implements JsonNodeReader {
16+
public class DefaultNodeReader implements NodeReader {
1717
protected final ObjectMapper jsonMapper;
1818
protected final ObjectMapper yamlMapper;
1919
protected final JsonNodeFactoryFactory jsonNodeFactoryFactory;
@@ -25,7 +25,7 @@ public class DefaultJsonNodeReader implements JsonNodeReader {
2525
* @param yamlMapper the yaml mapper
2626
* @param jsonNodeFactoryFactory the json node factory factory
2727
*/
28-
protected DefaultJsonNodeReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper,
28+
protected DefaultNodeReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper,
2929
JsonNodeFactoryFactory jsonNodeFactoryFactory) {
3030
this.jsonMapper = jsonMapper;
3131
this.yamlMapper = yamlMapper;
@@ -84,7 +84,7 @@ protected ObjectMapper getObjectMapper(InputFormat inputFormat) {
8484
}
8585

8686
/**
87-
* Gets the builder for {@link DefaultJsonNodeReader}.
87+
* Gets the builder for {@link DefaultNodeReader}.
8888
*
8989
* @return the builder
9090
*/
@@ -93,7 +93,7 @@ public static Builder builder() {
9393
}
9494

9595
/**
96-
* Builder support for {@link JsonNodeReader}.
96+
* Builder support for {@link NodeReader}.
9797
*
9898
* @param <T> the super type
9999
*/
@@ -143,7 +143,7 @@ public T jsonNodeFactoryFactory(JsonNodeFactoryFactory jsonNodeFactoryFactory) {
143143
}
144144

145145
/**
146-
* Builder for {@link DefaultJsonNodeReader}.
146+
* Builder for {@link DefaultNodeReader}.
147147
*/
148148
public static class Builder extends BuilderSupport<Builder> {
149149

@@ -162,12 +162,12 @@ public Builder locationAware() {
162162
}
163163

164164
/**
165-
* Builds the {@link JsonNodeReader}.
165+
* Builds the {@link NodeReader}.
166166
*
167167
* @return the object reader
168168
*/
169-
public JsonNodeReader build() {
170-
return new DefaultJsonNodeReader(this.jsonMapper, this.yamlMapper, this.jsonNodeFactoryFactory);
169+
public NodeReader build() {
170+
return new DefaultNodeReader(this.jsonMapper, this.yamlMapper, this.jsonNodeFactoryFactory);
171171
}
172172
}
173173
}

src/main/java/com/networknt/schema/serialization/JsonNodeReader.java renamed to src/main/java/com/networknt/schema/serialization/NodeReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* Reader for reading content to {@link JsonNode}.
2626
*/
27-
public interface JsonNodeReader {
27+
public interface NodeReader {
2828

2929
/**
3030
* Deserialize content as a tree.
@@ -47,11 +47,11 @@ public interface JsonNodeReader {
4747
JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException;
4848

4949
/**
50-
* Creates a builder for {@link JsonNodeReader}.
50+
* Creates a builder for {@link NodeReader}.
5151
*
5252
* @return the builder
5353
*/
54-
static DefaultJsonNodeReader.Builder builder() {
55-
return DefaultJsonNodeReader.builder();
54+
static DefaultNodeReader.Builder builder() {
55+
return DefaultNodeReader.builder();
5656
}
5757
}

src/test/java/com/networknt/schema/serialization/DefaultJsonNodeReaderTest.java renamed to src/test/java/com/networknt/schema/serialization/DefaultNodeReaderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/**
3030
* Test for Default Object Reader.
3131
*/
32-
class DefaultJsonNodeReaderTest {
32+
class DefaultNodeReaderTest {
3333
@Test
3434
void location() throws JsonParseException, IOException {
3535
String schemaData = "{\r\n"
@@ -41,7 +41,7 @@ void location() throws JsonParseException, IOException {
4141
+ " }\r\n"
4242
+ " }\r\n"
4343
+ "}";
44-
JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON);
44+
JsonNode jsonNode = NodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON);
4545
JsonNode idNode = jsonNode.at("/$id");
4646
JsonLocation location = JsonNodes.tokenLocationOf(idNode);
4747
assertEquals(2, location.getLineNr());
@@ -69,7 +69,7 @@ void jsonLocation() throws IOException {
6969
+ " }\r\n"
7070
+ " }\r\n"
7171
+ "}";
72-
JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON);
72+
JsonNode jsonNode = NodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON);
7373

7474
JsonLocation formatSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/format"));
7575
JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/minLength"));
@@ -89,7 +89,7 @@ void yamlLocation() throws IOException {
8989
+ " startDate:\r\n"
9090
+ " format: 'date'\r\n"
9191
+ " minLength: 6\r\n";
92-
JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.YAML);
92+
JsonNode jsonNode = NodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.YAML);
9393

9494
JsonLocation formatSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/format"));
9595
JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/minLength"));

src/test/java/com/networknt/schema/utils/JsonNodesTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import com.networknt.schema.SpecificationVersion;
3838
import com.networknt.schema.Error;
3939
import com.networknt.schema.serialization.JsonMapperFactory;
40-
import com.networknt.schema.serialization.JsonNodeReader;
40+
import com.networknt.schema.serialization.NodeReader;
4141
import com.networknt.schema.serialization.node.LocationJsonNodeFactoryFactory;
4242
/**
4343
* Tests for JsonNodes.
@@ -87,7 +87,7 @@ void jsonLocation() {
8787
+ " \"startDate\": \"1\"\r\n"
8888
+ "}";
8989
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12,
90-
builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build()));
90+
builder -> builder.nodeReader(nodeReader -> nodeReader.locationAware()));
9191
Schema schema = factory.getSchema(schemaData, InputFormat.JSON);
9292
List<Error> messages = schema.validate(inputData, InputFormat.JSON, executionContext -> {
9393
executionContext.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true));
@@ -132,7 +132,7 @@ void yamlLocation() {
132132
String inputData = "---\r\n"
133133
+ "startDate: '1'\r\n";
134134
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12,
135-
builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build()));
135+
builder -> builder.nodeReader(NodeReader.builder().locationAware().build()));
136136
Schema schema = factory.getSchema(schemaData, InputFormat.YAML);
137137
List<Error> messages = schema.validate(inputData, InputFormat.YAML, executionContext -> {
138138
executionContext.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true));

0 commit comments

Comments
 (0)