【发布时间】:2020-12-28 06:14:29
【问题描述】:
尝试使用 YAML 模式验证 json 数据时出现以下错误。我正在使用给定here
的 networknt 库com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException: 'reader',第 1 行,第 58 列中不允许映射值:
... -schema.org/draft-04/schema#type: objectproperties: id: type ... ^在 [来源:(StringReader);行:1,列:58] 在 com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException.from(MarkedYAMLException.java:27) 在 com.fasterxml.jackson.dataformat.yaml.YAMLParser.nextToken(YAMLParser.java:359) 在 com.fasterxml.jackson.core.JsonParser.nextFieldName(JsonParser.java:861) 在 com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject(JsonNodeDeserializer.java:250) 在 com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:68) 在 com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:15) 在 com.fasterxml.jackson.databind.ObjectMapper._readTreeAndClose(ObjectMapper.java:4270) 在 com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2720) 在 com.nike.nifi.validatejson.App.getJsonNodeFromStringContent(App.java:53) 在 com.nike.nifi.validatejson.App.validate(App.java:36) 在 com.nike.nifi.validatejson.App.main(App.java:25)
代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
public boolean validate() {
boolean isValid = false;
try {
String inputJsonString = getFileData("C:\\Projects\\data\\input.json");
JsonNode node = getJsonNodeFromStringContent(inputJsonString);
String schema = getFileData("C:\\Projects\\data\\yamlfrmjsonschema.yml");
JsonNode schemaNode = getJsonNodeFromStringContent(schema);
JsonSchema validator = getJsonSchemaFromJsonNodeAutomaticVersion(schemaNode);
Set<ValidationMessage> errors = validator.validate(node);
if (errors.isEmpty()) {
isValid = !isValid;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return isValid;
}
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
private JsonNode getJsonNodeFromStringContent(String content) throws IOException {
return mapper.readTree(content);
}
private JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build();
return factory.getSchema(jsonNode);
}
输入.json
{
"id": "0001",
"type": "donut",
"name": "Cake",
"image":
{
"url": "images/0001.jpg",
"width": 200,
"height": 200
},
"thumbnail":
{
"url": "images/thumbnails/0001.jpg",
"width": 32,
"height": 32
}
}
YAML
---
"$schema": http://json-schema.org/draft-04/schema#
type: object
properties:
id:
type: string
type:
type: string
name:
type: string
image:
type: object
properties:
url:
type: string
width:
type: integer
height:
type: integer
required:
- url
- width
- height
thumbnail:
type: object
properties:
url:
type: string
width:
type: integer
height:
type: integer
required:
- url
- width
- height
required:
- id
- type
- name
- image
- thumbnail
【问题讨论】:
标签: java yaml jackson-dataformat-yaml