C# 在使用anyOf属性下的多个引用解析json模式时,输出中只返回一个引用(最后一个)
我有一个有效的json模式,如下所示C# 在使用anyOf属性下的多个引用解析json模式时,输出中只返回一个引用(最后一个),c#,json,json.net,jsonschema,C#,Json,Json.net,Jsonschema,我有一个有效的json模式,如下所示 { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "abcd", "title": "test schema", "description": "............", "type": "object", "properties": { "a": { .......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "abcd",
"title": "test schema",
"description": "............",
"type": "object",
"properties": {
"a": {
...........
...........
},
"b": {
.........
........
.........
},
"c": {
...........
..........
},
"d": {
...........
..........
}
},
"anyOf": [
{
"type": "object",
"$ref": "#/properties/a",
"$ref": "#/properties/b"
},
{
"type": "object",
"$ref": "#/properties/c",
"$ref": "#/properties/d"
}
]
}
My Json Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "abcd",
"title": "test schema",
"description": "............",
"type": "object",
"properties": {
"a": {
...........
...........
},
"b": {
.........
........
.........
},
"c": {
...........
..........
},
"d": {
...........
..........
}
},
"anyOf": [
{
"$ref": "#/properties/b"
},
{
"$ref": "#/properties/d"
}
]
}
I'm wondering why I'm getting only the last reference under the anyOf property
On parsing shouldn't the output be the same as that in the file?
Am I missing something?
My desired output under anyOf is
"anyOf": [
{
"type": "object",
"$ref": "#/properties/a",
"$ref": "#/properties/b"
},
{
"type": "object",
"$ref": "#/properties/c",
"$ref": "#/properties/d"
}
]
上面的模式存储在一个文件中,我正在加载它进行解析,如下所示
JSchema schema =
JSchema.Parse(File.ReadAllText(@"D:\Backups\testschema.json"));
因此,当我查看schema的输出时,它如下所示
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "abcd",
"title": "test schema",
"description": "............",
"type": "object",
"properties": {
"a": {
...........
...........
},
"b": {
.........
........
.........
},
"c": {
...........
..........
},
"d": {
...........
..........
}
},
"anyOf": [
{
"type": "object",
"$ref": "#/properties/a",
"$ref": "#/properties/b"
},
{
"type": "object",
"$ref": "#/properties/c",
"$ref": "#/properties/d"
}
]
}
My Json Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "abcd",
"title": "test schema",
"description": "............",
"type": "object",
"properties": {
"a": {
...........
...........
},
"b": {
.........
........
.........
},
"c": {
...........
..........
},
"d": {
...........
..........
}
},
"anyOf": [
{
"$ref": "#/properties/b"
},
{
"$ref": "#/properties/d"
}
]
}
I'm wondering why I'm getting only the last reference under the anyOf property
On parsing shouldn't the output be the same as that in the file?
Am I missing something?
My desired output under anyOf is
"anyOf": [
{
"type": "object",
"$ref": "#/properties/a",
"$ref": "#/properties/b"
},
{
"type": "object",
"$ref": "#/properties/c",
"$ref": "#/properties/d"
}
]
关于如何实现所需输出的想法?在Json中,每个对象只能有一个特定的键一次。因此,在一个对象中,只能有一个名为
$ref
的键。您在上面发布的Json无效;它的功能取决于实现——理想情况下,它应该抛出一个错误,但在这种情况下,第二个错误似乎覆盖了第一个错误
请注意,对于$ref
,其他属性将被忽略,因此除了$ref
之外,使用另一个关键字,如type
,没有什么意义
我不完全确定,但看起来你想要实现的是,要么属性“a”和“b”应该存在,要么属性“c”和“d”应该存在
您可以通过将anyOf
子句替换为:
"anyOf": [
{
"required": ["a", "b"]
},
{
"required": ["c", "d"]
}
]
完美的谢谢@Erwin Bolwidt,这正是我想要的