【问题标题】:Unable to parse JSON Schema and resolve the reference无法解析 JSON Schema 并解析引用
【发布时间】:2018-07-04 07:18:51
【问题描述】:

我有一个带有 ref 的 JSON 模式。我正在尝试使用JsonSchemaResolver 解析所有引用。但是,不幸的是, ref 没有解决并得到如下错误。

我正在尝试通过解析所有参考来获取替换的 JSON。

代码:

        var schemaFileContents = File.ReadAllText(schemaFileName);
        JsonSchemaResolver resolver = new JsonSchemaResolver();
        var result = JsonSchema.Parse(schemaFileContents, resolver);
        Console.WriteLine(result);

JSON 架构:

{
  "$schema": "YYYYYYY",
  "id": "XXXXXX",
  "title": "Current Employee Details",
  "description": "XXXXXXXXXXXXX",
  "type": "object",
  "properties": {
    "EMP": {
      "title": "Employee ",
      "description": "Details of the Employee",
      "$ref": "#/definitions/Employee"
    }},
    "definitions": {
      "EmployeeAddress": {
        "title": "Address",
        "description": "EmployeeAddress - Present Address",
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "EmployeeAddress"
            ]
          },
          "address": {
            "title": "Address",
            "type": "string"
          },
          "postalCode": {
            "title": "Postal Code",
            "type": "string"
          }
        },
        "required": [
          "postalCode",
          "address"
        ]
      },

      "Employee": {
        "title": "Party",
        "description": "Employee Details",
        "type": "object",
        "properties": {
          "firstName": {
            "title": "First name",
            "type": "string"
          },
          "address": {
            "title": "Employee Address",
            "$ref": "#/definitions/EmployeeAddress"
          }
        },
        "required": [
          "firstName"
        ]
      }
    }
  }

错误:

Unhandled Exception: System.ArgumentException: Can not convert Array to Boolean.
   at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
   at Newtonsoft.Json.Schema.JsonSchemaBuilder.ProcessSchemaProperties(JObject schemaObject)
   at Newtonsoft.Json.Schema.JsonSchemaBuilder.BuildSchema(JToken token)
   at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema)
   at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema)
   at Newtonsoft.Json.Schema.JsonSchemaBuilder.Read(JsonReader reader)
   at Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader reader, JsonSchemaResolver resolver)
   at Newtonsoft.Json.Schema.JsonSchema.Parse(String json, JsonSchemaResolver resolver)

【问题讨论】:

标签: c# json json.net jsonschema


【解决方案1】:

看起来您正在使用json schema V4,但JsonSchemaResolver 需要json schema V3。它们之间的区别在于required 字段。尝试在属性级别使用 bool 值而不是更高级别的数组值:

"address": {
    "title": "Address",
    "type": "string",
    "required": true
}

根据docs JsonSchemaResolver 已过时。要将 json 模式与最新标准一起使用,您需要使用 separate package。使用JSchemaPreloadedResolver,见例子here

【讨论】:

  • 感谢阿尔伯特的建议。但是,我应该如何解析 json 架构 V4?我是否需要使用任何其他库?
  • 我已经尝试过上面提到的示例示例。但是,只有 "type": "object", "properties": { "EMP": { "$ref": "XXXXXX#/definitions/ Employee" } 单独移动到最后一部分,定义部分在描述之后移动。我仍然可以看到“$ref”值没有被正确替换。我需要在同一个 JSON 中解析 $ref。
  • “$ref 值未正确替换”和“解决 $ref”是什么意思?您是否尝试过使用您的方案测试真实employee 对象的有效性?它工作正常吗?
【解决方案2】:

JsonSchemaResolver 现在已弃用。在下面尝试并使用 Newtonsoft.Json.Schema NuGet

var schemaFileContents = File.ReadAllText(schemaFileName);
JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
var result = JSchema.Parse(schemaFileContents, resolver);
Console.WriteLine(result);       

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多