【问题标题】:Parse Json Schema - C# - Could not resolve schema reference解析 Json 架构 - C# - 无法解析架构引用
【发布时间】:2021-09-29 09:18:24
【问题描述】:

我正在开发 C# 项目,我需要帮助来使用 Newtonsoft v. 13.0.1 解析 Json Schema

我的目标是使用关键字 $ref 在 appconfig-schema.json 文件中包含一些存在于 Common.json 文件中的 json 模式定义。

我创建了以下 json 架构(文件名 appconfig-schema.json)

    {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "title": "JSON Schema for my JSON file format",
      "additionalProperties": false,
      "type": "object",
    
      "properties": {
        "proxy": {
          "type": "object",
          "properties": {
            "url": {
              "type": "string"
            },
            "port": {
              "type": "number"
            },
            "user": {
              "type": "string"
            },
            "password": {
              "type": "string"
            }
          },
          "required": [ "url", "port", "user", "password" ]
        },
    
        "ReferenceToExternalSchema": {
          "$ref": "Common.json#/definitions/ExternalType"
        }
}

还有这个其他 json 架构 (Common.json)

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "additionalProperties": false,
  "definitions": {
    "ExternalType": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "src": {
          "type": "string"
        }
      }
    }
  }
}

当我尝试执行我的代码时,会抛出异常 bool validSchema = false;

        IList<string> messages;
            //JsonSchema js = JsonSchema.Parse(schemaJson);

        using (StreamReader r = new StreamReader("appconfig.json"))
        {
            string json = r.ReadToEnd();

            string schemaJson = File.ReadAllText("appconfig-schema1.json");
            JSchema schema = JSchema.Parse(schemaJson);

            items = JsonConvert.DeserializeObject<Configs>(json);
            JObject person = JObject.Parse(json);

            validSchema = person.IsValid(schema,out messages);
        }

异常:Newtonsoft.Json.Schema.JSchemaReaderException:'无法解析架构引用'Common.json#/definitions/ExternalType'。路径“properties.ReferenceToExternalSchema”,第 27 行,位置 34。

请注意,Visual Studio 2019 可以正确识别 Common.json 文件 enter image description here

【问题讨论】:

    标签: c# visual-studio jsonschema


    【解决方案1】:

    有趣,

    他们在官方文档中以不同的方式做到了这一点:https://www.newtonsoft.com/jsonschema/help/html/LoadingSchemas.htm

    // person.json, has a relative external schema reference 'address.json'
    // --------
    // {
    //   'type': 'object',
    //   'properties': {
    //     'name': {'type':'string'},
    //     'addresses': {
    //       'type': 'array',
    //       'items': {'$ref': 'address.json'}
    //     }
    //   }
    // }
    // 
    
    using (StreamReader file = File.OpenText(@"c:\person.json"))
    using (JsonTextReader reader = new JsonTextReader(file))
    {
        JSchemaUrlResolver resolver = new JSchemaUrlResolver();
    
        JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings
        {
            Resolver = resolver,
            // where the schema is being loaded from
            // referenced 'address.json' schema will be loaded from disk at   
            'c:\address.json'
    
            BaseUri = new Uri(@"c:\person.json")
        });
    
        // validate JSON
    }
    

    【讨论】:

    • 嗨,苏珊,非常感谢。现在它完美地工作了。
    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多