category
Technology
Tags
JSON
JSONSchema
Collection
CreateTime
Jun 14, 2022 11:00 AM
UpdateTime
Dec 11, 2023 12:12 PM
Published
Published
相关资料
JSON
JSON Schema 校验
JSON Schema 生成
JSON简介语法数据结构JSONSchema简介关键字$schema $id titledescriptiondefaultexamplestypepatternenumconstApplying subschemas conditionallyCombining schemascomplex schemacontentMediaTypecontentEncoding$comment
JSON
简介
JSON是一种轻量级的数据交换格式
语法
- 数据使用名/值对表示
- 使用大括号保存对象,每个名称后面跟着一个 ':'(冒号),名/值对使用 ,(逗号)分割。
- 使用方括号保存数组,数组值使用 ,(逗号)分割。
数据结构
- object(对象)
{ "a" : { "b": "c" } }
- array (数组)
21{ "a": [1, 2, 3] }
- string (字符串)
{ "a": "a" }
- number (整数 | 浮点数):
{ "a": 1 }
- boolean(true | false)
{ "a": true }
- null
{ "a": null }
JSONSchema
简介
基于JSON格式,定义JSON数据结构、校验数据内容
eg:Http返回内容校验
关键字
$schema
关键字被用于声明一个JSON片段实际上是一块JSONSchema的。它还声明了针对该架构编写的JSON Schema标准版本
{ "$schema": "http://json-schema.org/draft/2019-09/schema" }
https://json-schema.org/draft/2020-12/schema
https://json-schema.org/draft/2019-09/schema
https://json-schema.org/draft-07/schema
https://json-schema.org/draft-06/schema
https://json-schema.org/draft-04/schema
$id
- 当前schema唯一id标识
- 它声明一个基础URI,可以根据该基础URI
$ref引用进行解析
- 该$id属性绝不能为空字符串或空片段(#),因为这实际上没有任何意义。
{ "$id": "http://example.com/example.json" }
title
标题,字符串
description
描述,字符串
default
该关键字常常用来指定待校验JSON元素的默认值,当然,这个默认值最好是符合要求的,即能够通过相应的JSON Schema的校验。另外,需要注意的是,该关键字除了提示作用外,并不会产生任何实质性的影响。
examples
关键字是用于提供一系列 针对该模式进行验证的示例的地方。这不用于验证,但是可以帮助向读者解释该模式的效果和目的。每个条目都应针对所驻留的架构进行验证,但这不是严格要求的。无需重复数组中的值 ,因为这将作为另一个示例
type
指定模式的数据类型,可以是字符串或者数组.
字符串
{ "type": "number" }
42
42.0
"42"
数组:与任一类型匹配则有效
{ "type": ["string", "number"] }
42
"42"
["123"]
Type-specific keywords (1)pattern
正则表达式
enum
用于限制值,以固定的一组值,至少包含一个元素的数组,每个元素都是唯一的。
{ "type": "string", "enum": ["red", "amber", "green", null] }
"red"
null
const
用于限定值,以一个单一的值
{ "properties": { "country": { "const": "United States of America" } } }
{ "country": "United States of America" }
{ "country": "Canada" }
对于
enum 来说是一个语法糖{ "const": "United States of America" } { "enum": [ "United States of America" ] }
Applying subschemas conditionally
Applying subschemas conditionally (1)Combining schemas
Combining schemas (1)complex schema
complex schema (1)contentMediaType
关键字指定的MIME类型的字符串的内容
{ "type": "string", "contentMediaType": "text/html" }
"<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head></html>"
contentEncoding
关键字指定的编码用于存储内容
- 7bi
- 8bit
- binary
- quoted-printable
- base64
{ "type": "string", "contentEncoding": "base64", "contentMediaType": "image/png" }
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABmJLR0QA/wD/AP+gvaeTAAAA..."
