json - JSON 模式是否应该要求文档声明 `$schema`(自引用)?

标签 json jsonschema

JSON-schema 表示 JSON 文档可以使用 the $schema property 声明文档符合的模式。 。示例:

{
    "$schema": "http://example.com/example_fancy_schema#",
    "example_fancy_property": "cute fluffy kittens"
}

架构如下所示:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Example Fancy Schema",
    "description": "The schema that describes the example format.",
    "type": "object",
    "properties": {
        "example_fancy_property": {
            "type": "string",
            "enum": ["cute fluffy kittens"]
        }
    },
    "additionalProperties": false,
    "required": [ "example_fancy_property" ]
}

这是否意味着应该在实际架构中为此添加一个属性,例如:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Example Fancy Schema",
    "description": "The schema that describes the example format.",
    "type": "object",
    "properties": {
        "$schema": {
            "type": "string",
            "enum": ["http://example.com/example_fancy_schema#"]
        },
        "example_fancy_property": {
            "type": "string",
            "enum": ["cute fluffy kittens"]
        }
    },
    "additionalProperties": false,
    "required": [ "$schema", "example_fancy_property" ]
}

JSON 架构网站上的示例似乎都没有声明这一点,因此我怀疑不应该声明它。但我还是很好奇:)

最佳答案

建议在 JSON 模式中使用 $schema 关键字,以表示正在使用的模式标准的版本。

但是,它在数据中没有特殊含义。通过 HTTP,有 recommended ways将数据与架构关联起来,但 $schema 属性不是其中之一。

关于json - JSON 模式是否应该要求文档声明 `$schema`(自引用)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26494535/

相关文章:

json - 如何使用 Serde 使用顶级数组反序列化 JSON?

java - 如何正确地从 RESTful API 传递错误?

php - 如何将带有数据的 http header 发送到 rest Api codeigniter?

xml - 从 XML 模式 (XSD) 生成 Json 模式

json - 只允许在 JSON 模式中声明的属性

java - 将嵌套的 Java 对象序列化为 json

java - Jackson:在序列化时抑制 JsonTypeinfo?

json - 如何在 JSON 模式中将 additionalProperties 与 allOf 一起使用?

typescript - 如何使用 webpack 将 json 模式动态加载到 typescript

Java - 如何从 JSON Schema 获取所有字段名称