使用Feature.Allow_Comments使用以下代码忽略即时json模式中的注释:
public static ProcessingReport validateJson(String jsonSchemaNode, String jsonText)
throws ProcessingException {
JsonFactory jsonFactory = new JsonFactory();
jsonFactory.enable(JsonParser.Feature.ALLOW_COMMENTS);
jsonFactory.enable(JsonParser.Feature.ALLOW_YAML_COMMENTS);
ObjectMapper mapper = new ObjectMapper(jsonFactory)
.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true)
.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
JsonNode jsonSchema = null;
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
try {
jsonSchema = mapper.readTree(jsonSchemaNode);
return factory.getJsonSchema(jsonSchema).validate(mapper.readTree(jsonText));
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
但是,如果Json文件引用了另一个架构文件。导入的json模式文件中不会忽略这些注释。
{
/*Hello,
I'm ABhishekt*/
//This is a comment
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "SubmitProcess",
"description": "Sample COmmand",
"type": "object",
"allOf": [{ "$ref": "resource:/JsonSchema/Schema.json#"}]
}
因此,如果导入的Schema.json中有任何注释。该文件中不允许评论。
请确认这是否是增强功能。
出现错误:
--- BEGIN MESSAGES ---
fatal: content at URI "resource:/JsonSchema/Schema.json#" is not valid JSON
level: "fatal"
uri: "resource:/JsonSchema/Schema.json#"
parsingMessage: "Unexpected character ('/' (code 47)): was expecting double-quote to start field name"
info: "other messages follow (if any)"
--- END MESSAGES ---
有什么方法可以将映射器/解析器传递给JsonSchemaFactory?