如何使用JSON.net引用外部文件? [英] How to reference external files with JSON.net?

查看:105
本文介绍了如何使用JSON.net引用外部文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是当JSON.net尝试读取我的JSON模式(return JsonSchema.Read(new JsonTextReader(reader));)时出现的错误:

Here is the error I am getting when JSON.net is trying to read my JSON schema ( 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尚不知道它的存在.有某种解决办法吗?我的一个想法是浏览模式,如果有对外部文件的引用,则使用常见的

My problem is that I have this JSON schema with this reference to an external file, data-result.json, but JSON.net does not yet know it exists. Is there some sort of fix for this? One idea I have is to skim through the schema, and if there are any references to external files, to parse those with a common JsonSchemaResolver. I'd have to add IDs as appropriate, since it looks like $ref likes to match by the ID, even though over at json-schema.org, there are clear examples of $ref being used with file names. I'd like to know if there is a better way that JSON.net natively supports referencing external schemas.

源代码托管在 Github 上,如果有帮助的话.我已经测试过删除了$ref字段,并且编译成功.

The source code is hosted on Github, if it helps. I have tested with the $ref field removed, and it compiles successfully.

推荐答案

我的一个想法是浏览模式,如果有对外部文件的引用,则使用通用的JsonSchemaResolver

是的,您需要知道您的架构所依赖的架构,首先解析它们并将其添加到JsonSchemaResolver中.架构将使用其ID进行解析.

Yes, you need to know which schemas your schema depends on, parse those first and add them to a JsonSchemaResolver. The schemas will be resolved using their IDs.

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

Here's an example (using draft-03 syntax):

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

这篇关于如何使用JSON.net引用外部文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆