解析 Json 架构 - C# - 无法解析架构引用
Parse Json Schema - C# - Could not resolve schema reference
我正在处理 C# 项目,我需要帮助来解析 Json Newtonsoft v. 13.0.1 的架构
我的目标是在 appconfig-schema.json 文件中使用关键字 $ref.
在 Common.json 文件中包含一些 json 模式定义
我创建了以下 json 架构(文件名 appconfig-schema.json)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "JSON Schema for my JSON file format",
"additionalProperties": false,
"type": "object",
"properties": {
"proxy": {
"type": "object",
"properties": {
"url": {
"type": "string"
},
"port": {
"type": "number"
},
"user": {
"type": "string"
},
"password": {
"type": "string"
}
},
"required": [ "url", "port", "user", "password" ]
},
"ReferenceToExternalSchema": {
"$ref": "Common.json#/definitions/ExternalType"
}
}
和另一个 json 架构 (Common.json)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"definitions": {
"ExternalType": {
"type": "object",
"additionalProperties": false,
"properties": {
"src": {
"type": "string"
}
}
}
}
}
当我尝试执行我的代码时,会抛出一个异常
bool validSchema = false;
IList<string> messages;
//JsonSchema js = JsonSchema.Parse(schemaJson);
using (StreamReader r = new StreamReader("appconfig.json"))
{
string json = r.ReadToEnd();
string schemaJson = File.ReadAllText("appconfig-schema1.json");
JSchema schema = JSchema.Parse(schemaJson);
items = JsonConvert.DeserializeObject<Configs>(json);
JObject person = JObject.Parse(json);
validSchema = person.IsValid(schema,out messages);
}
异常:Newtonsoft.Json.Schema.JSchemaReaderException:'无法解析架构引用 'Common.json#/definitions/ExternalType'。路径 'properties.ReferenceToExternalSchema',第 27 行,位置 34。'
请注意Visual studio 2019正确识别Common.json文件
enter image description here
有趣,
他们在官方文档中以不同的方式做到了:https://www.newtonsoft.com/jsonschema/help/html/LoadingSchemas.htm
// person.json, has a relative external schema reference 'address.json'
// --------
// {
// 'type': 'object',
// 'properties': {
// 'name': {'type':'string'},
// 'addresses': {
// 'type': 'array',
// 'items': {'$ref': 'address.json'}
// }
// }
// }
//
using (StreamReader file = File.OpenText(@"c:\person.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
JSchemaUrlResolver resolver = new JSchemaUrlResolver();
JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings
{
Resolver = resolver,
// where the schema is being loaded from
// referenced 'address.json' schema will be loaded from disk at
'c:\address.json'
BaseUri = new Uri(@"c:\person.json")
});
// validate JSON
}
我正在处理 C# 项目,我需要帮助来解析 Json Newtonsoft v. 13.0.1 的架构
我的目标是在 appconfig-schema.json 文件中使用关键字 $ref.
在 Common.json 文件中包含一些 json 模式定义我创建了以下 json 架构(文件名 appconfig-schema.json)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "JSON Schema for my JSON file format",
"additionalProperties": false,
"type": "object",
"properties": {
"proxy": {
"type": "object",
"properties": {
"url": {
"type": "string"
},
"port": {
"type": "number"
},
"user": {
"type": "string"
},
"password": {
"type": "string"
}
},
"required": [ "url", "port", "user", "password" ]
},
"ReferenceToExternalSchema": {
"$ref": "Common.json#/definitions/ExternalType"
}
}
和另一个 json 架构 (Common.json)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"definitions": {
"ExternalType": {
"type": "object",
"additionalProperties": false,
"properties": {
"src": {
"type": "string"
}
}
}
}
}
当我尝试执行我的代码时,会抛出一个异常 bool validSchema = false;
IList<string> messages;
//JsonSchema js = JsonSchema.Parse(schemaJson);
using (StreamReader r = new StreamReader("appconfig.json"))
{
string json = r.ReadToEnd();
string schemaJson = File.ReadAllText("appconfig-schema1.json");
JSchema schema = JSchema.Parse(schemaJson);
items = JsonConvert.DeserializeObject<Configs>(json);
JObject person = JObject.Parse(json);
validSchema = person.IsValid(schema,out messages);
}
异常:Newtonsoft.Json.Schema.JSchemaReaderException:'无法解析架构引用 'Common.json#/definitions/ExternalType'。路径 'properties.ReferenceToExternalSchema',第 27 行,位置 34。'
请注意Visual studio 2019正确识别Common.json文件 enter image description here
有趣,
他们在官方文档中以不同的方式做到了:https://www.newtonsoft.com/jsonschema/help/html/LoadingSchemas.htm
// person.json, has a relative external schema reference 'address.json'
// --------
// {
// 'type': 'object',
// 'properties': {
// 'name': {'type':'string'},
// 'addresses': {
// 'type': 'array',
// 'items': {'$ref': 'address.json'}
// }
// }
// }
//
using (StreamReader file = File.OpenText(@"c:\person.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
JSchemaUrlResolver resolver = new JSchemaUrlResolver();
JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings
{
Resolver = resolver,
// where the schema is being loaded from
// referenced 'address.json' schema will be loaded from disk at
'c:\address.json'
BaseUri = new Uri(@"c:\person.json")
});
// validate JSON
}