what is a schema?作用与位置

if you have ever used XML Schema, RelaxNG or ASN. you probably already know what a schema is and you can happly skip along to the next section. if all that sounds like gobbledygook to you, you have come to right place. To define what JSON Schema is, we should probably fist define what JSON is.
JSON stands for “javaScript Object Notation”, a simple data interchange format. It began as a nataion for the world wide web. Since JavaScript exists in most web browers, and JSON is base on JavaScript, it is very easy to support there. However, it has proven useful enough and simple enough that it is now used in many other contexts that does not invole web surfing.
At its heart, JSON is built on the following data structures:

  • object: {“key1”:“value1”, “key2”:“value2”}
  • array: [“fist”, “second”, “third”]
  • number: 42
  • string: “This is a string”
  • boolean: true false
  • null: null
    these types have analogs in most programming languages, though they may go by different names.
    With these simple data types, all kinds of structured, With that great fiexibillty comes great responsibillty, however, as the same concept could be represented im myriad ways. For example, you could imagine prepresenting imformation about a person in JSON indifferent ways:
{
	"name":"Geoege Washington",
	"brithday":"February 22 , 1732",
	"address":"mount Vernon, Virginia, United States"
}

{
	"first_name":"George",
	"last_name":"Washington",
	"birthday":"1732-02-22",
	"address"{
		"street_address":"3200 Mount Vernon Memorial Highway",
		"city":"Mount Vernon",
		"state":"Virginia",
		"country":"United States"
	}
}

Both representations are equally valid, though one is clearly more formal than the other. The design of a record will largely depend on its intended use within the application, so there is no right or wrong answer here. However, when an application says “give me a JSON record for a person”, it’s important to know exactly how that record should be organized . For example, we need to know what fields are expected, and how the values are represented. That is where JSON Schema comes in. The following JSON Schema fragment describes how the second example above is structured. Do not very too much about the details for now. They are explained in subsequent chapters.

{
	"type":"object",
	"properties":{
		"first_name":{"type":"string"},
		"last_name":{"type":"string"},
		"brithday":{"type":"string", "format":"date"},
		"address":{
			"type":"object",
			"properties":{
				"street_address":{"type":"string"},
				"city":{"type":"string"},
				"state":{"type":"string"},
				"country":{"type":"string"}
			}
		}	
    }
}

By “validating” the first example against this schema, you can see that it fails;
However, the second example passes;

You may have noticed that the JSON schema itself is written in JSON. It is data itself, not a computer program. It is just a declarative format for “describing the structure of other data”. This is both its strength and its weakness(which it shares with other similar schema languages).it is easy to concisely describe the surface structure of data, and automate validating data against it. However, since a JSON Schema can not contain arbitrary code, there are certain constraints on the relationships between data elements that can not be expressed.**Any “validation tool” for a sufficiently complex data format, therefore, will likely have two phases of validation: one at the schema(or structural) level, and one at the semantic level.**The latter check will likely need to be implemented using a more general-purpose programming language.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值