Can I Use Json.Net de-serialization by default with ASP.net Web Api and MVC 3?

自作多情 提交于 2020-01-03 03:43:06

问题


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.

来源:https://stackoverflow.com/questions/17309076/can-i-use-json-net-de-serialization-by-default-with-asp-net-web-api-and-mvc-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!