一、介绍
json schema是用来描述、校验json文档(json数据结构)的一个标准。
- 官网:http://json-schema.org/
- github:https://github.com/json-schema-org/json-schema-spec
- google groups:https://groups.google.com/g/json-schema?pli=1
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开始,按出版的年和月确定元模式。
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文档数据进行校验。
- .NET
- Json.NET Schema 2019-09, draft-07, -06, -04, -03 (AGPL-3.0-only)
- Manatee.Json 2019-09, draft-07, -06, -04 (MIT)
- C
- C++
- f5-json-schema draft-07 (Boost Software License 1.0)
- JSON schema validator for JSON for Modern C++ draft-07 (MIT)
- Valijson draft-07 header-only library, works with many JSON parser implementations (BSD-2-Clause)
- Clojure
- jinx draft-07 (MIT)
- json-schema draft-07 (Apache License, Version 2.0)
- Common Lisp
- json-schema 2019-09, draft-04, -06, -07 (LGPL)
- Elixir
- Elixir JSON Schema validator draft-04 Draft-06+ progress: issue 24; branch multi-draft-support (MIT)
- JsonXema draft-07, -06, -04 (MIT)
- Go
- gojsonschema draft-07, -06, -04 (Apache 2.0)
- santhosh-tekuri/jsonschema draft-07, -06, -04 (BSD-3-Clause)
- qri-io/jsonschema 2019-09, draft-07 includes custom validator support, rich error returns (MIT)
- Java
- Snow 2019-09, draft-07, -06 Uses Maven for the project and Gson under the hood. (GNU Affero General Public License v3.0)
- everit-org/json-schema draft-07, -06, -04 (Apache License 2.0)
- Justify draft-07, -06, -04 (Apache License 2.0)
- networknt/json-schema-validator draft-07, -06, -04 Support OpenAPI 3.0 with Jackson parser (Apache License 2.0)
- Kotlin
- Medeia-validator draft-07, -06, -04 streaming validator for Kotlin and Java clients; works with Jackson and Gson (Apache License 2.0)
- JavaScript
- ajv draft-07, -06, -04 for Node.js and browsers - supports custom keywords and $data reference (MIT)
- djv draft-06, -04 for Node.js and browsers (MIT)
- Hyperjump JSV 2019-09, draft-07, -06, -04 Built for Node.js and browsers. Includes support for custom vocabularies. (MIT)
- vue-vuelidate-jsonschema draft-06 (MIT)
- @cfworker/json-schema 2019-09, draft-07, -06, -04 Built for Cloudflare workers, browsers, and Node.js (MIT)
- Perl
- JSON::Validator draft-07, -06, -04 (The Artistic License 2.0 (GPL Compatible))
- JSON::Schema::Draft201909 2019-09 (GNU General Public License, Version 1 + The Artistic License 1.0)
- PHP
- Opis Json Schema draft-07, -06 (Apache License 2.0)
- Swaggest Json Schema draft-07, -06, -04 (MIT)
- Python
- jsonschema draft-07, -06, -04, -03 (MIT)
- fastjsonschema draft-07, -06, -04 Great performance thanks to code generation. (BSD-3-Clause)
- jsonschema-rs draft-07, -06, -04 Python bindings to Rust’s jsonschema crate (MIT)
- Ruby
- JSONSchemer draft-07, -06, -04 (MIT)
- Rust
- jsonschema-rs draft-07, -06, -04 Fast due to compiling schema into a validation tree (MIT)
- Objective-C
- DSJSONSchemaValidation draft-07, -06, -04 (MIT)
- Lua/LuaJIT
- lua-resty-jsonschema draft-07, -06, -04 (MIT)
- Web (Online)
- Hyperjump JSV 2019-09, draft-07, -06, -04 Supports multiple schemas and multiple instances
- JSON Schema Validator 2019-09, draft-07, -06, -04, -03
- JSON Schema Lint draft-07, -06, -04, -03, -02, -01
- ExtendsClass's JSON Schema Validator draft-07
- Command Line
- ajv-cli draft-07, -06, -04 (MIT)
- Polyglottal JSON Schema Validator draft-06, -04 can be used with YAML and many other formats besides JSON (MIT)
- yajsv draft-07, -06, -04 wraps xeipuuv/gojsonschema (MIT)
2、生成json schema:
根据json文档(或者其他数据类型,如java bean),生成对应的json schema。
2.1)根据json文档生成json schema:
- Java
- saasquatch/json-schema-inferrer draft-07, -06, -04 (Apache 2.0) - Java library for inferring JSON Schemas from one or multiple JSON samples.
- 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
- luposlip/json-schema (Apache 2.0) - infer JSON Schema from Clojure data
- 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
- scala-jsonschema (Apache 2.0) - generates schemad out of Scala case classes
3、根据json schema生成其他组件
我们可以定义一个json schema的文档,通过该文档来生成其他组件(java bean、web界面...)
3.1)生成其他类型数据:
- Delphi
- DJsonSchema (MIT) - JSON Schema reader and code generator for Delphi.
- Elm
- json-schema-to-elm - generates Elm types, JSON decoders+encoders, and fuzz tests from one or more JSON Schema files, using dragonwasrobot/json_schema supports Draft 7
- 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
- php-code-builder(MIT) - generates PHP mapping structures defined by JSON schema using swaggest/json-schema supports Draft 7
- 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:
- Alpaca Forms (ASL 2.0)
- Angular Schema Form (MIT)
- Angular2 Schema Form unrelated to Angular Schema Form (MIT)
- Angular6-json-schema-form (MIT)
- Dashjoin JSON Schema Form (Apache 2) draft-06 (minus oneOf, anyOf, allOf, not)
- JSON Editor (MIT)
- JSON Form (joshfire) (joshfire) (MIT)
- Json Forms (brutusin) (brutusin) (MIT)
- JSONForms (jsonforms.io) (EclipseSource) (MIT)
- Liform-react (MIT)
- React JSON Schema Form (mozilla) (Apache 2)
- React Schema Form (networknt) (MIT)
- uniforms (Vazco) (MIT)