繁体   English   中英

使用JSON模式验证JSON输入

[英]Validating JSON input with a JSON schema

如果您在输入文件中指定了在架构中配置为需求的元素,则表示它确定有效。 并且如果添加了“ maxItems”:1,则不在乎是否在输入文件中添加了另一个元素,验证器仍将其视为有效的输入文件。

即:架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "Books": {
            "type": "object",
            "minItems": 1,
            "properties": {
                "Book": {
                    "type": "object",
                    "minItems": 1,
                    "maxItems": 1,
                    "properties": {
                        "Author": {
                            "type": "string",
                            "minItems": 1,
                            "maxItems": 1
                        }
                    },
                    "required": ["Author"]
                }
            },
            "required": ["Book"]
        }
    },
    "required": ["Books"]
}

输入文件:

{
    "Books": {
        "Book": {
            "Author": "Andreas",
            "Author": "Geir"
        }
    }
}

这不应该是无效的输入文件吗?

验证者:

根据您定义的架构,给定的JSON是正确的。 模式所表示的是,对于每个对象Author ,JSON都应符合的最小值1和最大值1字符串属性。
除此之外, minItemsmaxItems属性是专门用于数组的 ,但是在您的定义中,它们位于objects下。 在底部的链接文档中了解有关此内容的更多信息。

引起混乱的部分是您期望数组是对象,而对象则是数组,有时很难区分。


简单来说:
JSON对象是一 key:value对。 就像您在定义对象(类)并使用OOP语言设置其属性值一样。
JSON对象的基本定义:

{
  "type": "object",
  "properties": {
    "MyString": {
      "type": "string"
    },
    "MyInterger": {
      "type": "integer"
    }
}

JSON阵列是相同的,有时类似,对象或单值的集合
JSON数组的基本定义:

{
  "type": "array",
  "items": {
    "type": "string"
  }
}

还可以帮助定义何时使用的内容,是想将您要创建的内容作为OOP语言中的对象。

例:

对于以下Book JSON对象 ,我可以想象如图所示的类结构,然后从中创建模式:

JSON:

{
  "Author": "First Author",
  "TotalPages": 128,
  "Chapters": [
    {
      "Number": 1,
      "Heading": "Chapter One"
    },
    {
      "Number": 2,
      "Heading": "Chapter Two"
    }
  ]
}

我们拥有的是

  • 两个基本对象 Author (string)TotalPages (integer)
  • Chapters对象的数组 ,其中包含两个基本对象Number (integer)Heading (string)

类表示:

public class Book
{
  public string Author { get; set; }
  public int TotalPages { get; set; }
  // Note the array
  public Chapter[] Chapters { get; set; } // Could be List<Chapter>
}

public class Chapter
{
  public int Number { get; set; }
  public string Heading { get; set; }
}

结果模式:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "Author": {
      "type": "string"
    },
    "TotalPages": {
      "type": "integer"
    },
    "Chapters": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "Number": {
            "type": "integer"
          },
          "Heading": {
            "type": "string"
          }
        }
      }
    }
  },
  "required": ["Author", "Chapters"]
}



现在您会注意到我故意省略了Books部分,因为那是Book 的数组/集合 如果要将其添加到JSON模式和类中,则需要这样定义它。 在讨论的同时,让我们还在每本书中为Keywords添加一个字符串数组,这很清楚如何定义它们。

首先,让我们更改所需的输出( JSON )。
我们希望我们的基础对象现在是Books ,并且应该是Book对象的集合,因此我们将Book 对象括在[ ]并为其添加一本书。 我们还向对象Book添加了一个Keywords集合。

{
  "Books":
  [
    {
      "Author": "First Author",
      "TotalPages": 128,
      "Chapters": [
        {
          "Number": 1,
          "Heading": "Chapter One"
        },
        {
          "Number": 2,
          "Heading": "Chapter Two"
        }
      ],
      "Keywords": [
        "This",
        "is",
        "book",
        "Alpha"
      ]
    },
    {
      "Author": "Second Author",
      "TotalPages": 256,
      "Chapters": [
        {
          "Number": 1,
          "Heading": "Erstes Kapitel"
        },
        {
          "Number": 2,
          "Heading": "Zweites Kapitel"
        }
      ],
      "Keywords": [
        "This",
        "is just",
        "Beta"
      ]
    }
  ]
}

现在我们有以下内容:

  • 一个对象Books包含我们先前定义的数组 Book对象。 (请注意, Book永远不会被命名,因为它将在JSON中添加另一个层次结构)
  • 除了先前定义的对象之外,我们还具有一个表示Keywordsstring 数组

让我们更改JSON的类/对象表示形式,这样做将有助于了解如何修改架构。

public class MyBookCollection
{
  // Note the array!!
  public Book[] Books { get; set; } // Could also be List<Book>
}

public class Book
{
  public string Author { get; set; }
  public int TotalPages { get; set; }
  // Note the arrays!!
  public Chapter[] Chapters { get; set; } // Could also be List<Chapter>
  public string[] Keywords { get; set; }  // Could also be List<string>
}

public class Chapter
{
  public int Number { get; set; }
  public string Heading { get; set; }
}

现在我们知道,当我们最终解析JSON时, 数据将是什么样。 让我们更改JSON模式,以便我们可以在验证器中使用一些东西。

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "Books": {
      "type": "array",
      "minItems": 1,
      "maxItems": 15,
      "title": "Book",
      "items": {
        "type": "object",
        "properties": {
          "Author": {
            "type": "string"
          },
          "TotalPages": {
            "type": "integer"
          },
          "Chapters": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "object",
              "properties": {
                "Number": {
                  "type": "integer"
                },
                "Heading": {
                  "type": "string"
                }
              }
            }
          },
          "Keywords": {
            "type": "array",
            "minItems":2,
            "items": {
              "type": "string"
            }
          }
        },
        "required": ["Author", "Chapters"]
      }
    }
  }
}

我在数组定义中添加了一些minItemsmaxItems ,以便您可以在哪里以及如何设置它们。 您可以将架构和数据复制到任何验证器,并与它们一起使用以查看它们如何工作。


另外一件重要的事情:
您无法通过架构验证来阻止或检查对象内部的重复属性。
例如,使用我们的简单JSON对象并添加重复属性,

{
  "Author": "First Author",
  "Author": "!!Duplicate Author!!",
  "TotalPages": 128,
  "Chapters": [
    {
      "Number": 1,
      "Heading": "Chapter One"
    },
    {
      "Number": 2,
      "Heading": "Chapter Two"
    }
  ]
}


将JSON转储到提到的任何验证器中,它们将全部验证为正确通过 我检查了一下,并在JSON Schema Google网上论坛上确认,目前无法通过模式定义对其进行检查。

如何重复属性的处理方式也图书馆具体。
例如,用于C#的Newtonsoft.Json库和ServiceStack库都将使用属性的最后一次出现。
因此,在我们的示例中,使用任一库反序列化之后的Book.Author属性的值将为“ !! Duplicate Author !!”。

一些来源:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM