目录
1. 什么是JSON Schema?
2. 如何定义一个JSON Schema
3. 如何测试JSON Schema
a) 使用JSON Schema validator GUI
b) 在Java code里使用JSON Schema validator
4.参考文档

什么是JSON Schema?

JSON模式是基于JSON格式定义JSON数据结构的规范。

  • 描述现有的数据格式
  • 干净的人类和机器可读的文档
  • 完成结构验证, 用户
    • 自动化测试
    • 验证客户端提交的数据

如何定义一个JSON Schema

一个简单的例子

JSON Data 如下

"Hello, World"

JSON Schema 定义成

{
"type": "string"
}

用这个Schema 我们就可以来验证JSON数据

根据Data来生成JSON Schema 有现成的工具可以用http://jsonschema.net/#/

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.html

接下来看一个基本的JSON Schema

{
"$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

##转载注明出处: http://www.cnblogs.com/wade-xu/p/4662127.html

如何测试JSON Schema

当我们编写了一个JSON Schema 用于对客户端提交的数据进行验证之前,我们得确保我们编写的JSON Schema是正确的,我们当然就可以构造一些数据反向验证我们的JSON Schema的正确性与否。

网上有三十多个各种语言实现的JSON Schema validator, 我们用的是Java 里非常流行的,GitHub地址在这里

使用JSON Schema validator GUI

地址 http://json-schema-validator.herokuapp.com/

Validation results: success

Validation results: failure

Error Message的信息非常详细。

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.html

在Java code里使用JSON Schema validator

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>
 import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonNodeReader;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

Validation success

     @Test
public void validate_driverSet_schema() { //some code to create Json schema, which we want to use data to validate JsonNode schema = readJSONfile("src/test/resources/json/Schema.json"); JsonNode data = readJSONfile("src/test/resources/json/DataGoodExample.json"); ProcessingReport report =
JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data);
Assert.assertTrue(report.isSuccess());
}

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.html

Validation failure

  @Test
// wrong data
public void validate_driverSet_schema2() { //some code to create Json schema, which we want to use data to validate JsonNode data = readJSONfile("src/test/resources/json/DataBadExample.json");
JsonNode schema = readJSONfile("src/test/resources/json/Schema.json"); ProcessingReport report =
JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data);
Assert.assertFalse(report.isSuccess()); // assert error message
Iterator<ProcessingMessage> it = report.iterator();
Assert.assertEquals(
"instance type (string) does not match any allowed primitive type (allowed: [\"integer\"])",
it.next().getMessage()); } private JsonNode readJSONfile(String filePath) {
JsonNode instance = null;
try {
instance = new JsonNodeReader().fromReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return instance;
}

参考文档

官方文档: http://json-schema.org/

GitHub: json-schema-validator

感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮,您的鼓励是我创作的动力。

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.html

技术那么多,你想看看JSON Schema的测试吗?的更多相关文章

  1. rest-assured之Schema validation(包括JSON Schema validation及Xml Schema validation)

    rest-assured从2.1.0版本开始支持  Schema 验证,包括JSON Schema validation及Xml Schema validation.我们之前断言响应体都是一个一个字段 ...

  2. .net 项目使用 JSON Schema

    .net 项目使用 JSON Schema 最近公司要做配置项的改造,要把appsettings.json的内容放到数据库,经过分析还是用json的方式存储最为方便,项目改动性最小,这就牵扯到一个问题 ...

  3. Json Schema的使用

    直接上案例: 在Web Api通讯中,客户端发送json数据,服务端反序列化json(json与某个类形成对应关系),在某些情况下,需要校验其上传的json是否合法. 服务端是使用Json.net(n ...

  4. Json Schema简介

    1. 引言 什么是Json Schema? 以一个例子来说明 假设有一个web api,接受一个json请求,返回某个用户在某个城市关系最近的若干个好友.一个请求的例子如下: { "city ...

  5. Json schema 以及在python中的jsonschema

    目录 1. JSON Schema简介 2. JSON Schema关键字详解 2.1 $schema 2.2 title和description 2.3 type 3 type常见取值 3.1 当t ...

  6. Understanding JSON Schema

    json schema 在线校验器 译自:Understanding JSON Schema { "type": "object", "propert ...

  7. Map传参优雅检验,试试json schema validator

    背景 笔者目前所在团队的代码年代已久,早年规范缺失导致现在维护成本激增,举一个深恶痛疾的例子就是方法参数使用Map"一撸到底",说多了都是泪,我常常在团队内自嘲"咱硬是把 ...

  8. 使用JSON Schema来验证接口数据

    最近在做一些关于JSON Schema的基建,JSON Schema可以描述一个JSON结构,那么反过来他也可以来验证一个JSON是否符合期望的格式. 如果之前看我写的<使用joi来验证数据模型 ...

  9. Json.Net使用JSON Schema验证JSON格式【实例】

    给出一个Json,验证其格式是否符合规则. { "coord": { //对象 "lon": 145.77, "lat": -16.92 } ...

随机推荐

  1. speechSynthesis

    /* $timeout.cancel(timer); */window.speechSynthesis.onvoiceschanged = function(e) { voices = speechS ...

  2. 当BPM业务流程管理遇上制造业

    2015年5月,K2正式与赛科利签约,准备上线核心采购类流程,包括PR.PO.Payment.供应商管理等. 上海赛科利汽车模具技术应用有限公司隶属于上汽集团,现有员工2300余人.为解决汽车“安全” ...

  3. java面试准备之基础排序——冒泡与选择排序

    选择排序:     [java]    public void select(int[] arr){            for(int i=0;i<arr.length;i++){      ...

  4. android 总结(样式)—跑马灯 button的点击效果 RadioGroup 实现滑动的效果 button 下面有阴影 卡片样式

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content&qu ...

  5. Android 自动生成表格

    Layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  6. HDFS中Java的API使用测试

    import java.io.IOException; import java.util.Arrays; import java.util.Date; import java.util.Scanner ...

  7. highcharts异步获取数据

    页面异步代码 $(function () { var chart_validatestatics; $(document).ready(function () { var options_valida ...

  8. github常见操作和常见错误!错误提示:fatal: remote origin already exists.

    如果输入$ git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remote ...

  9. Android刷新Dialog

    在编写数独游戏时遇到一个问题,当我一次游戏成功后会弹出一个dialog,告诉玩家当前的游戏难度,积分和所用时间,我在onCreateDialog中setMessage之后发现内容一直是不变的,后来找到 ...

  10. 一种工业级系统交互建模工具的应用--EventStudio System Designer

    一种工业级系统交互建模工具的应用 [摘要] 本文以探索如何维护大规模复杂系统交互设计模型为目的,以EventHelix公司的商业付费软件EventStudio System Designer为建模工具 ...