I'm using Newtonsoft's Json.NET 7.0.0.0 to serialize classes to JSON from C#:
class Foo
{
public string X;
public List<string> Y = new List<string>();
}
var json =
JsonConvert.SerializeObject(
new Foo(),
Formatting.Indented,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
The value of json
here is
{ "Y": [] }
but I would like it to be { }
if Y
is an empty list.
I couldn't find a satisfactory way to achieve this. Maybe with a custom contract resolver?