Reputation: 191
Trying to build and use the Schema object:
Schema rootSchema = ObjectSchema.builder()
.additionalProperties(true)
.build();
It seems to work fine until I try to serialize it as a string and then to reload using the SchemaLoader:
String json = objectMapper.writeValueAsString(rootSchema);
JSONObject schemaObject = new JSONObject(json);
Schema schema = SchemaLoader.load(schemaObject);
The problem is that the unset keywords are serialized as null:
{
"title" : null,
"description" : null,
"id" : null,
"propertySchemas" : { },
"schemaOfAdditionalProperties" : null,
"requiredProperties" : [ ],
"minProperties" : null,
"maxProperties" : null,
"propertyDependencies" : { },
"schemaDependencies" : { },
"patternProperties" : { }
}
The SchemaLoader reports these as errors, e.g. "java.lang.RuntimeException: org.json.JSONException: JSONObject["id"] not a string." Would it be possible to skip these keywords?
Another problem: the serialized version doesn't include the additionalProperties keyword.
Also, is it possible to include additional keywords, not specified in the JSON Schema spec? AFAIIK the spec itself doesn't prohibit such keywords, and some libraries such as AJV actually use them?
Upvotes: 0
Views: 498
Reputation: 3141
Trying to serialize the schema instance with jackson (like String json = objectMapper.writeValueAsString(rootSchema);
) won't work, since it will dump the internals of the schema object (which will actually be quite similar to a json schema, but not exactly).
Instead, you have to serialize the schema object to schema json by calling String json = rootSchema.toString();
Upvotes: 0