How to omit empty collections when serializing with Json.NET
<p>I'm using Newtonsoft's Json.NET 7.0.0.0 to serialize classes to JSON from C#:</p>

<pre><code>class Foo
{
    public string X;
    public List&lt;string&gt; Y = new List&lt;string&gt;();
}

var json =
    JsonConvert.SerializeObject(
        new Foo(),
        Formatting.Indented,
        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
</code></pre>

<p>The value of <code>json</code> here is</p>

<pre><code>{ "Y": [] }
</code></pre>

<p>but I would like it to be <code>{ }</code> if <code>Y</code> is an empty list.</p>

<p>I couldn't find a satisfactory way to achieve this. Maybe with a custom contract resolver?</p>