2626import com .networknt .schema .resource .ResourceLoaders ;
2727import com .networknt .schema .resource .SchemaIdResolvers ;
2828import 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
3233import org .slf4j .Logger ;
3334import 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 /**
0 commit comments