Skip to content

Commit

Permalink
Rename to json node reader
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay committed Jun 14, 2024
1 parent 17184ce commit 68acc5e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ String inputData = "{\r\n"
+ " \"startDate\": \"1\"\r\n"
+ "}";
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
builder -> builder.objectReader(ObjectReader.builder().locationAware().build()));
builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build()));
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setPathType(PathType.JSON_POINTER);
JsonSchema schema = factory.getSchema(schemaData, InputFormat.JSON, config);
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/com/networknt/schema/JsonSchemaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.networknt.schema.resource.SchemaMapper;
import com.networknt.schema.resource.SchemaMappers;
import com.networknt.schema.serialization.JsonMapperFactory;
import com.networknt.schema.serialization.ObjectReader;
import com.networknt.schema.serialization.JsonNodeReader;
import com.networknt.schema.serialization.YamlMapperFactory;

import org.slf4j.Logger;
Expand Down Expand Up @@ -55,7 +55,7 @@ public class JsonSchemaFactory {
public static class Builder {
private ObjectMapper jsonMapper = null;
private ObjectMapper yamlMapper = null;
private ObjectReader objectReader = null;
private JsonNodeReader jsonNodeReader = null;
private String defaultMetaSchemaIri;
private final ConcurrentMap<String, JsonMetaSchema> metaSchemas = new ConcurrentHashMap<String, JsonMetaSchema>();
private SchemaLoaders.Builder schemaLoadersBuilder = null;
Expand All @@ -64,17 +64,17 @@ public static class Builder {
private JsonMetaSchemaFactory metaSchemaFactory = null;

/**
* Sets the object reader to read the data.
* Sets the json node reader to read the data.
* <p>
* If set this takes precedence over the configured json mapper and yaml mapper.
* <p>
* A location aware object reader can be created using ObjectReader.builder().locationAware().build().
* A location aware object reader can be created using JsonNodeReader.builder().locationAware().build().
*
* @param objectReader the object reader
* @param jsonNodeReader the object reader
* @return the builder
*/
public Builder objectReader(ObjectReader objectReader) {
this.objectReader = objectReader;
public Builder jsonNodeReader(JsonNodeReader jsonNodeReader) {
this.jsonNodeReader = jsonNodeReader;
return this;
}

Expand Down Expand Up @@ -172,7 +172,7 @@ public JsonSchemaFactory build() {
return new JsonSchemaFactory(
jsonMapper,
yamlMapper,
objectReader,
jsonNodeReader,
defaultMetaSchemaIri,
schemaLoadersBuilder,
schemaMappersBuilder,
Expand All @@ -185,7 +185,7 @@ public JsonSchemaFactory build() {

private final ObjectMapper jsonMapper;
private final ObjectMapper yamlMapper;
private final ObjectReader objectReader;
private final JsonNodeReader jsonNodeReader;
private final String defaultMetaSchemaIri;
private final SchemaLoaders.Builder schemaLoadersBuilder;
private final SchemaMappers.Builder schemaMappersBuilder;
Expand All @@ -201,7 +201,7 @@ public JsonSchemaFactory build() {
private JsonSchemaFactory(
ObjectMapper jsonMapper,
ObjectMapper yamlMapper,
ObjectReader objectReader,
JsonNodeReader jsonNodeReader,
String defaultMetaSchemaIri,
SchemaLoaders.Builder schemaLoadersBuilder,
SchemaMappers.Builder schemaMappersBuilder,
Expand All @@ -218,7 +218,7 @@ private JsonSchemaFactory(
}
this.jsonMapper = jsonMapper;
this.yamlMapper = yamlMapper;
this.objectReader = objectReader;
this.jsonNodeReader = jsonNodeReader;
this.defaultMetaSchemaIri = defaultMetaSchemaIri;
this.schemaLoadersBuilder = schemaLoadersBuilder;
this.schemaMappersBuilder = schemaMappersBuilder;
Expand Down Expand Up @@ -311,7 +311,7 @@ public static Builder builder(final JsonSchemaFactory blueprint) {
.defaultMetaSchemaIri(blueprint.defaultMetaSchemaIri)
.jsonMapper(blueprint.jsonMapper)
.yamlMapper(blueprint.yamlMapper)
.objectReader(blueprint.objectReader);
.jsonNodeReader(blueprint.jsonNodeReader);
if (blueprint.schemaLoadersBuilder != null) {
builder.schemaLoadersBuilder = SchemaLoaders.builder().with(blueprint.schemaLoadersBuilder);
}
Expand Down Expand Up @@ -463,18 +463,18 @@ protected JsonMetaSchema loadMetaSchema(String iri, SchemaValidatorsConfig confi
}

JsonNode readTree(String content, InputFormat inputFormat) throws IOException {
if (this.objectReader == null) {
if (this.jsonNodeReader == null) {
return getObjectMapper(inputFormat).readTree(content);
} else {
return this.objectReader.readTree(content, inputFormat);
return this.jsonNodeReader.readTree(content, inputFormat);
}
}

JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException {
if (this.objectReader == null) {
if (this.jsonNodeReader == null) {
return getObjectMapper(inputFormat).readTree(content);
} else {
return this.objectReader.readTree(content, inputFormat);
return this.jsonNodeReader.readTree(content, inputFormat);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import com.networknt.schema.utils.JsonNodes;

/**
* Default {@link ObjectReader}.
* Default {@link JsonNodeReader}.
*/
public class DefaultObjectReader implements ObjectReader {
public class DefaultJsonNodeReader implements JsonNodeReader {
protected final ObjectMapper jsonMapper;
protected final ObjectMapper yamlMapper;
protected final JsonNodeFactoryFactory jsonNodeFactoryFactory;
Expand All @@ -25,7 +25,7 @@ public class DefaultObjectReader implements ObjectReader {
* @param yamlMapper the yaml mapper
* @param jsonNodeFactoryFactory the json node factory factory
*/
protected DefaultObjectReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper,
protected DefaultJsonNodeReader(ObjectMapper jsonMapper, ObjectMapper yamlMapper,
JsonNodeFactoryFactory jsonNodeFactoryFactory) {
this.jsonMapper = jsonMapper;
this.yamlMapper = yamlMapper;
Expand Down Expand Up @@ -84,7 +84,7 @@ protected ObjectMapper getObjectMapper(InputFormat inputFormat) {
}

/**
* Gets the builder for {@link DefaultObjectReader}.
* Gets the builder for {@link DefaultJsonNodeReader}.
*
* @return the builder
*/
Expand All @@ -93,7 +93,7 @@ public static Builder builder() {
}

/**
* Builder support for {@link ObjectReader}.
* Builder support for {@link JsonNodeReader}.
*
* @param <T> the super type
*/
Expand Down Expand Up @@ -143,7 +143,7 @@ public T jsonNodeFactoryFactory(JsonNodeFactoryFactory jsonNodeFactoryFactory) {
}

/**
* Builder for {@link DefaultObjectReader}.
* Builder for {@link DefaultJsonNodeReader}.
*/
public static class Builder extends BuilderSupport<Builder> {

Expand All @@ -162,12 +162,12 @@ public Builder locationAware() {
}

/**
* Builds the {@link ObjectReader}.
* Builds the {@link JsonNodeReader}.
*
* @return the object reader
*/
public ObjectReader build() {
return new DefaultObjectReader(this.jsonMapper, this.yamlMapper, this.jsonNodeFactoryFactory);
public JsonNodeReader build() {
return new DefaultJsonNodeReader(this.jsonMapper, this.yamlMapper, this.jsonNodeFactoryFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import com.networknt.schema.InputFormat;

/**
* Object reader.
* Reader for reading content to {@link JsonNode}.
*/
public interface ObjectReader {
public interface JsonNodeReader {

/**
* Deserialize content as a tree.
Expand All @@ -47,11 +47,11 @@ public interface ObjectReader {
JsonNode readTree(InputStream content, InputFormat inputFormat) throws IOException;

/**
* Creates a builder for {@link ObjectReader}.
* Creates a builder for {@link JsonNodeReader}.
*
* @return the builder
*/
public static DefaultObjectReader.Builder builder() {
return DefaultObjectReader.builder();
public static DefaultJsonNodeReader.Builder builder() {
return DefaultJsonNodeReader.builder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* Test for Default Object Reader.
*/
class DefaultObjectReaderTest {
class DefaultJsonNodeReaderTest {
@Test
void location() throws JsonParseException, IOException {
String schemaData = "{\r\n"
Expand All @@ -41,7 +41,7 @@ void location() throws JsonParseException, IOException {
+ " }\r\n"
+ " }\r\n"
+ "}";
JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON);
JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON);
JsonNode idNode = jsonNode.at("/$id");
JsonLocation location = JsonNodes.tokenLocationOf(idNode);
assertEquals(2, location.getLineNr());
Expand Down Expand Up @@ -69,7 +69,7 @@ void jsonLocation() throws IOException {
+ " }\r\n"
+ " }\r\n"
+ "}";
JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON);
JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.JSON);

JsonLocation formatSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/format"));
JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/minLength"));
Expand All @@ -90,7 +90,7 @@ void yamlLocation() throws IOException {
+ " format: 'date'\r\n"
+ " minLength: 6\r\n"
+ "";
JsonNode jsonNode = ObjectReader.builder().locationAware().build().readTree(schemaData, InputFormat.YAML);
JsonNode jsonNode = JsonNodeReader.builder().locationAware().build().readTree(schemaData, InputFormat.YAML);

JsonLocation formatSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/format"));
JsonLocation minLengthSchemaNodeTokenLocation = JsonNodes.tokenLocationOf(jsonNode.at("/properties/startDate/minLength"));
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/networknt/schema/utils/JsonNodesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import com.networknt.schema.SpecVersion.VersionFlag;
import com.networknt.schema.ValidationMessage;
import com.networknt.schema.serialization.JsonMapperFactory;
import com.networknt.schema.serialization.ObjectReader;
import com.networknt.schema.serialization.JsonNodeReader;
import com.networknt.schema.serialization.node.LocationJsonNodeFactoryFactory;
/**
* Tests for JsonNodes.
Expand Down Expand Up @@ -90,7 +90,7 @@ void jsonLocation() {
+ " \"startDate\": \"1\"\r\n"
+ "}";
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
builder -> builder.objectReader(ObjectReader.builder().locationAware().build()));
builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build()));
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setPathType(PathType.JSON_POINTER);
JsonSchema schema = factory.getSchema(schemaData, InputFormat.JSON, config);
Expand Down Expand Up @@ -139,7 +139,7 @@ void yamlLocation() {
+ "startDate: '1'\r\n"
+ "";
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
builder -> builder.objectReader(ObjectReader.builder().locationAware().build()));
builder -> builder.jsonNodeReader(JsonNodeReader.builder().locationAware().build()));
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setPathType(PathType.JSON_POINTER);
JsonSchema schema = factory.getSchema(schemaData, InputFormat.YAML, config);
Expand Down

0 comments on commit 68acc5e

Please sign in to comment.