如何强制不存在的属性为空?
How do I force non-existent properties to null?
如果我有如下架构:
{
"id": "http://example.com/my_application",
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "my_application_schema",
"additionalProperties": false,
"definitions": {
"NumberField": {
"type": "number",
"min": 0,
"max": 2147483647
},
"StringField": {
"type": "string",
"minLength": 1,
"maxLength": 12
},
"MainObject": {
"type": "object",
"additionalProperties": false,
"properties": {
"object1": {
"$ref": "#/definitions/Object1"
},
"object2": {
"$ref": "#/definitions/Object2"
}
},
"minProperties": 1,
"maxProperties": 1
},
"Object1": {
"type": "object",
"additionalProperties": false,
"properties": {
"field1": {
"$ref": "#/definitions/NumberField",
"proto_field": 1
}
},
"required": [
"field1"
]
},
"Object2": {
"type": "object",
"additionalProperties": false,
"properties": {
"field1": {
"$ref": "#/definitions/StringField",
"proto_field": 1
}
},
"required": [
"field1"
]
}
},
"type": "object",
"properties": {
"Content": {
"$ref": "#/definitions/MainObject"
}
}
}
我正在使用以下代码生成一组 C# 类:
NJsonSchema.JsonSchema4 schema = await NJsonSchema.JsonSchema4.FromFileAsync(<path to schema>);
var generator = new CSharpGenerator(schema);
var file = generator.GenerateFile();
然后,使用结果 类,执行此操作:
My_application_schema o = JsonConvert.DeserializeObject<My_application_schema>(@"
{
'Content': {
'object1': {
'field1': 20
}
}
}");
然后反序列化完成且没有错误,但是生成的对象 o 包括对 object1 和 object2[ 的引用=29=],尽管 object2 的所有成员都是空的。
JsonSerializerSettings settings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
};
String s = Newtonsoft.Json.JsonConvert.SerializeObject(o, settings);
// {"Content":{"object1":{"field1":20.0},"object2":{}}}
我需要的是 object2 要么不存在于反序列化对象中,要么设置为 null。有没有办法做到这一点,无论是在模式本身还是在这个管道中涉及的各种进程之一?
因为我不懂 C#,所以回答比较乱。
GSON (Java) 将缺失的属性视为空值,NJsonSchema 将缺失的属性视为其类型的默认值。
看起来不需要的初始值是由模板在如下一行生成的:https://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid#L16
这是以 HasDefaultValue
为真为条件的。这是在 https://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs#L40
中设置的
https://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs#L50 提及 _settings.GenerateDefaultValues
。也许你可以设置 _settings.GenerateDefaultValues
false?
从 https://groups.google.com/forum/#!topic/jsonschema/mD6GDca4zN8 看来 JSON 模式可以有空值。也许这会以向模式添加文本为代价解决它?
这是否意味着您的 object2 为空或您正在寻找的类型,因此所需的架构更改类似于:
"properties": {
"object1": {
"oneOf": [
{
"$ref": "#/definitions/Object1"
},
{
"type": "null"
}
]
},
"object2": {
"oneOf": [
{
"$ref": "#/definitions/Object2"
},
{
"type": "null"
}
]
}
}
可以在显示公司对象的 github page 上找到类似的示例
如果我有如下架构:
{
"id": "http://example.com/my_application",
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "my_application_schema",
"additionalProperties": false,
"definitions": {
"NumberField": {
"type": "number",
"min": 0,
"max": 2147483647
},
"StringField": {
"type": "string",
"minLength": 1,
"maxLength": 12
},
"MainObject": {
"type": "object",
"additionalProperties": false,
"properties": {
"object1": {
"$ref": "#/definitions/Object1"
},
"object2": {
"$ref": "#/definitions/Object2"
}
},
"minProperties": 1,
"maxProperties": 1
},
"Object1": {
"type": "object",
"additionalProperties": false,
"properties": {
"field1": {
"$ref": "#/definitions/NumberField",
"proto_field": 1
}
},
"required": [
"field1"
]
},
"Object2": {
"type": "object",
"additionalProperties": false,
"properties": {
"field1": {
"$ref": "#/definitions/StringField",
"proto_field": 1
}
},
"required": [
"field1"
]
}
},
"type": "object",
"properties": {
"Content": {
"$ref": "#/definitions/MainObject"
}
}
}
我正在使用以下代码生成一组 C# 类:
NJsonSchema.JsonSchema4 schema = await NJsonSchema.JsonSchema4.FromFileAsync(<path to schema>);
var generator = new CSharpGenerator(schema);
var file = generator.GenerateFile();
然后,使用结果 类,执行此操作:
My_application_schema o = JsonConvert.DeserializeObject<My_application_schema>(@"
{
'Content': {
'object1': {
'field1': 20
}
}
}");
然后反序列化完成且没有错误,但是生成的对象 o 包括对 object1 和 object2[ 的引用=29=],尽管 object2 的所有成员都是空的。
JsonSerializerSettings settings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
};
String s = Newtonsoft.Json.JsonConvert.SerializeObject(o, settings);
// {"Content":{"object1":{"field1":20.0},"object2":{}}}
我需要的是 object2 要么不存在于反序列化对象中,要么设置为 null。有没有办法做到这一点,无论是在模式本身还是在这个管道中涉及的各种进程之一?
因为我不懂 C#,所以回答比较乱。
GSON (Java) 将缺失的属性视为空值,NJsonSchema 将缺失的属性视为其类型的默认值。
看起来不需要的初始值是由模板在如下一行生成的:https://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid#L16
这是以 HasDefaultValue
为真为条件的。这是在 https://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs#L40
https://github.com/RSuter/NJsonSchema/blob/1a50dfa4d5d562ae89e7aac49c1573ad4e32313a/src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs#L50 提及 _settings.GenerateDefaultValues
。也许你可以设置 _settings.GenerateDefaultValues
false?
从 https://groups.google.com/forum/#!topic/jsonschema/mD6GDca4zN8 看来 JSON 模式可以有空值。也许这会以向模式添加文本为代价解决它?
这是否意味着您的 object2 为空或您正在寻找的类型,因此所需的架构更改类似于:
"properties": {
"object1": {
"oneOf": [
{
"$ref": "#/definitions/Object1"
},
{
"type": "null"
}
]
},
"object2": {
"oneOf": [
{
"$ref": "#/definitions/Object2"
},
{
"type": "null"
}
]
}
}
可以在显示公司对象的 github page 上找到类似的示例