定义JSON结构是否需要JSON模式? [英] Are JSON schemas necessary for defining the structure of a JSON?

查看:111
本文介绍了定义JSON结构是否需要JSON模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之所以问这个问题,是因为我看到了当前的JSON模式草案( http://json-schema.org/)建议以以下方式使用JSON模式:

I am asking this because I see that the current JSON schema draft (http://json-schema.org/) proposes to have the schema of JSON in the following way:

对于JSON:

{
 "a":"abc"
 "b": 123
}

草案中提出的模式就像

{
 "type":"object"
 "properties":{
  "a": {"type":"string"}
  "b": {"type":"integer"}
 }
}

我的问题是JSON本身是否未定义其结构?是否需要单独的架构?

My question here is does the JSON itself not define its structure? Is a separate schema necessary?

草案提出的模式验证了具有上述结构的JSON,并且这些JSON始终具有以下格式

The schema proposed by the draft validates the JSON that have the above structure and those JSON are always of the format

{
 "a":"string"
 "b": 1 (or some number)
}

那么对JSON需要一个单独的架构是什么.我们也可以简单地使用JSON定义其结构.

So what is the need of a separate schema for JSON. We can simply use the JSON to define its structure also.

PS.我知道我们可以通过草案中提出的模式对JSON可以采用的值进行一些限制,但是从定义JSON结构的角度来看,提出的模式是否必要?

PS. I know that we can specify some restrictions on the values that the JSON can take through the schemas proposed in the draft, but from the point of view of defining structure of a JSON, are the proposed schemas necessary?

推荐答案

JSON本身未定义结构.例如,我可以写:

The JSON itself does not define the structure. For example, I could write:

{
  "a": "string",
  "b": "another string"
}

这也是有效的JSON-但它是结构不同"的JSON,因为"b"现在是字符串.但是您的API可能只接受具有特定结构的JSON,因此尽管它是有效的JSON,但并不是您需要的形状.

That's also valid JSON - but it's "differently structured" JSON, because "b" is now a string. But your API might only accept JSON with a particular structure, so although it's valid JSON, it's not the shape you need.

现在,您是否需要 JSON模式来定义JSON数据的结构?不,您可以说:

Now, do you need JSON Schema to define the structure of your JSON data? No. You could instead say:

该值必须是一个对象.它必须具有两个属性:

The value must be an object. It must have two properties:

  • "a"-必须为字符串
  • "b"-必须为整数
  • "a" - must be a string
  • "b" - must be an integer

程序员可以很容易地理解这一点,而无需花哨的括号或任何东西.

A programmer could understand this very easily, with no squiggly brackets or anything.

但是,对格式进行机器可读描述是有优势的,因为它使您可以自动化各种操作(例如,测试,生成文档,生成代码/类等).

However, there are advantages to having a machine-readable description of the format, because it lets you automate various things (e.g. testing, generating documentation, generating code/classes, etc.)

编辑:如注释中所指出的,您可以从一些示例数据中获取类型信息,并将其用作其他数据的模型.在这种情况下,您基本上是将示例数据用作超基本模式.

As pointed out in the comments, you can take the type information from some example data, and use that as a model for other data. In this case, you're basically using your example data as a super-basic schema.

对于非常简单的约束(基本类型),此方法有效.但是,您怎么说"b"必须是整数而不是浮点数?您怎么说"b"必须大于0?您怎么说"a"一定不能是空字符串("")?

For very simple constraints (basic type), this works. However, how would you say that "b" has to be an integer instead of a float? How do you say that "b" must be > 0? How do you say that "a" must not be the empty string ("")?

确实有一些工具可以从示例数据中生成基本的JSON模式-但是,生成的模式通常需要进行一些调整才能实际描述格式(例如,最小/最大,必需/可选属性等).

There are indeed tools that generate a basic JSON Schema from example data - however, the resulting schema usually requires a bit of tweaking to actually describe the format (e.g. min/max, required/optional properties, etc.).

这篇关于定义JSON结构是否需要JSON模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆