0

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
      }
   ]
}
VKM
  • 39
  • 5
  • 1
    Could you please share with us a sample json? Could you please also indicate which json library are you using? – Peter Csala Mar 30 '22 at 08:16
  • Hi Peter, Thank you for your response. Please find JSON added in question as I am not able to add in comments. I am using newtonsoft Json. – VKM Mar 30 '22 at 08:34
  • Are you aware of the fact that the second json (with null values) is not valid? You can use online tools like [this](https://jsonformatter.curiousconcept.com/#) to perform preliminary validation. – Peter Csala Mar 30 '22 at 08:47
  • I am aware of that but received similar JSON object once and Apologies, I attached wrong example of null JSON as reference. I want to replace if there is any null or empty value present in any properties like Property3, Property4 etc – VKM Mar 30 '22 at 10:52
  • How about this? Annotate those fields that can have fallback value with two attributes (`DefaultValue` and `JsonProperty`) like this: `[DefaultValue(5), JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]` – Peter Csala Mar 30 '22 at 11:43
  • The above property will work if the key is missing altogether in JSON response. But If i receive JSON with key:"" and value as Null or "" it is not replacing default value. I tried to set the default value through constructor but nothing worked – VKM Mar 30 '22 at 14:22
  • Well in that case you need to implement a custom converter. – Peter Csala Mar 31 '22 at 08:56
  • check examples: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?pivots=dotnet-6-0 – Power Mouse Apr 25 '22 at 19:23

0 Answers0