# jsonschematest **Repository Path**: singdown/jsonschematest ## Basic Information - **Project Name**: jsonschematest - **Description**: json schema 引用测试,draft04 版本 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2020-05-20 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Why? 目前主流的报文格式主要有XML和JSON,而其中XML有XSD进行校验,而JSON则有JSON schema进行校验。 # What? JSON 全称 JavaScript Object Notation(JavaScript 对象简谱),是从JavaScript中衍生的东西,所以他和JavaScript语言几乎就是父子关系,故JSON能够无缝对接在Javascript中,其表现形式为JSON能够直接粘贴在javascript中声明一个对象,javascript语言也能够使用 JSON.parse(str)方法和JSON.stringify(obj)方法对字符串中的JSON内容和对象转换。 > 一个简单的JSON: {"field1":"field1value","numbervalue":123,"objectValue":{"objectf1":"objectv1","objectf2":12313},"arrayValue":["arr1","arr2"]} ---- > JSON在JS中声明一个对象 ![2019/03/image-b9bb99c7.png](http://192.9.200.223:8081/symphony/upload/2019/03/image-b9bb99c7.png) ---- > JSON和字符串互相转换 ![2019/03/image-72fe2971.png](http://192.9.200.223:8081/symphony/upload/2019/03/image-72fe2971.png) JSON schema 是 JSON 引申出来的用于校验JSON的格式规范。 [点我看详细的JSON schema 格式 https://blog.csdn.net/taiyangdao/article/details/77865622](https://blog.csdn.net/taiyangdao/article/details/77865622) 样例如下: ```json { "$schema":"http://json-schema.org/draft-04/schema", "type":"object", "properties": { "name": { "type":"string" }, "versions": { "type":"array", "items": { "type":"object", "properties": { "id": { "type":"string" }, "version": { "type":"integer" }, "comment": { "type":"string" } }, "required":["id", "version"], "minItems":1 } } }, "required":["name", "versions"] } ``` # How? 本文的目标就是建立JSON schema 并创建校验的样例程序。 由于网上在线校验工具不包含JSON schema引用,我们决定自己寻找技术方案来进行验证。 我们使用的技术是 https://github.com/java-json-tools/json-schema-validator 根据上面的说明,他实现的是JSON schema draft04版本的JSON schema,可以顺势找到其官方的协议定义文档 [https://tools.ietf.org/html/draft-zyp-json-schema-04](https://tools.ietf.org/html/draft-zyp-json-schema-04)。 这个文档不需要通读,只有在细节迷惑的地方才会去寻找这个官方文档找寻答案。 我们测试的案例如下 1. 测试JSON ```json { "$schema":"http://json-schema.agree.com.cn/scenario/ACD00009A88", "Head":{ "CONSUM_SYS_CODE":"CONSUM01", "PROVID_SYS_CODE":"PROVID01", "CHARACTER_SET":"UTF8", "COMM_TYPE":"00", "SERVICE_NAME":"ACD00001", "SCENES_CODE":"A01", "SERVICE_VERSION":"1.0.0", "SCENES_VERSION":"1.0", "SERVICE_REQ_SEQ":"12", "CONSUM_SEND_DATE":"20180101", "CONSUM_SEND_TIME":"121212000", "LOCAL_LANG":"zhg", "FILE_FLAG":"0" }, "Body":{ "Brc":"Brc1", "BrcName0":"15-其他金融计息类账户", "TermDate":"2019年3月4日22:04:51" } } ``` 2. 测试直接JSON Schema ```json { "$schema":"http://json-schema.org/draft-04/schema#", "id":"http://json-schema.agree.com.cn/scenario/ACD00009A88", "properties": { "Head":{ "$ref":"http://json-schema.agree.com.cn/REQheaderOfJSON#/definitions/SYS_HEAD" }, "Body":{ "title":"ACD00009A88", "type":"object", "properties":{ "Brc":{ "type":"string", "description":"sample1", "format":"9" }, "BrcName0":{ "type":"string", "description":"sample2", "format":"40", "enum":[ "15-其他金融计息类账户", "08-存放同业活期", "09-存放同业定期", "10-暂收暂付账号", "21-待销账主账号 ", "20-待销账账号", "99-其他(或全部)" ] }, "TermDate":{ "type":"string", "description":"sample3", "format":"10" } }, "required":[ "Brc", "BrcName0", "TermDate" ] } }, "required":[ "Head", "Body" ] } ``` 3. 测试间接引用的 JSON Schema ```json { "$schema": "http://json-schema.org/draft-04/schema#", "id":"http://json-schema.agree.com.cn/REQheaderOfJSON", "title": "REQheaderOfJSON", "type": "object", "description": "ESB-Standard-Message-head,ESB标准头信息校验Schema文件", "definitions": { "SYS_HEAD": { "type": "object", "description": "系统头节点", "properties": { "CONSUM_SYS_CODE": { "type": "string", "minLength": 8, "maxLength": 8, "description": "服务消费方系统编码" }, "PROVID_SYS_CODE": { "type": "string", "minLength": 8, "maxLength": 8, "description": "服务提供方系统编码" }, "CHARACTER_SET": { "type": "string", "minLength": 1, "maxLength": 10, "enum": [ "UTF8", "GBK", "GB2312", "GB18030" ], "description": "字符集" }, "COMM_TYPE": { "type": "string", "minLength": 2, "maxLength": 2, "enum": [ "00", "10" ], "description": "通讯类型" }, "SERVICE_NAME": { "type": "string", "minLength": 1, "maxLength": 20, "description": "服务名" }, "SCENES_CODE": { "type": "string", "minLength": 1, "maxLength": 3, "description": "场景编码" }, "SERVICE_VERSION": { "type": "string", "minLength": 1, "maxLength": 8, "description": "服务版本号" }, "SCENES_VERSION": { "type": "string", "minLength": 1, "maxLength": 8, "description": "场景版本号" }, "GLOBAL_SEQ": { "type": "string", "minLength": 1, "maxLength": 50, "description": "全局流水号" }, "SERVICE_REQ_SEQ": { "type": "string", "minLength": 0, "maxLength": 50, "description": "服务请求流水号" }, "CONSUM_SEND_DATE": { "type": "string", "minLength": 8, "maxLength": 8, "format": "date", "description": "服务消费方发起日期" }, "CONSUM_SEND_TIME": { "type": "string", "minLength": 9, "maxLength": 9, "description": "服务消费方发起时间" }, "LOCAL_LANG": { "type": "string", "minLength": 1, "maxLength": 3, "description": "本地语言" }, "SRC_ENC_NODE": { "type": "string", "minLength": 0, "maxLength": 20, "description": "源加密节点" }, "DEST_ENC_NODE": { "type": "string", "minLength": 0, "maxLength": 20, "description": "目标加密节点" }, "FILE_FLAG": { "type": "string", "minLength": 1, "maxLength": 1, "enum": [ "0", "1" ], "description": "文件标志" }, "FILE_PATH": { "type": "string", "minLength": 0, "maxLength": 512, "description": "文件路径" }, "MAC_ENC_NODE": { "type": "string", "minLength": 0, "maxLength": 20, "description": "MAC校验节点" }, "MAC": { "type": "string", "minLength": 0, "maxLength": 512, "description": "MAC值" }, "SYS_EXTEND": { "type": "string", "minLength": 0, "maxLength": 512, "description": "系统头扩展" } }, "required": [ "CONSUM_SYS_CODE", "PROVID_SYS_CODE", "CHARACTER_SET", "COMM_TYPE", "SERVICE_NAME", "SCENES_CODE", "SERVICE_VERSION", "SCENES_VERSION", "SERVICE_REQ_SEQ", "CONSUM_SEND_DATE", "CONSUM_SEND_TIME", "LOCAL_LANG", "FILE_FLAG" ] }, "PUB_DOMAIN": { "type": "object", "description": "公共头节点", "properties": { "LEGAL_CODE": { "type": "string", "minLength": 10, "maxLength": 10, "description": "法人代码" }, "BRANCH_ID": { "type": "string", "minLength": 0, "maxLength": 10, "description": "机构代码" }, "CHANNEL_CODE": { "type": "string", "minLength": 0, "maxLength": 6, "description": "业务渠道编码" }, "TRAN_CODE": { "type": "string", "minLength": 20, "maxLength": 20, "description": "交易码" }, "CONSUM_TRAN_DATE": { "type": "string", "minLength": 8, "maxLength": 8, "format": "date", "description": "服务消费方交易日期" }, "CONSUM_TRAN_TIME": { "type": "string", "minLength": 9, "maxLength": 9, "format": "time", "description": "服务消费方交易时间" }, "PROVID_TRAN_DATE": { "type": "string", "minLength": 8, "maxLength": 8, "format": "date", "description": "服务提供方交易日期" }, "TRAN_TELLER": { "type": "string", "minLength": 0, "maxLength": 12, "description": "交易操作员" }, "AUTH_TELLER": { "type": "string", "minLength": 0, "maxLength": 12, "description": "授权操作员" }, "AUTH_REQ_SEQ": { "type": "string", "minLength": 0, "maxLength": 50, "description": "授权请求流水号" }, "AUTH_STATUS": { "type": "string", "minLength": 0, "maxLength": 1, "description": "授权状态" }, "AUTH_FLAG": { "type": "string", "minLength": 2, "maxLength": 2, "format": "date", "description": "授权标志" }, "AUTH_INFO_NUM": { "type": "string", "minLength": 1, "maxLength": 2, "description": "授权信息笔数" }, "AUTH_INFO_ARRAY": { "type": "array", "items": { "type": "string", "examples": [ "AUTH_CODE", "AUTH_INFO" ] }, "description": "授权信息列表" }, "CONFIRM_STATUS": { "type": "string", "minLength": 2, "maxLength": 2, "description": "确认状态" }, "CONFIRM_FLAG": { "type": "string", "minLength": 2, "maxLength": 2, "format": "date", "description": "确认标志" }, "CONFIRM_INFO_NUM": { "type": "string", "minLength": 9, "maxLength": 9, "description": "确认信息笔数" }, "LOCAL_LANG": { "type": "string", "minLength": 1, "maxLength": 3, "description": "本地语言" }, "RET_STATUS": { "type": "string", "minLength": 0, "maxLength": 3, "description": "服务返回状态" }, "RET_INFO_NUM": { "type": "string", "minLength": 2, "maxLength": 2, "description": "服务返回信息笔数" }, "CONFIRM_INFO_ARRAY": { "type": "array", "items": { "type": "string", "examples": [ "CONFIRM_CODE", "CONFIRM_INFO" ] }, "description": "确认信息列表" }, "PER_PAGE_NUM": { "type": "string", "minLength": 1, "maxLength": 3, "description": "每页记录数" }, "CURR_PAGE_NUM": { "type": "string", "minLength": 1, "maxLength": 3, "description": "当前页码数" }, "PAGE_UP_DOWN": { "type": "string", "minLength": 1, "maxLength": 1, "description": "上翻/下翻标志" }, "PUB_EXTEND": { "type": "string", "minLength": 0, "maxLength": 512, "description": "公共域扩展" }, "required": [ "LEGAL_CODE", "BRANCH_ID", "CHANNEL_CODE", "TRAN_TELLER", "CONFIRM_STATUS" ] } } } } ``` 测试代码如下