title: Web API 2.0 JSON Object Serialization Error
body: <p>I am running into an issue where I am storing Mongo Documents using a class hierarchy as follows:</p>
  
  <pre><code>[BsonKnownTypes(typeof(DatePriceChangeRuleEntity), typeof(QuantityPriceChangeRuleEntity))]
  //***[JsonConverter(typeof(PriceChangeRuleConverter))]***
  public partial class ProductPriceChangeRuleEntity
  </code></pre>
  
  <p>As can be seen, this ProductPriceChangeRule has 2 known types viz: DatePriceChangeRule and QuantityPriceChangeRule. These are currently being persisted on Mongo correctly with the right discriminator because I am using the BsonKnownTypes attribute (which is similar to IsClassRegistered call) in Json.net.</p>
  
  <p>These are persisted on Mongo as shown:</p>
  
  <pre><code>[{
  "_t" : "QuantityPriceChangeRuleEntity",
  "_id" : ObjectId("52a57fa612a459164cc0d9f2"),
  "Value" : "24",
  "ValueType" : 3,
  "DateCreatedUtc" : new Date("12/9/2013 00:30:30"),
  "DateModifiedUtc" : new Date("12/9/2013 00:30:30"),
  "QuantityTrigger" : 3
  }]
  </code></pre>
  
  <p>As you can see, I have a Quantity Price change rule with a discriminator correctly identified. When I query the MongoDb, I get the correct de-serialization as well.</p>
  
  <p>However, when I try to return these using the Web API, I get the following error:</p>
  
  <pre><code>$id: "1",
  message: "An error has occurred.",
  exceptionMessage: "The 'ObjectContent`1' type failed to serialize the response body for    content type 'application/json; charset=utf-8'.",
  exceptionType: "System.InvalidOperationException",
  stackTrace: null,
  innerException: {
  $id: "2",
  message: "An error has occurred.",
  exceptionMessage: "Specified cast is not valid.
  </code></pre>
  
  <p>Now, I think that the WebAPi cannot figure out the Inheritance hierarchy because there's no converter present for it. So I added the converter (as shown in code here):</p>
  
  <pre><code>public class PriceChangeRuleConverter : JsonCreationConverter&lt;ProductPriceChangeRuleEntity&gt;
  {
  
      protected override ProductPriceChangeRuleEntity Create(Type objectType, JObject jObject)
      {
          if (FieldExists("QuantityTrigger", jObject))
          {
              return new QuantityPriceChangeRuleEntity();
          }
          else if (FieldExists("EffectiveDateFrom", jObject))
          {
              return new DatePriceChangeRuleEntity();
          }
          else
          {
              return null;
          }
      }
  
      private bool FieldExists(string fieldName, JObject jObject)
      {
          return jObject[fieldName] != null;
      }
  
  }
  </code></pre>
  
  <p>However, when I do this, I now get an error on JsonCreationConverter on the WriteJson method.</p>
  
  <pre><code>public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, JsonSerializer serializer)
  {
     throw new NotImplementedException();
  }
  </code></pre>
  
  <p>Is there any way I can resolve this issue?</p>