【问题标题】:How to reference external files with JSON.net?如何使用 JSON.net 引用外部文件?
【发布时间】:2014-07-15 21:27:21
【问题描述】:

这是我在 JSON.net 尝试读取我的 JSON 架构 (return JsonSchema.Read(new JsonTextReader(reader));) 时遇到的错误:

2014-07-15 15:33:42.7011 [Fatal] Newtonsoft.Json.JsonException: Could not resolve schema reference 'data-result.json'.
   at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema) in c:\Development\Releases\Json\Work
ing\Newtonsoft.Json\Src\Newtonsoft.Json\Schema\JsonSchemaBuilder.cs:line 139
   at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema) in c:\Development\Releases\Json\Work
ing\Newtonsoft.Json\Src\Newtonsoft.Json\Schema\JsonSchemaBuilder.cs:line 179
   at Newtonsoft.Json.Schema.JsonSchemaBuilder.Read(JsonReader reader) in c:\Development\Releases\Json\Working\Newtonsof
t.Json\Src\Newtonsoft.Json\Schema\JsonSchemaBuilder.cs:line 85
   at Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader reader, JsonSchemaResolver resolver) in c:\Development\Releases\
Json\Working\Newtonsoft.Json\Src\Newtonsoft.Json\Schema\JsonSchema.cs:line 280
   at Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader reader) in c:\Development\Releases\Json\Working\Newtonsoft.Json\
Src\Newtonsoft.Json\Schema\JsonSchema.cs:line 266
   at ThinkBinary.SchemaToPoco.Core.JsonSchemaToCodeUnit.LoadSchema(String file) in c:\Users\SLiu\Projects\json-schema-t
o-poco\Source\ThinkBinary.SchemaToPoco.Core\JsonSchemaToCodeUnit.cs:line 70
   at ThinkBinary.SchemaToPoco.Core.JsonSchemaToCodeUnit..ctor(String schemaDocument, String requestedNamespace) in c:\U
sers\SLiu\Projects\json-schema-to-poco\Source\ThinkBinary.SchemaToPoco.Core\JsonSchemaToCodeUnit.cs:line 19
   at ThinkBinary.SchemaToPoco.Console.Program.Main(String[] args) in c:\Users\SLiu\Projects\json-schema-to-poco\Source\
ThinkBinary.SchemaToPoco.Console\Program.cs:line 38

我的 JSON 架构:

{
  "$schema": "http://json-schema.org/draft-03/schema#",
  "title": "DataSet",
  "description": "A result set and description of measures and values",
  "type": "object",
  "properties": {
    "results": {
      "$ref": "data-result.json"
    },
    "dimensions": {
      "type": "array",
      "description": "An array of data dimensions included in a result set",
      "items": {
        "$ref": "data-dimension.json"
      },
      "uniqueItems": true
    },
    "measure": {
      "$ref": "data-measure.json",
      "description": "single measure represented in this data set."
    }
  },
}

我的问题是我有这个 JSON 模式,它引用了一个外部文件 data-result.json,但 JSON.net 还不知道它的存在。有什么解决办法吗?我的一个想法是浏览模式,如果有任何对外部文件的引用,则用常见的JsonSchemaResolver 解析那些。我必须酌情添加 ID,因为它看起来像 $ref 喜欢通过 ID 匹配,即使在 json-schema.org 上,也有 $reffile names 一起使用的明显示例。我想知道 JSON.net 是否有更好的方法原生支持引用外部模式。

源代码托管在Github,如果有帮助的话。我已经在删除$ref字段的情况下进行了测试,它编译成功。

【问题讨论】:

    标签: c# json json.net jsonschema


    【解决方案1】:

    Json.NET Schema 大大改进了对解析外部引用的支持。

    在此处阅读更多信息:http://www.newtonsoft.com/jsonschema/help/html/LoadingSchemas.htm

    【讨论】:

      【解决方案2】:

      我的一个想法是浏览架构,如果有任何对外部文件的引用,则使用常见的 JsonSchemaResolver 解析那些

      是的,您需要知道您的架构依赖于哪些架构,首先解析这些架构并将它们添加到JsonSchemaResolver。架构将使用其 ID 解析。

      这是一个示例(使用 Draft-03 语法):

      var baseSchema = JsonSchema.Parse(@"
      {
        ""$schema"": ""http://json-schema.org/draft-03/schema#"",
        ""id"": ""http://mycompany/base-schema#"",
        ""type"": ""object"",
      
        ""properties"": {
          ""firstName"": { ""type"": ""string"", ""required"": true}
        }
      }
      ");
      
      var resolver = new JsonSchemaResolver
          {
              LoadedSchemas = {baseSchema}
          };
      
      var derivedSchema = JsonSchema.Parse(@"
      {
        ""$schema"": ""http://json-schema.org/draft-03/schema#"",
        ""id"": ""http://mycompany/derived-schema#"",
        ""type"": ""object"",
      
        ""extends"":{ ""$ref"": ""http://mycompany/base-schema#""},
      
        ""properties"": {
          ""lastName"": { ""type"": ""string"", ""required"": true}
        }
      }
      ", resolver);
      

      小提琴:https://dotnetfiddle.net/g1nFew

      【讨论】:

        【解决方案3】:

        我认为问题可能在于您在这些 $ref 项目中有相对 URI,但没有 id 属性来建立基本 URI。您可以通过在最外层上下文中添加 id="<absolute-url>" 来确定可以从何处检索这些外部文件来解决您的问题。

        http://json-schema.org/latest/json-schema-core.html第7节

        附:在您的 measure 项目中,我注意到您同时具有 $refdescription 值。 json-reference 互联网草案说 JSON 引用对象中除 "$ref" 之外的任何成员都应被忽略。 这可能不会造成任何伤害,但如果有人预料到这一点可能会令人惊讶值以覆盖外部文件中的描述。

        【讨论】:

        • 我问这个问题已经有一段时间了,但我很确定答案是 JSON.net 不解析引用 - 你需要自己做。它将从id 属性中解析$ref,这是我提到并实施的解决方法。我在jsonschema2pojo 的代码中注意到他们自己解决了引用,所以我创建了一个similar utility for C#
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        相关资源
        最近更新 更多