次のように私は、スキーマを使用している場合:存在しないプロパティを強制的に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得られたオブジェクトは、オブジェクト1とオブジェクト2のいずれも、オブジェクト2のメンバーはすべてnullです。私は必要なもの
JsonSerializerSettings settings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
};
String s = Newtonsoft.Json.JsonConvert.SerializeObject(o, settings);
// {"Content":{"object1":{"field1":20.0},"object2":{}}}
は非直列化されたオブジェクトに存在する、またはnullに設定されていないのいずれかにobject2はためです。スキーマ自体、またはこのパイプラインに関連するさまざまなプロセスのいずれかでこれを行う方法はありますか?
私が必要としていたものです。 –