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"
}
}
JsonConverter
for classesA
andB
but it's probably much easier to filter/convert the data before serializing it. – Mo B.