【发布时间】: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 上,也有 $ref 与 file names 一起使用的明显示例。我想知道 JSON.net 是否有更好的方法原生支持引用外部模式。
源代码托管在Github,如果有帮助的话。我已经在删除$ref字段的情况下进行了测试,它编译成功。
【问题讨论】:
标签: c# json json.net jsonschema