如何使用JSON.net引用外部文件?

这是我在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 result.json,但JSON.net还不知道它存在。 对此有什么解决方法吗? 我的一个想法是浏览模式,如果有任何对外部文件的引用,则使用公共JsonSchemaResolver解析那些。 我必须在适当的时候添加ID,因为看起来$ref喜欢通过ID匹配,即使在json-schema.org上,也有明确的$ref与文件名一起使用的例子。 我想知道JSON.net是否有更好的方式支持引用外部模式。

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

Json.NET Schema对解析外部引用有很多改进的支持。

在这里阅读更多内容: http : //www.newtonsoft.com/jsonschema/help/html/LoadingSchemas.htm

我有一个想法是浏览模式,如果有任何外部文件的引用,解析那些具有共同的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

我认为问题可能是你在那些$ref项中有相对URI,但没有id属性来建立基URI。 您可以通过在最外层上下文中添加id=""来解决您的问题,从而确定可以从中检索这些外部文件的位置。

请参阅http://json-schema.org/latest/json-schema-core.html的第7节

PS在你的measure项目中,我注意到你有$refdescription值。 json-reference互联网草案说明JSON参考对象中除“$ ref”以外的任何成员都应该被忽略。 它可能没有任何伤害,但如果有人希望该值覆盖外部文件中的描述,则可能会令人惊讶。