title: Can I Use Json.Net de-serialization by default with ASP.net Web Api and MVC 3?
body: <p>I have a MVC 3 web application/ API that I am using and I am woefully tired of the default Microsoft Serialization. The big promise of the new ASP.net Web Api in MVC 4 is that it will use JSON.net de-serialization by default. Well I would love to use JSON deserialization by default and not mess with the WCF client. One issue I do have is I cannot convert to MVC 4 for various reasons. </p>
  
  <p>So my question is can I still use the Json.net deserialization by default with MVC 3? </p>
  
  <p>What I am trying to do specifically is implement a Json Converter to handle inheritance of objects I am de-serializing. So that if I send this Json, the API knows what type of object to return.</p>
  
  <p>I followed the article here to create a ASP.net web api in a MVC 3 application. </p>
  
  <p><a href="http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api" rel="nofollow">http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api</a></p>
  
  <p>So that if I send the JSON...</p>
  
  <pre><code>[{
      "WokerId" : "456",
      "Company" : "ACompany",
      "DOB" : "asdasd"
  },
  {
      "ContactId" : "123",
      "Name" : "Johnny Funtime",
      "DOB" : "asdasd"
  }]
  </code></pre>
  
  <p>To this Service Contract / API I will get a proper response. </p>
  
  <pre><code>[ServiceContract]
  public class ContactsApi
  {
      [WebInvoke(Method = "POST", UriTemplate = "", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
      public List&lt;Person&gt; Get(List&lt;Person&gt; input)
      {
          return input;
      }
  }
  
  public class Worker : Person
  {
      public int WorkerID { get; set; }
      public string Company { get; set; }
  }
  
  public class Contact : Person
  {
      public int ContactId { get; set; }
      public string Name { get; set; }
  }
  
  [JsonConverter(typeof(PersonConverter))]
  public abstract class Person
  {
      public string DOB { get; set; }
  }
  
  public class PersonConverter : JsonConverter
  {
      public override bool CanWrite { get { return false; } }
  
      protected Person Create(Type objectType, JObject jObject)
      {
          if (!string.IsNullOrEmpty(jObject.Value&lt;string&gt;("Company"))) {
              return new Worker();
          } else if (!string.IsNullOrEmpty(jObject.Value&lt;string&gt;("Name"))) {
              return new Contact();
          }
          return null;
      }
  
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          //base.WriteJson(writer, value, serializer);
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          if (reader.TokenType == JsonToken.Null)
              return null;
  
          var jObject = JObject.Load(reader);
  
          var target = Create(objectType, jObject);
          serializer.Populate(jObject.CreateReader(), target);
  
          return target;
      }
  
      public override bool CanConvert(Type objectType)
      {
          return typeof (Person).IsAssignableFrom(objectType);
      }
  }
  </code></pre>
  
  <p>This code currently throws an implementation error ( because person is abstract ), but I do believe that this should work if I was using Json.Net de-serialization. </p>
  
  <p>Thanks in advance for the help guys.</p>