I am trying to serialize an object using Newtonsoft Json.Net.
This object is an anonymous type filled with many heterogeneous things, mostly regular POCOs, but also with JObject or JArray s.
The fact is that when adding the NullValueHandling property to NullValueHandling.Ignore each null property is ignored, but only if it is part of a "regular" .Net object. Each null property inside a JObject or JArray remains.
Here's a minimal example:
var jobj = JObject.FromObject(new Anything{ x = 1, y = "bla", z = null }); var poco = new Foo { foo1 = "bar", foo2 = null }; var serialized = JsonConvert.SerializeObject(new { source1 = poco, source2 = jobj }, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});
Is there an easy way to ignore these null values? Am I missing some setting? Or do I need to deal with it manually?
xlecoustillier
source share