Skip to content

Commit 9e8dfe1

Browse files
committed
Rename JsonNodeReader to NodeReader
1 parent df2f695 commit 9e8dfe1

File tree

6 files changed

+83
-43
lines changed

6 files changed

+83
-43
lines changed

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

Lines changed: 58 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,42 @@ 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+
* <p>
83+
* A location aware object reader can be created using
84+
* schemaRegistryBuilder.nodeReader(nodeReader -> nodeReader.locationAware()).
85+
* <p>
86+
* A json ObjectMapper can be set using
87+
* schemaRegistryBuilder.nodeReader(nodeReader ->
88+
* nodeReader.jsonMapper(objectMapper)).
89+
*
90+
* @param customizer
91+
* @return the builder
92+
*/
93+
public Builder nodeReader(Consumer<DefaultNodeReader.Builder> customizer) {
94+
DefaultNodeReader.Builder builder = NodeReader.builder();
95+
customizer.accept(builder);
96+
this.nodeReader = builder.build();
7897
return this;
7998
}
8099

@@ -140,25 +159,25 @@ public Builder schemaRegistryConfig(SchemaRegistryConfig schemaRegistryConfig) {
140159
}
141160

142161
public SchemaRegistry build() {
143-
return new SchemaRegistry(jsonNodeReader, defaultDialectId, schemaLoader, enableSchemaCache,
162+
return new SchemaRegistry(nodeReader, defaultDialectId, schemaLoader, enableSchemaCache,
144163
dialectRegistry, schemaRegistryConfig);
145164
}
146165
}
147166

148-
private final JsonNodeReader jsonNodeReader;
167+
private final NodeReader nodeReader;
149168
private final String defaultDialectId;
150169
private final SchemaLoader schemaLoader;
151170
private final ConcurrentMap<SchemaLocation, Schema> schemaCache = new ConcurrentHashMap<>();
152171
private final boolean enableSchemaCache;
153172
private final DialectRegistry dialectRegistry;
154173
private final SchemaRegistryConfig schemaRegistryConfig;
155174

156-
private SchemaRegistry(JsonNodeReader jsonNodeReader, String defaultDialectId, SchemaLoader schemaLoader,
175+
private SchemaRegistry(NodeReader nodeReader, String defaultDialectId, SchemaLoader schemaLoader,
157176
boolean enableSchemaCache, DialectRegistry dialectRegistry, SchemaRegistryConfig schemaRegistryConfig) {
158177
if (defaultDialectId == null || defaultDialectId.trim().isEmpty()) {
159178
throw new IllegalArgumentException("defaultDialectId must not be null or empty");
160179
}
161-
this.jsonNodeReader = jsonNodeReader != null ? jsonNodeReader : BasicJsonNodeReader.getInstance();
180+
this.nodeReader = nodeReader != null ? nodeReader : BasicNodeReader.getInstance();
162181
this.defaultDialectId = defaultDialectId;
163182
this.schemaLoader = schemaLoader != null ? schemaLoader : SchemaLoader.getDefault();
164183
this.enableSchemaCache = enableSchemaCache;
@@ -184,6 +203,10 @@ public static Builder builder() {
184203
/**
185204
* Creates a new schema registry with a default schema dialect. The schema
186205
* dialect will only be used if the input does not specify a $schema.
206+
* <p>
207+
* This uses a dialect registry that contains all the supported standard
208+
* specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft
209+
* 2020-12.
187210
*
188211
* @param specificationVersion the default dialect id corresponding to the
189212
* specification version used when the schema does
@@ -197,6 +220,10 @@ public static SchemaRegistry withDefaultDialect(SpecificationVersion specificati
197220
/**
198221
* Creates a new schema registry with a default schema dialect. The schema
199222
* dialect will only be used if the input does not specify a $schema.
223+
* <p>
224+
* This uses a dialect registry that contains all the supported standard
225+
* specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft
226+
* 2020-12.
200227
*
201228
* @param specificationVersion the default dialect id corresponding to the
202229
* specification version used when the schema does
@@ -213,6 +240,10 @@ public static SchemaRegistry withDefaultDialect(SpecificationVersion specificati
213240
/**
214241
* Creates a new schema registry with a default schema dialect. The schema
215242
* dialect will only be used if the input does not specify a $schema.
243+
* <p>
244+
* This uses a dialect registry that contains all the supported standard
245+
* specification dialects, Draft 4, Draft 6, Draft 7, Draft 2019-09 and Draft
246+
* 2020-12.
216247
*
217248
* @param dialectId the default dialect id used when the schema does not
218249
* specify the $schema keyword
@@ -231,6 +262,9 @@ public static SchemaRegistry withDefaultDialectId(String dialectId, Consumer<Sch
231262
* Gets a new schema registry that supports a specific dialect only.
232263
* <p>
233264
* Schemas that do not specify dialect using $schema will use the dialect.
265+
* <p>
266+
* This uses a dialect registry that only contains this dialect and will throw
267+
* an exception for unknown dialects.
234268
*
235269
* @param dialect the dialect
236270
* @return the schema registry
@@ -243,6 +277,9 @@ public static SchemaRegistry withDialect(Dialect dialect) {
243277
* Gets a new schema registry that supports a specific dialect only.
244278
* <p>
245279
* Schemas that do not specify dialect using $schema will use the dialect.
280+
* <p>
281+
* This uses a dialect registry that only contains this dialect and will throw
282+
* an exception for unknown dialects.
246283
*
247284
* @param dialect the dialect
248285
* @param customizer to customize the registry
@@ -261,15 +298,18 @@ public static SchemaRegistry withDialect(Dialect dialect, Consumer<SchemaRegistr
261298
* Builder from an existing {@link SchemaRegistry}.
262299
* <p>
263300
* <code>
264-
* SchemaRegistry.builder(SchemaRegistry.withDefaultDialect(Specification.Version.DRAFT_2019_09));
301+
* SchemaRegistry.builder(SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2019_09));
265302
* </code>
266303
*
267304
* @param blueprint the existing factory
268305
* @return the builder
269306
*/
270307
public static Builder builder(SchemaRegistry blueprint) {
271-
Builder builder = builder().schemaLoader(blueprint.schemaLoader).defaultDialectId(blueprint.defaultDialectId)
272-
.jsonNodeReader(blueprint.jsonNodeReader);
308+
Builder builder = builder().schemaLoader(blueprint.schemaLoader)
309+
.defaultDialectId(blueprint.defaultDialectId)
310+
.nodeReader(blueprint.nodeReader)
311+
.dialectRegistry(blueprint.dialectRegistry)
312+
.schemaRegistryConfig(blueprint.schemaRegistryConfig);
273313
return builder;
274314
}
275315

@@ -402,11 +442,11 @@ public Dialect getDialect(String dialectId) {
402442
}
403443

404444
JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
405-
return this.jsonNodeReader.readTree(content, inputFormat);
445+
return this.nodeReader.readTree(content, inputFormat);
406446
}
407447

408448
JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
409-
return this.jsonNodeReader.readTree(content, inputFormat);
449+
return this.nodeReader.readTree(content, inputFormat);
410450
}
411451

412452
/**

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)