1
votes

Let there be a class C like

    class A
    {
        public string p;
        public string q;
    }

    class B
    {
        public string c;
        public string d;
    }

    class C
    {
        public A a;
        public B b;
    }

and I'm serializing it with the code

string json = JsonConvert.SerializeObject(JsonObject,
                            new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented });

so this code removes all the null values. How do I get null values if any of the value is filled in one nested object. For example, if

A.p = null,  
A.q = "filled",
B.c = "filled",
B.d = "filled"

so the json string should be

{
  "A": {
    "p": null,
    "q": "filled"
  },
  "B": {
    "c": "filled",
    "d": "filled" 
  } 
}

But if object is like,

A.p = null,  
A.q = null,
B.c = "filled",
B.d = "filled"

Json string should be like,

{
  "B": {
    "c": "filled",
    "d": "filled" 
  } 
}
1
I could be wrong, but I think your best bet is just to add some logic before you Serialize. if all properties of an object are null, then don't serialize it. To my knowledge, Newtonsoft does not have a way to exclude an entire object if all the properties are null.Casey Crookston
Your example does not match your description. Should any of the value read none of the properties?Mo B.
You can do this sort of thing with a custom JsonConverter for classes A and B but it's probably much easier to filter/convert the data before serializing it.Mo B.
Sorry if my question wasn't clear enough. I have found a solution for this problem.Harsh Raj

1 Answers

0
votes

I don't whether it is best solution available but this is I solved this problem.

//Serialize the object to json, if false then removes the null properties else ignore them
private static string Serialize(C JsonObject, bool ignoreNullProperties)
{
    return JsonConvert.SerializeObject(JsonObject,
             new JsonSerializerSettings { NullValueHandling = ignoreNullProperties == true ? NullValueHandling.Ignore : NullValueHandling.Include, Formatting = Formatting.Indented });
}

        
public static string SerializePerservingNullValuesForNonEmptyClass(C JsonObject)
{
    string notHavingNullValues = Serialize(C, true);// serialized to get json with no null properties
    string havingNullValues = Serialize(C, false);// serialized to json with null properties
    //converted both Json string to dictionary<string,object>
    Dictionary<string, object> notHavingNullValuesDictionary = 
       JsonConvert.DeserializeObject<Dictionary<string, object>>(notHavingNullValues);
    Dictionary<string, object> havingNullValuesDictionary = 
       JsonConvert.DeserializeObject<Dictionary<string, object>>(havingNullValues);

    Dictionary<string, object> resultDictionary = 
    GetNullValuesForObjectHavingAnyNonNullValues(havingNullValuesDictionary, 
    notHavingNullValuesDictionary);

    return Serialize(resultDictionary, false);
}

//iterated through dictionary having no null value and taking values from dictionary having all values and stored them to a new dictionary
private static Dictionary<string, object> GetNullValuesForObjectHavingAnyNonNullValues(Dictionary<string, object> havingNull, Dictionary<string, object> notHavingNull)
{
   Dictionary<string, object> expectedJsonFormat = new Dictionary<string, object>();
   foreach (KeyValuePair<string, object> values in notHavingNull)
   {
      expectedJsonFormat.Add(values.Key, havingNull[values.Key]);
   }
   return expectedJsonFormat;
}

Hope this helps to everyone.