JSON Schema介绍1

294 篇文章 13 订阅

一、介绍

json schema是用来描述校验json文档(json数据结构)的一个标准。

1、JSON Schema是什么:

1)JSON Schema

  • Describes your existing data format(s). (json schema用来描述json文档)
  • Provides clear human- and machine- readable documentation. (json schema是机器、人可读的文档)
  • Validates data which is useful for:
    • Automated testing. 
    • Ensuring quality of client submitted data.

2)JSON Hyper-Schema

  • Make any JSON format a hypermedia format with no constraints on document structure
  • Allows use of URI Templates with instance data
  • Describe client data for use with links using JSON Schema.
  • Recognizes collections and collection items.

可以看到JSON Schema包含两大块功能。我们常用的是第一个,下面我们也是重点介绍第一个。

2、JSON Schema 规范:

2019年9月16日发布了(最新):2019-09(以前称为draft-08)!

1)命名规范和序列号:

IETF Internet-Drafts (I-Ds) 中使用了编写者姓名+序列号方式命名,但是序列号会随着新的一个编辑者而重置。Meta-schemas采用draft-nn的方式命名,为了不引起混乱,从draft-08开始,按出版的年和月确定元模式。

IETF identifiersmeta‑schema identifierscommon namenotes
draft‑zyp‑json‑schema‑00draft‑00Draft 0due to a markup error, this draft was replaced by draft‑01 on the same day
draft‑zyp‑json‑schema‑01draft‑01Draft 1 
draft‑zyp‑json‑schema‑02draft‑02Draft 2 
draft‑zyp‑json‑schema‑03draft‑03Draft 3 
draft‑zyp‑json‑schema‑04
draft‑fge‑json‑schema‑validation‑00
draft‑luff‑json‑hyper‑schema‑00
draft‑pbryan‑zyp‑json‑ref‑03
draft‑04Draft 4json‑ref drafts 00‑02 were all published between json‑schema‑03 and json‑schema‑04
draft‑wright‑json‑schema‑00
draft‑wright‑json‑schema‑validation‑00
draft‑wright‑json‑schema‑hyperschema‑00
Draft 5meta‑schemas not changed, so "draft‑05" is really draft‑04
Milestone: draft-5 (2016-10-13)
draft‑wright‑json‑schema‑01
draft‑wright‑json‑schema‑validation‑01
draft‑wright‑json‑schema‑hyperschema‑01
draft‑06Draft 6Milestones: draft-6Meta-schema draft-06
draft‑handrews‑json‑schema‑00
draft‑handrews‑json‑schema‑validation‑00
draft‑handrews‑json‑schema‑hyperschema‑00
draft‑handrews‑relative‑json‑pointer‑00
draft‑07Draft 7the draft‑handrews‑*‑01 drafts were bugfixes and/or clarifications without meta‑schema or functional changes
Milestone: draft-07
draft‑handrews‑json‑schema‑01
draft‑handrews‑json‑schema‑validation‑01
draft‑handrews‑json‑schema‑hyperschema‑01
draft‑handrews‑relative‑json‑pointer‑01
draft‑handrews‑json‑schema‑02
draft‑handrews‑json‑schema‑validation‑02
draft‑handrews‑json‑schema‑hyperschema‑02
draft‑handrews‑relative‑json‑pointer‑02
2019‑09Draft 8Milestone: draft-08
(TBD, probably only a subset of the documents)bugfixes and/or clarifications, no functional changes
Milestone: draft-08-patch-1
(TBD)(TBD)Draft 9Milestone: draft-09

2)older drafts:

详情见:http://json-schema.org/specification-links.html

3、标准化之路:

JSON Schema项目打算将所有四个draft(草案)系列推广到RFC状态。目前,我们正在继续完善自己发布的nternet-Drafts。下一步将是获得IETF工作组通过的草案。

补充:关于IETF和RFC的相关知识见:https://blog.csdn.net/liuxiao723846/article/details/108553456

二、实现

官网连接:http://json-schema.org/implementations.html

根据上面”json schema是什么“可以得出,JSON Schema主要用来定义以下两个标准:

  • 描述json文档:根据json数据文档(或者其他),生成对应的json schema;
  • 校验json文档:根据json schema对json数据进行校验

注:原始json文档称作instance,用于描述、校验该json文档称为schema(本身也是一个json) 。这里的描述指的是双向的,即:通过json生成json schema,也可以通过json schema 生成其他的组件(web UI...)

以下实现用不同的语言编写,并且支持该规范的至少一个最新版本的一部分或全部。

1、校验:

根据json schema 对 json文档数据进行校验。

2、生成json schema:

根据json文档(或者其他数据类型,如java bean),生成对应的json schema。

2.1)根据json文档生成json schema:

  • Java
  • Scala
    • Schema Guru (Apache 2.0) - CLI util, Spark Job and Web UI for deriving JSON Schemas out of corpus of JSON instances; see issue 178 for progress towards draft-06+ support
  • Clojure
  • Online (web tool)
    • jsonschema.net - generates schemas from example data
    • quicktype.io - infer JSON Schema from samples, and generate TypeScript, C++, go, Java, C#, Swift, etc. types from JSON Schema

2.2)根据code生成json schema:

  • .NET
    • Json.NET (AGPL-3.0) - generates schemas from .NET types
    • NJsonSchema - (Ms-PL) - generates schemas from .NET types, see issue 574 for draft-06+ support progress
  • PHP
    • Liform (MIT) - generates schemas from Symfony forms
  • TypeScript
  • Python
    • Pydantic (MIT) - generates schemas from Python models based on Python 3.6+ type hints.
  • Java
    • jsonschema-generator (Apache 2.0) - generates schemas from Java types supports Draft 7 and Draft 2019-09
  • Scala

3、根据json schema生成其他组件

我们可以定义一个json schema的文档,通过该文档来生成其他组件(java bean、web界面...)

3.1)生成其他类型数据:

  • Delphi
    • DJsonSchema (MIT) - JSON Schema reader and code generator for Delphi.
  • Elm
  • Java
    • jsonCodeGen (MIT) - Groovy based generation tasks from JSON schema. Already includes templates/generators for Java Beans, Swagger specification files and PlantUML diagrams.
  • Online (web tool)
    • quicktype.io - infer JSON Schema from samples, and generate TypeScript, C++, go, Java, C#, Swift, etc. types from JSON Schema
  • PHP
  • Python
    • yacg (MIT) - parse JSON Schema and OpenApi files to build a meta model from them. This meta model can be used in Mako templates to generate source code, other schemas or plantUml.
  • Rust
    • schemafy - generates Rust types and serialization code from a JSON schema. supports Draft 4

3.2)生成web UI:

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值