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,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 -> nodeReader.locationAware()).
86+ * </pre>
87+ *
88+ * A json ObjectMapper can be set using
89+ *
90+ * <pre>
91+ * schemaRegistryBuilder.nodeReader(nodeReader -> 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 /**
0 commit comments