Skip to content

Commit 45c0b8f

Browse files
authored
Adds JsonNodeReader (#1065)
* Add object reader * Deprecate setting jsonMapper and yamlMapper on JsonSchemaFactory * Fix javadoc * Fix javadoc * Rename to json node reader
1 parent 6621810 commit 45c0b8f

19 files changed

+403
-59
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ Assertions contains the following additional information
353353
| Arguments | The arguments used for generating the message.
354354
| Type | The keyword that generated the message.
355355
| Property | The property name that caused the validation error for example for the `required` keyword. Note that this is not part of the instance location as that points to the instance node.
356-
| Schema Node | The `JsonNode` pointed to by the Schema Location. This is the schema data that caused the input data to fail. It is possible to get the location information by configuring the `JsonSchemaFactory` with the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(schemaNode)`.
357-
| Instance Node | The `JsonNode` pointed to by the Instance Location. This is the input data that failed validation. It is possible to get the location information by configuring the `JsonSchemaFactory` with the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(instanceNode)`.
356+
| Schema Node | The `JsonNode` pointed to by the Schema Location. This is the schema data that caused the input data to fail. It is possible to get the location information by configuring the `JsonSchemaFactory` with a `JsonNodeReader` that uses the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(schemaNode)`.
357+
| Instance Node | The `JsonNode` pointed to by the Instance Location. This is the input data that failed validation. It is possible to get the location information by configuring the `JsonSchemaFactory` with a `JsonNodeReader` that uses the `LocationJsonNodeFactoryFactory` and using `JsonNodes.tokenLocationOf(instanceNode)`.
358358
| Details | Additional details that can be set by custom keyword validator implementations. This is not used by the library.
359359

360360
Annotations contains the following additional information
@@ -367,7 +367,7 @@ Annotations contains the following additional information
367367

368368
The library can be configured to store line and column information in the `JsonNode` instances for the instance and schema nodes. This will adversely affect performance and is not configured by default.
369369

370-
This is done by configuring a `LocationJsonNodeFactoryFactory` on the `JsonSchemaFactory`. The `JsonLocation` information can then be retrieved using `JsonNodes.tokenLocationOf(jsonNode)`.
370+
This is done by configuring a `JsonNodeReader` that uses the `LocationJsonNodeFactoryFactory`on the `JsonSchemaFactory`. The `JsonLocation` information can then be retrieved using `JsonNodes.tokenLocationOf(jsonNode)`.
371371

372372
```java
373373
String schemaData = "{\r\n"
@@ -383,7 +383,7 @@ String inputData = "{\r\n"
383383
+ " \"startDate\": \"1\"\r\n"
384384
+ "}";
385385
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
386-
builder -> builder.jsonNodeFactoryFactory(LocationJsonNodeFactoryFactory.getInstance()));
386+
builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build()));
387387
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
388388
config.setPathType(PathType.JSON_POINTER);
389389
JsonSchema schema = factory.getSchema(schemaData, InputFormat.JSON, config);

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

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ private static class Holder {
3131
private static DisallowUnknownJsonMetaSchemaFactory INSTANCE = new DisallowUnknownJsonMetaSchemaFactory();
3232
}
3333

34+
/**
35+
* Gets the instance of {@link DisallowUnknownJsonMetaSchemaFactory}.
36+
*
37+
* @return the json meta schema factory
38+
*/
3439
public static DisallowUnknownJsonMetaSchemaFactory getInstance() {
3540
return Holder.INSTANCE;
3641
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ private static class Holder {
3636
private static DisallowUnknownKeywordFactory INSTANCE = new DisallowUnknownKeywordFactory();
3737
}
3838

39+
/**
40+
* Gets the instance of {@link DisallowUnknownKeywordFactory}.
41+
*
42+
* @return the keyword factory
43+
*/
3944
public static DisallowUnknownKeywordFactory getInstance() {
4045
return Holder.INSTANCE;
4146
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ public ValidationResult walk(ExecutionContext executionContext, JsonNode node, b
10211021
/**
10221022
* Walk the JSON node.
10231023
*
1024+
* @param <T> the result type
10241025
* @param executionContext the execution context
10251026
* @param node the input
10261027
* @param outputFormat the output format
@@ -1053,6 +1054,7 @@ public ValidationResult walk(ExecutionContext executionContext, JsonNode node, b
10531054
/**
10541055
* Walk the JSON node.
10551056
*
1057+
* @param <T> the result type
10561058
* @param executionContext the execution context
10571059
* @param node the input
10581060
* @param outputFormat the output format
@@ -1099,6 +1101,7 @@ public ValidationResult walk(ExecutionContext executionContext, String input, In
10991101
/**
11001102
* Walk the input.
11011103
*
1104+
* @param <T> the result type
11021105
* @param executionContext the execution context
11031106
* @param input the input
11041107
* @param inputFormat the input format
@@ -1132,6 +1135,7 @@ public ValidationResult walk(ExecutionContext executionContext, String input, In
11321135
/**
11331136
* Walk the input.
11341137
*
1138+
* @param <T> the result type
11351139
* @param executionContext the execution context
11361140
* @param input the input
11371141
* @param inputFormat the input format
@@ -1160,6 +1164,7 @@ public ValidationResult walk(JsonNode node, boolean validate) {
11601164
/**
11611165
* Walk the JSON node.
11621166
*
1167+
* @param <T> the result type
11631168
* @param node the input
11641169
* @param validate true to validate the input against the schema
11651170
* @param outputFormat the output format

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

+40-23
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
import com.networknt.schema.resource.SchemaMapper;
2626
import com.networknt.schema.resource.SchemaMappers;
2727
import com.networknt.schema.serialization.JsonMapperFactory;
28+
import com.networknt.schema.serialization.JsonNodeReader;
2829
import com.networknt.schema.serialization.YamlMapperFactory;
29-
import com.networknt.schema.serialization.node.JsonNodeFactoryFactory;
30-
import com.networknt.schema.utils.JsonNodes;
3130

3231
import org.slf4j.Logger;
3332
import org.slf4j.LoggerFactory;
@@ -56,7 +55,7 @@ public class JsonSchemaFactory {
5655
public static class Builder {
5756
private ObjectMapper jsonMapper = null;
5857
private ObjectMapper yamlMapper = null;
59-
private JsonNodeFactoryFactory jsonNodeFactoryFactory = null;
58+
private JsonNodeReader jsonNodeReader = null;
6059
private String defaultMetaSchemaIri;
6160
private final ConcurrentMap<String, JsonMetaSchema> metaSchemas = new ConcurrentHashMap<String, JsonMetaSchema>();
6261
private SchemaLoaders.Builder schemaLoadersBuilder = null;
@@ -65,25 +64,47 @@ public static class Builder {
6564
private JsonMetaSchemaFactory metaSchemaFactory = null;
6665

6766
/**
68-
* Configures the {@link JsonNodeFactoryFactory} to use.
67+
* Sets the json node reader to read the data.
6968
* <p>
70-
* To get location information from {@link JsonNode} the
71-
* {@link com.networknt.schema.serialization.node.LocationJsonNodeFactoryFactory}
72-
* can be used.
69+
* If set this takes precedence over the configured json mapper and yaml mapper.
70+
* <p>
71+
* A location aware object reader can be created using JsonNodeReader.builder().locationAware().build().
7372
*
74-
* @param jsonNodeFactoryFactory the factory to create json node factories
73+
* @param jsonNodeReader the object reader
7574
* @return the builder
7675
*/
77-
public Builder jsonNodeFactoryFactory(JsonNodeFactoryFactory jsonNodeFactoryFactory) {
78-
this.jsonNodeFactoryFactory = jsonNodeFactoryFactory;
76+
public Builder jsonNodeReader(JsonNodeReader jsonNodeReader) {
77+
this.jsonNodeReader = jsonNodeReader;
7978
return this;
8079
}
8180

81+
/**
82+
* Sets the json mapper to read the data.
83+
* <p>
84+
* If the object reader is set this will not be used.
85+
* <p>
86+
* This is deprecated use a object reader instead.
87+
*
88+
* @param jsonMapper the json mapper
89+
* @return the builder
90+
*/
91+
@Deprecated
8292
public Builder jsonMapper(final ObjectMapper jsonMapper) {
8393
this.jsonMapper = jsonMapper;
8494
return this;
8595
}
8696

97+
/**
98+
* Sets the yaml mapper to read the data.
99+
* <p>
100+
* If the object reader is set this will not be used.
101+
* <p>
102+
* This is deprecated use a object reader instead.
103+
*
104+
* @param yamlMapper the yaml mapper
105+
* @return the builder
106+
*/
107+
@Deprecated
87108
public Builder yamlMapper(final ObjectMapper yamlMapper) {
88109
this.yamlMapper = yamlMapper;
89110
return this;
@@ -151,7 +172,7 @@ public JsonSchemaFactory build() {
151172
return new JsonSchemaFactory(
152173
jsonMapper,
153174
yamlMapper,
154-
jsonNodeFactoryFactory,
175+
jsonNodeReader,
155176
defaultMetaSchemaIri,
156177
schemaLoadersBuilder,
157178
schemaMappersBuilder,
@@ -164,7 +185,7 @@ public JsonSchemaFactory build() {
164185

165186
private final ObjectMapper jsonMapper;
166187
private final ObjectMapper yamlMapper;
167-
private final JsonNodeFactoryFactory jsonNodeFactoryFactory;
188+
private final JsonNodeReader jsonNodeReader;
168189
private final String defaultMetaSchemaIri;
169190
private final SchemaLoaders.Builder schemaLoadersBuilder;
170191
private final SchemaMappers.Builder schemaMappersBuilder;
@@ -180,7 +201,7 @@ public JsonSchemaFactory build() {
180201
private JsonSchemaFactory(
181202
ObjectMapper jsonMapper,
182203
ObjectMapper yamlMapper,
183-
JsonNodeFactoryFactory jsonNodeFactoryFactory,
204+
JsonNodeReader jsonNodeReader,
184205
String defaultMetaSchemaIri,
185206
SchemaLoaders.Builder schemaLoadersBuilder,
186207
SchemaMappers.Builder schemaMappersBuilder,
@@ -197,7 +218,7 @@ private JsonSchemaFactory(
197218
}
198219
this.jsonMapper = jsonMapper;
199220
this.yamlMapper = yamlMapper;
200-
this.jsonNodeFactoryFactory = jsonNodeFactoryFactory;
221+
this.jsonNodeReader = jsonNodeReader;
201222
this.defaultMetaSchemaIri = defaultMetaSchemaIri;
202223
this.schemaLoadersBuilder = schemaLoadersBuilder;
203224
this.schemaMappersBuilder = schemaMappersBuilder;
@@ -290,7 +311,7 @@ public static Builder builder(final JsonSchemaFactory blueprint) {
290311
.defaultMetaSchemaIri(blueprint.defaultMetaSchemaIri)
291312
.jsonMapper(blueprint.jsonMapper)
292313
.yamlMapper(blueprint.yamlMapper)
293-
.jsonNodeFactoryFactory(blueprint.jsonNodeFactoryFactory);
314+
.jsonNodeReader(blueprint.jsonNodeReader);
294315
if (blueprint.schemaLoadersBuilder != null) {
295316
builder.schemaLoadersBuilder = SchemaLoaders.builder().with(blueprint.schemaLoadersBuilder);
296317
}
@@ -442,18 +463,18 @@ protected JsonMetaSchema loadMetaSchema(String iri, SchemaValidatorsConfig confi
442463
}
443464

444465
JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
445-
if (this.jsonNodeFactoryFactory == null) {
466+
if (this.jsonNodeReader == null) {
446467
return getObjectMapper(inputFormat).readTree(content);
447468
} else {
448-
return JsonNodes.readTree(getObjectMapper(inputFormat), content, this.jsonNodeFactoryFactory);
469+
return this.jsonNodeReader.readTree(content, inputFormat);
449470
}
450471
}
451472

452473
JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
453-
if (this.jsonNodeFactoryFactory == null) {
474+
if (this.jsonNodeReader == null) {
454475
return getObjectMapper(inputFormat).readTree(content);
455476
} else {
456-
return JsonNodes.readTree(getObjectMapper(inputFormat), content, this.jsonNodeFactoryFactory);
477+
return this.jsonNodeReader.readTree(content, inputFormat);
457478
}
458479
}
459480

@@ -627,10 +648,6 @@ ObjectMapper getJsonMapper() {
627648
return this.jsonMapper != null ? this.jsonMapper : JsonMapperFactory.getInstance();
628649
}
629650

630-
JsonNodeFactoryFactory getJsonNodeFactoryFactory() {
631-
return this.jsonNodeFactoryFactory;
632-
}
633-
634651
/**
635652
* Creates a schema validators config.
636653
*

0 commit comments

Comments
 (0)