I need to use custom JSON deserialization to replace null values from my JSON with default text. The JSON object is DE-serialized into below POCO model. To replace the null values, I need help to find best way to avoid multiple iterations as POCO model is list and trying to avoid multiple 'if' statements to check each property for null and replace value.
I tried to override 'ReadJson' method and tried to follow the below example: How to check all properties of an object whether null or empty?
It might not be best approach to check with each property name jsonObject.Name.Equals("Property1")
public class Class1List
{
public List<Class2> Property1 { get; set; }
}
public class Class2
{
public class3 Property2{ get; set; }
public string Property3 { get; set; }
public string Property4 { get; set; }
public class4 Property5 { get; set; }
public bool Property6 { get; set; }
}
public class Class3
{
public string Property7 { get; set; }
}
public class Class4
{
public string Property8 { get; set; }
}
JSON sample:
{
"Property1":[
{
"Property2":{
"Property7":"test1"
},
"Property3":"test34",
"Property4":"test56",
"Property5":{
"Property8":"test78"
},
"Property6":false
},
{
"Property2":{
"Property7":"test6"
},
"Property3":"test2",
"Property4":"test3",
"Property5":{
"Property8":"test5"
},
"Property6":true
}
]
}
JSON example with null values:
I left couple of parameters empty or null as I want to handle those scenarios in customer deserializer.
{
"Property1":[
{
"Property2":{
"Property7":"test1"
},
"Property3":"",
"Property4":"test56",
"Property5":{
"Property8":""
},
"Property6":false
},
{
"Property2":{
"Property7":"test6"
},
"Property3":"test2",
"Property4":"test3",
"Property5":{
"Property8":"test5"
},
"Property6":true
}
]
}