https://www.imooc.com/article/67826
https://blog.csdn.net/zjg379569986/article/details/84634803(【JAVA】使用JSON Schema校验JSON数据是否合规)
Json?
了解json schema首先要知道什么是json?
json 是 JavaScript Object Notation 的缩写,它是一种简化的数据交换格式,是目前互联网服务间进行数据交换最常见的一种交换格式,具有简洁、可读性好等特点。
在json中常见的数据类型主要包括
-
object
{ "key1": "value1", "key2": "value2" }
-
array
[ "first", "second", "third" ]
-
number
42
3.1415926 -
string
"This is a string"
- boolean
true
false - null
null
一个示例json格式比如:
{
"fruits": [ "apple", "orange", "pear" ],
"vegetables": [
{
"veggieName": "potato",
"veggieLike": true
},
{
"veggieName": "broccoli",
"veggieLike": false
}
]
}
何为Json Schema
如前文所述,json是目前应用非常广泛的数据狡猾格式。既然是用于数据交换的格式,那么就存在数据交换的是双方,如何约定或校验对方的数据格式符合要求,就成了服务交互需要解决的一个问题。所以Json Schema就是用来定义json数据约束的一个标准。根据这个约定模式,交换数据的双方可以理解json数据的要求和约束,也可以据此对数据进行验证,保证数据交换的正确性。
目前最新的Json-schema版本是draft 7,发布于2018-03-19。下面我们就以官网的一个实例来看看Json-schema是如何进行数据约束以及其应用
如下是一个schema实例:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/product.schema.json",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"productId": {
"description": "The unique identifier for a product",
"type": "integer"
},
"productName": {
"description": "Name of the product",
"type": "string"
},
"price": {
"description": "The price of the product",
"type": "number",
"exclusiveMinimum": 0
},
"tags": {
"description": "Tags for the product",
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
},
"dimensions": {
"type": "object",
"properties": {
"length": {
"type": "number"
},
"width": {
"type": "number"
},
"height": {
"type": "number"
}
},
"required": [ "length", "width", "height" ]
}
},
"required": [ "productId", "productName", "price" ]
}
分析说明:
"$schema": "http://json-schema.org/draft-07/schema#",
说明当前使用的schema版本,可以不包含
"$id": "http://example.com/product.schema.json",
当前schema的唯一id标识,一般指向一个自主域名。方便后续引用,可以不包含
"title": "Product",
当前schema的标题,简要描述信息,可不包含
"description": "A product from Acme's catalog",
详细描述信息,可不包含
"type": "object",
约束对象是object,也就是在 { }
中的数据
"properties": {
"productId": {
"description": "The unique identifier for a product",
"type": "integer"
},
object中具体属性的约束,description是描述信息,不产生具体约束。
type约束productid
属性类型为整型
"productName": {
"description": "Name of the product",
"type": "string"
},
约束productName
属性类型为字符型
"price": {
"description": "The price of the product",
"type": "number",
"exclusiveMinimum": 0
},
约束price
属性类型为数字型,可以是整型或浮点型。exclusiveMinimum
约束该数字>0(不包含0)
"tags": {
"description": "Tags for the product",
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
},
约束tag
属性是array数组。items是数组项约束,这里约束数组项均为字符型minItems
数组至少包含1项。uniqueItems
约束数组中每项不得重复
"dimensions": {
"type": "object",
"properties": {
"length": {
"type": "number"
},
"width": {
"type": "number"
},
"height": {
"type": "number"
}
},
"required": [ "length", "width", "height" ]
}
},
约束dimensions
嵌套对象,其中length,width,height均为数字类型
且这三个字段在dimensions对象中必须包含
"required": [ "productId", "productName", "price" ]
当前数据对象必须包含productId,productName,price三个字段
以上是对Json-Schema的简要说明,详细介绍大家可以关注后续更新和相关实战课。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
相信很多小伙伴现在处理数据交互的时候都喜欢使用JSON,因为它使用方便易读,结构简单,语言无关。但是在使用JSON数据的过程中难免会遇到这样的问题,那就是我要发送/接受的JSON是否满足相应的格式要求。比如这个JSON数据中某个字段我不希望它是null的,某个字段的类型我希望它是Int而不是String。这个时候我们往往就需要对我们将要发送或是收到的JSON数据进行校验,看看这个JSON数据是不是满足我要求的JSON,如果不满足我就要及时捕获到其中不正确的地方。
今天我们就分享一种非常实用且十分方便进行维护的方法来验证JSON数据是否合规。那就是JSON Schema,其实很多语言都支持的,但是我主要分享的是在JAVA上的使用,如果想了解其他的语言使用的方法,可以直接去官网查看相关的文档,附上地址:http://json-schema.org/implementations.html。
JSON Schema的验证机制很简单,首先你需要维护一份JSON文件,这个文件就是标准的你想要的JSON文件格式,例如:
{
2 "definitions": {},
3 "$schema": "http://json-schema.org/draft-07/schema#",
4 "$id": "http://example.com/root.json",
5 "type": "object",
6 "title": "The Root Schema",
7 "required": [
8 "checked",
9 "dimensions",
10 "id",
11 "name",
12 "price",
13 "tags"
14 ],
15 "properties": {
16 "checked": {
18 "type": "boolean"
24 },
25 "dimensions": {
27 "type": "object",
29 "required": [
30 "width",
31 "height"
32 ],
33 "properties": {
34 "width": {
36 "type": "integer"
42 },
43 "height": {
45 "type": "integer"
51 }
52 }
53 },
54 "id": {
56 "type": "integer"
62 },
63 "name": {
65 "type": "string"
72 },
73 "price": {
75 "type": "number"
81 },
82 "tags": {
84 "type": "array",
86 "items": {
88 "type": "string",
96 }
97 }
98 }
99 }
这个JSON文件看着是不是会有点晕呢,没关系,你再看看下面这个JSON文件你就知道上面的文件是什么意思了
{
"checked": false,
"dimensions": {
"width": 5,
"height": 10
},
"id": 1,
"name": "A green door",
"price": 12.5,
"tags": [
"home",
"green"
]
}
其实啊,那份标准的JSON文件呢,就是把你需要的JSON格式描述了出来。其中“type”就是JSON中某个部分或是字段的类型,“title”就是这一部分或是字段的名称可以随意设置的,"required"就是你想要这个JSON中必须有的字段。以后校验的过程,其实就是把拿到的JSON去和这份定义好的标准的JSON文件做比较,如果满足设定呢就是正确的JSON,如果不满足其中的某个设定,例如:其中没有某个要求必存在的字段或者是其中某个字段的类型不对则都会抛出相应的异常。这个“标准的”JSON就类似于一份标准答案的功能。
大家可以自己照着这个格式去写,如果不会写也没关系,有可以自动生成的工具,在这里附上地址:https://jsonschema.net/。
有了这个“标准答案”之后我们就可以快速的写一个例子来尝试一下了。首先我们要在pom文件中引入
<dependency>
<groupId>com.github.everit-org.json-schema</groupId>
<artifactId>org.everit.json.schema</artifactId>
<version>1.9.2</version>
</dependency>
...
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
这个是目前最新的版本。大家如果对这个项目感兴趣可以直接去github看,在此附上地址:https://github.com/everit-org/json-schema
之后我们就可以快速开始尝试一下如何验证了。Demo如下:
//这个就是你设定的标准JSON
InputStream inputStream = getClass().getResourceAsStream("/Schema.json");
//这个是你打算验证的JSON,这里我也用一份文件来存放,你也可以使用string或者jsonObject
InputStream inputStream1 = getClass().getResourceAsStream("/failure.json");
JSONObject Schema = new JSONObject(new JSONTokener(inputStream));
JSONObject data = new JSONObject(new JSONTokener(inputStream1));
Schema schema = SchemaLoader.load(Schema);
try {
schema.validate(data);
} catch (ValidationException e) {
System.out.println(e.getErrorMessage());
你可以通过修改你打算验证的JSON来尝试看看输出的信息,上手非常简单,大家可以自己试一试,如果有什么疑问,欢迎随时交流。我已经把使用JSON Schema进行验证的机制和简单的例子分享给大家了,大家如果有更多的需求也可以到我文中分享的地址中去看看官方的代码和文档,我就不进行深层次分享了,希望对大家能有帮助。