title: Ignore property when serializing under certain conditions
body: <p>Json.net has a very useful property that can be set on the <code>JsonPropertyAttribute</code> called <code>NullValueHandling</code>. If you decorate a property like this:</p>
  
  <pre><code>[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
  string MyProp { get; set; }
  </code></pre>
  
  <p>Then if <code>MyProp</code> is null, it won't be included in the output at all.</p>
  
  <p>I would like to do something similar, but with a different condition for exclusion. For example - say we have an enum</p>
  
  <pre><code>public enum MyEnum { None = 0, Value1 = 1, Value2 = 2, Value3 = 4 };
  </code></pre>
  
  <p>And a property</p>
  
  <pre><code>MyEnum MyProp { get; set; }
  </code></pre>
  
  <p>Then I'd like to have <code>MyProp</code> completely excluded from the input if <code>MyProp == MyEnum.None</code>.</p>
  
  <p>I know one solution would be to use <code>MyEnum?</code> instead of <code>MyEnum</code> and then I could use <code>NullValueHandling</code> again, but I can't use nullable here. I thought I might be able to use a <code>JsonConverter</code> so I tried to subclass <code>StringEnumConverter</code> (because I want them outputted as strings ideally):</p>
  
  <pre><code>    public class MyEnumConverter : Newtonsoft.Json.Converters.StringEnumConverter
      {            
          public override bool CanConvert(Type objectType)
          {
              return objectType == typeof(MyEnum);
          }
  
          public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
          {
              throw new NotImplementedException();
          }
  
          public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
          {
              // don't write if value is none
              var v = (MyEnum)value;
              if (v == MyEnum.None)
              {
                  // what to do here? The two options below both include the property as null
                  //writer.WriteNull();
                  //serializer.Serialize(writer, null);
                  return;        // just returning without doing anything will break the serialization
              }
              base.WriteJson(writer, value, serializer);
          }
      }
  </code></pre>
  
  <p>Can I, or should I, try subclassing <code>JsonProperty</code>? And if so, what would I need to override to get the behavior I want?</p>
  
  <p>I hoping to avoid having to attach a converter to the class that contains this property because then I have to manually serialize the whole thing, which is a pain.</p>