【发布时间】:2021-09-29 09:18:24
【问题描述】:
我正在开发 C# 项目,我需要帮助来使用 Newtonsoft v. 13.0.1 解析 Json Schema
我的目标是使用关键字 $ref 在 appconfig-schema.json 文件中包含一些存在于 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
【问题讨论】:
标签: c# visual-studio jsonschema