title: NullValueHandling setting for a class on JSON.NET by JsonConverter attribute (for Azure DocumentDb)
body: <p>I have several DAO files, which are stored in Azure DocumentDb, and now I want that the null values won't be stored in the DocDb, it is possible by the     <code>[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]</code> attribute for properties. But I don't wan't to put such attribute on every property.</p>
  
  <p>The problem is, there isn't any way to set the <code>JsonSerializerSettings</code> for the Json serializer used by the Azure DocumentDb API.</p>
  
  <p>The way which seems for me to go, is to use the <code>JsonConverter</code> attribute on a class, and create a custom <code>JsonConverter</code> class which will use standard serialization but with changing the serialization settings.</p>
  
  <p>That's the converter:</p>
  
  <pre><code>public class CommonJsonConverter : JsonConverter
  {
      public override bool CanConvert(Type objectType)
      {
          return true;
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          var token = JObject.ReadFrom(reader);
          return token.ToObject(objectType, serializer);
      }
  
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          serializer.NullValueHandling = NullValueHandling.Ignore;
          var jo = JObject.FromObject(value, serializer);
          jo.WriteTo(writer);
      }
  }
  </code></pre>
  
  <p>but I get on WriteJson such exception:</p>
  
  <blockquote>
    <p>A first chance exception of type
    'Newtonsoft.Json.JsonSerializationException' occurred in
    Newtonsoft.Json.dll</p>
    
    <p>Additional information: Self referencing loop detected with type
    'Infrastructure.Dao.Contacts.PersonDao'. Path ''.</p>
  </blockquote>
  
  <p>I tried to change the WriteJson function to:</p>
  
  <pre><code>    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          var jo = JObject.FromObject(value, new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore });
          jo.WriteTo(writer);
      }
  </code></pre>
  
  <p>but then I get:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.StackOverflowException'
    occurred in Newtonsoft.Json.dll</p>
  </blockquote>