1

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.

So my question is can I still use the Json.net deserialization by default with MVC 3?

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.

I followed the article here to create a ASP.net web api in a MVC 3 application.

http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api

So that if I send the JSON...

[{
    "WokerId" : "456",
    "Company" : "ACompany",
    "DOB" : "asdasd"
},
{
    "ContactId" : "123",
    "Name" : "Johnny Funtime",
    "DOB" : "asdasd"
}]

To this Service Contract / API I will get a proper response.

[ServiceContract]
public class ContactsApi
{
    [WebInvoke(Method = "POST", UriTemplate = "", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public List<Person> Get(List<Person> 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<string>("Company"))) {
            return new Worker();
        } else if (!string.IsNullOrEmpty(jObject.Value<string>("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);
    }
}

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.

Thanks in advance for the help guys.

tereško
  • 58,060
  • 25
  • 98
  • 150
recneps
  • 1,285
  • 5
  • 16
  • 27
  • 2
    possible duplicate of [Using JSON.net as default JSON serializer in ASP.NET MVC 3 - Is it possible?](http://stackoverflow.com/questions/7109967/using-json-net-as-default-json-serializer-in-asp-net-mvc-3-is-it-possible) – Cybermaxs Jun 26 '13 at 07:26
  • 1
    @Cybermaxs-Betclic thats serialization, not deserialization – wal Jun 30 '13 at 12:18
  • Somehow google dumped me into this ancient question; for other weary travelers (and myself if it happens again): 1.) The linked dupe has terrible answers; there are definitely better ones to link to. (i.e. http://stackoverflow.com/a/26068063/398630 ) 2.) Deserialization is handled automatically in the latest MVC (it seems to just use the default serializer for the posted content type). – BrainSlugs83 May 05 '17 at 15:52

0 Answers0