📜  JSON-模式

📅  最后修改于: 2020-11-13 05:09:23             🧑  作者: Mango


JSON模式是基于JSON格式的规范,用于定义JSON数据的结构。它是根据IETF草案编写的,该草案于2011年到期。JSON Schema-

  • 描述您现有的数据格式。
  • 清晰的,人机可读的文档。
  • 完整的结构验证,对于自动化测试很有用。
  • 完整的结构验证,验证客户端提交的数据。

JSON模式验证库

当前有几种可用于不同编程语言的验证器。当前,最完整和最合规的JSON Schema验证器是JSV。

Languages Libraries
C WJElement (LGPLv3)
Java json-schema-validator (LGPLv3)
.NET Json.NET (MIT)
ActionScript 3 Frigga (MIT)
Haskell aeson-schema (MIT)
Python Jsonschema
Ruby autoparse (ASL 2.0); ruby-jsonschema (MIT)
PHP php-json-schema (MIT). json-schema (Berkeley)
JavaScript Orderly (BSD); JSV; json-schema; Matic (MIT); Dojo; Persevere (modified BSD or AFL 2.0); schema.js.

JSON模式示例

下面给出的是一个基本的JSON模式,涵盖了经典的产品目录描述-

{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "title": "Product",
   "description": "A product from Acme's catalog",
   "type": "object",
    
   "properties": {
    
      "id": {
         "description": "The unique identifier for a product",
         "type": "integer"
      },
        
      "name": {
         "description": "Name of the product",
         "type": "string"
      },
        
      "price": {
         "type": "number",
         "minimum": 0,
         "exclusiveMinimum": true
      }
   },
    
   "required": ["id", "name", "price"]
}

让我们检查一下可以在此架构中使用的各种重要关键字-

Sr.No. Keyword & Description
1

$schema

The $schema keyword states that this schema is written according to the draft v4 specification.

2

title

You will use this to give a title to your schema.

3

description

A little description of the schema.

4

type

The type keyword defines the first constraint on our JSON data: it has to be a JSON Object.

5

properties

Defines various keys and their value types, minimum and maximum values to be used in JSON file.

6

required

This keeps a list of required properties.

7

minimum

This is the constraint to be put on the value and represents minimum acceptable value.

8

exclusiveMinimum

If “exclusiveMinimum” is present and has boolean value true, the instance is valid if it is strictly greater than the value of “minimum”.

9

maximum

This is the constraint to be put on the value and represents maximum acceptable value.

10

exclusiveMaximum

If “exclusiveMaximum” is present and has boolean value true, the instance is valid if it is strictly lower than the value of “maximum”.

11

multipleOf

A numeric instance is valid against “multipleOf” if the result of the division of the instance by this keyword’s value is an integer.

12

maxLength

The length of a string instance is defined as the maximum number of its characters.

13

minLength

The length of a string instance is defined as the minimum number of its characters.

14

pattern

A string instance is considered valid if the regular expression matches the instance successfully.

您可以检查http://json-schema.org ,以获取可用于定义JSON模式的关键字的完整列表。上面的模式可用于测试以下JSON代码的有效性-

[
   {
      "id": 2,
      "name": "An ice sculpture",
      "price": 12.50,
   },
    
   {
      "id": 3,
      "name": "A blue mouse",
      "price": 25.50,
   }
]