这是当 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 http://james.newtonking.com/json/help/index.html?topic=html/RefJsonSchemaResolver.htm。我必须根据需要添加 ID,因为它看起来像$ref
喜欢通过 ID 进行匹配,尽管在 json-schema.org 上有明确的示例$ref
与一起使用文件名 http://json-schema.org/example2.html。我想知道是否有更好的方法让 JSON.net 本身支持引用外部模式。
源代码托管于Github https://github.com/sbl03/json-schema-to-poco,如果有帮助的话。我已经测试过$ref
字段被删除,并且编译成功。