title: How to ignore a property in class if null, using json.net
body: <p>I am using <a href="http://www.newtonsoft.com/json" rel="noreferrer">Json.NET</a> to serialize a class to JSON.</p>
  
  <p>I have the class like this:</p>
  
  <pre><code>class Test1
  {
      [JsonProperty("id")]
      public string ID { get; set; }
      [JsonProperty("label")]
      public string Label { get; set; }
      [JsonProperty("url")]
      public string URL { get; set; }
      [JsonProperty("item")]
      public List&lt;Test2&gt; Test2List { get; set; }
  }
  </code></pre>
  
  <p>I want to add a <code>JsonIgnore()</code> attribute to <code>Test2List</code> property only when <code>Test2List</code> is <code>null</code>. If it is not null then I want to include it in my json.</p>