If I have a schema as follows:
{
"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"
}
}
}
And I'm using the following code to generate a set of C# classes:
NJsonSchema.JsonSchema4 schema = await NJsonSchema.JsonSchema4.FromFileAsync(<path to schema>);
var generator = new CSharpGenerator(schema);
var file = generator.GenerateFile();
And, using the resultant classes, perform this operation:
My_application_schema o = JsonConvert.DeserializeObject<My_application_schema>(@"
{
'Content': {
'object1': {
'field1': 20
}
}
}");
Then the deserialization completes without errors, but the resulting object o includes references to both object1 and object2, although all of object2's members are null.
JsonSerializerSettings settings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
};
String s = Newtonsoft.Json.JsonConvert.SerializeObject(o, settings);
// {"Content":{"object1":{"field1":20.0},"object2":{}}}
What I need is for object2 to either not exist in the deserialized object, or set to null. Is there any way to do this, either in the schema itself or in one of the various processes involved in this pipeline?