Sub*_*raj 5 c# json json.net jsonschema
我有一个带有 ref 的 JSON 架构。我正在尝试使用 来解决所有参考问题JsonSchemaResolver。但是,不幸的是, ref 未解决并出现如下错误。
我试图通过解析所有引用来获取替换的 JSON。
代码:
var schemaFileContents = File.ReadAllText(schemaFileName);
JsonSchemaResolver resolver = new JsonSchemaResolver();
var result = JsonSchema.Parse(schemaFileContents, resolver);
Console.WriteLine(result);
Run Code Online (Sandbox Code Playgroud)
JSON 架构:
{
"$schema": "YYYYYYY",
"id": "XXXXXX",
"title": "Current Employee Details",
"description": "XXXXXXXXXXXXX",
"type": "object",
"properties": {
"EMP": {
"title": "Employee ",
"description": "Details of the Employee",
"$ref": "#/definitions/Employee"
}},
"definitions": {
"EmployeeAddress": {
"title": "Address",
"description": "EmployeeAddress - Present Address",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"EmployeeAddress"
]
},
"address": {
"title": "Address",
"type": "string"
},
"postalCode": {
"title": "Postal Code",
"type": "string"
}
},
"required": [
"postalCode",
"address"
]
},
"Employee": {
"title": "Party",
"description": "Employee Details",
"type": "object",
"properties": {
"firstName": {
"title": "First name",
"type": "string"
},
"address": {
"title": "Employee Address",
"$ref": "#/definitions/EmployeeAddress"
}
},
"required": [
"firstName"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
Unhandled Exception: System.ArgumentException: Can not convert Array to Boolean.
at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.ProcessSchemaProperties(JObject schemaObject)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.BuildSchema(JToken token)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema)
at Newtonsoft.Json.Schema.JsonSchemaBuilder.Read(JsonReader reader)
at Newtonsoft.Json.Schema.JsonSchema.Read(JsonReader reader, JsonSchemaResolver resolver)
at Newtonsoft.Json.Schema.JsonSchema.Parse(String json, JsonSchemaResolver resolver)
Run Code Online (Sandbox Code Playgroud)
看起来您正在使用json schema V4但JsonSchemaResolver期望json schema V3. 他们之间的区别在于required领域。尝试在属性级别使用boolvalue 而不是更高级别的数组值:
"address": {
"title": "Address",
"type": "string",
"required": true
}
Run Code Online (Sandbox Code Playgroud)
根据文档 JsonSchemaResolver已过时。要使用符合最新标准的 json 架构,您需要使用单独的包。使用,请参阅此处的JSchemaPreloadedResolver示例
| 归档时间: |
|
| 查看次数: |
4161 次 |
| 最近记录: |