标签:json schema validation 数据规范
目录 1. 什么是JSON Schema? 2. 如何定义一个JSON Schema 3. 如何测试JSON Schema a) 使用JSON Schema validator GUI b) 在Java code里使用JSON Schema validator 4.参考文档
JSON模式是基于JSON格式定义JSON数据结构的规范。
JSON Data 如下
"Hello, World"
JSON Schema 定义成
{
"type": "string"
}
用这个Schema 我们就可以来验证JSON数据
根据Data来生成JSON Schema 有现成的工具可以用http://jsonschema.net/#/
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "User",
"description": "demo schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"LastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": [
"firstName",
"LastName"
]
}
让我们来看看在这个模式中可以使用的各种重要的关键词:
更多的关键字可以参考http://json-schema.org/latest/json-schema-validation.html
or http://tools.ietf.org/html/draft-zyp-json-schema-03#page-11
当我们编写了一个JSON Schema 用于对客户端提交的数据进行验证之前,我们得确保我们编写的JSON Schema是正确的,我们当然就可以构造一些数据反向验证我们的JSON Schema的正确性与否。
网上有三十多个各种语言实现的JSON Schema validator, 我们用的是Java 里非常流行的,GitHub地址在这里。
地址 http://json-schema-validator.herokuapp.com/
Validation results: success
Validation results: failure
Error Message的信息非常详细。
Maven pom.xml 配置
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.github.fge</groupId> <artifactId>json-schema-validator</artifactId> <version>2.2.6</version> </dependency>
1 import com.fasterxml.jackson.databind.JsonNode; 2 import com.github.fge.jackson.JsonNodeReader; 3 import com.github.fge.jsonschema.core.report.ProcessingMessage; 4 import com.github.fge.jsonschema.core.report.ProcessingReport; 5 import com.github.fge.jsonschema.main.JsonSchemaFactory;
Validation success
1 @Test 2 public void validate_driverSet_schema() { 3 4 //some code to create Json schema, which we want to use data to validate 5 6 JsonNode schema = readJSONfile("src/test/resources/json/Schema.json"); 7 8 JsonNode data = readJSONfile("src/test/resources/json/DataGoodExample.json"); 9 10 ProcessingReport report = 11 JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data); 12 Assert.assertTrue(report.isSuccess()); 13 }
Validation failure
1 @Test 2 // wrong data 3 public void validate_driverSet_schema2() { 4 5 //some code to create Json schema, which we want to use data to validate 6 7 JsonNode data = readJSONfile("src/test/resources/json/DataBadExample.json"); 8 JsonNode schema = readJSONfile("src/test/resources/json/Schema.json"); 9 10 ProcessingReport report = 11 JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data); 12 Assert.assertFalse(report.isSuccess()); 13 14 // assert error message 15 Iterator<ProcessingMessage> it = report.iterator(); 16 Assert.assertEquals( 17 "instance type (string) does not match any allowed primitive type (allowed: [\"integer\"])", 18 it.next().getMessage()); 19 20 } 21 22 private JsonNode readJSONfile(String filePath) { 23 JsonNode instance = null; 24 try { 25 instance = new JsonNodeReader().fromReader(new FileReader(filePath)); 26 } catch (FileNotFoundException e) { 27 e.printStackTrace(); 28 } catch (IOException e) { 29 e.printStackTrace(); 30 } 31 return instance; 32 }
官方文档: http://json-schema.org/
GitHub: json-schema-validator
该文同时发表在:http://www.cnblogs.com/wade-xu/p/4662127.html
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:json schema validation 数据规范
原文地址:http://blog.csdn.net/chndata/article/details/47296281