【问题标题】:JSON Schema - Can not convert Array to BooleanJSON Schema - 无法将数组转换为布尔值
【发布时间】:2020-09-29 09:10:20
【问题描述】:

我正在测试 NewtonSoft 的 JsonSchema 包,代码如下

string schemaJson = File.ReadAllText("c:\\temp\\schema.txt");
JsonSchema schema = JsonSchema.Parse(schemaJson);

当我在 https://www.jsonschemavalidator.net/ 上测试架构时,它可以正确执行,但是当我在本地运行上述代码时,我得到一个 ArgumentException“无法将数组转换为布尔值”。

这是架构:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "http://example.com/root.json",
  "type": "object",
  "title": "The Root Schema",
  "description": "The root schema is the schema that comprises the entire JSON document.",
  "default": {},
  "required": [
    "checked",
    "dimensions",
    "id",
    "name",
    "price",
    "tags"
  ],
  "properties": {
    "checked": {
      "$id": "#/properties/checked",
      "type": "boolean",
      "title": "The Checked Schema",
      "description": "An explanation about the purpose of this instance.",
      "default": false,
      "examples": [
        false
      ]
    },
    "dimensions": {
      "$id": "#/properties/dimensions",
      "type": "object",
      "title": "The Dimensions Schema",
      "description": "An explanation about the purpose of this instance.",
      "default": {},
      "examples": [
        {
          "height": 10.0,
          "width": 5.0
        }
      ],
      "required": [
        "width",
        "height"
      ],
      "properties": {
        "width": {
          "$id": "#/properties/dimensions/properties/width",
          "type": "integer",
          "title": "The Width Schema",
          "description": "An explanation about the purpose of this instance.",
          "default": 0,
          "examples": [
            5
          ]
        },
        "height": {
          "$id": "#/properties/dimensions/properties/height",
          "type": "integer",
          "title": "The Height Schema",
          "description": "An explanation about the purpose of this instance.",
          "default": 0,
          "examples": [
            10
          ]
        }
      }
    },
    "id": {
      "$id": "#/properties/id",
      "type": "integer",
      "title": "The Id Schema",
      "description": "An explanation about the purpose of this instance.",
      "default": 0,
      "examples": [
        1
      ]
    },
    "name": {
      "$id": "#/properties/name",
      "type": "string",
      "title": "The Name Schema",
      "description": "An explanation about the purpose of this instance.",
      "default": "",
      "examples": [
        "A green door"
      ]
    },
    "price": {
      "$id": "#/properties/price",
      "type": "number",
      "title": "The Price Schema",
      "description": "An explanation about the purpose of this instance.",
      "default": 0,
      "examples": [
        12.5
      ]
    },
    "tags": {
      "$id": "#/properties/tags",
      "minItems": 3,
      "type": "array",
      "title": "The Tags Schema",
      "description": "An explanation about the purpose of this instance.",
      "default": [],
      "examples": [
        [
          "home",
          "green"
        ]
      ],
      "items": {
        "$id": "#/properties/tags/items",
        "type": "string",
        "title": "The Items Schema",
        "description": "An explanation about the purpose of this instance.",
        "default": "",
        "examples": [
          "home",
          "green"
        ]
      }
    }
  }
}

这是我在网站上用来测试架构验证的(无效)JSON:

{
    "checked": "false",
    "dimensions": {
        "width": 5,
        "height": 10
    },
    "id": 1,
    "name": "A green door",
    "price": 12.5,
    "tags": [
        "home",
        "green"
    ]
}

【问题讨论】:

  • 这是导致错误所需的所有代码吗? JSON 本身是有效的,因此无法复制错误,任何人都不太可能提供帮助。
  • 这是一个 100% 可执行的测试用例。
  • 我不使用 .net,所以让我请社区中的其他人来看看 =]
  • 这些是我希望验证器报告的问题。加载模式是失败的。需要明确的是,这是 Newtonsoft 库
  • @dbc 我能够重现问题here。看来他正在尝试使用 Newtonsoft.Json 包中过时的 JsonSchema 组件,而不是使用 Newtonsoft.Json.Schema 中的 JSchema

标签: c# json.net jsonschema


【解决方案1】:

我能够重现您的问题here

您似乎正在使用Newtonsoft.Json 包中的JsonSchema 组件。如documentation 中所述,此组件已过时,已被新的JSchema 组件取代,该组件位于单独的Newtonsoft.Json.Schema 包中。

如果您使用较新的组件,您的架构 loads as expected

【讨论】:

  • 谢谢你,布赖恩!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 2023-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多