1

I am trying to implement a pretty simple JsonConverter in an ASP.net Web Api MVC 3 application. Previously this was not possible because the WCF client used Microsoft serialization by default.

Anyways I would like to send my API some Json like this...

[{
"WorkerID" : 456,
"Company" : "ACompany",
"DOB" : "asdasd"
},
{
"ContactId" : 123,
"Name" : "roger",
"DOB" : "asdasd"
}]

And have the API know what type of object to deserializing the Json into. Currently though my code is only deserializing it the base object. So if the Json object has a WorkerID attribute the deserialization knows that it is a worker object, or if it has a ContactId it is a Contact.

Here is some code that will hopefully make it more clear.

[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 string DOB { get; set; }
}

public class Contact : Person
{
    public int ContactId { get; set; }
    public string Name { get; set; }
    public string DOB { get; set; }
}

[JsonConverter(typeof(PersonConverter))]
public class Person
{
    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);
    }
}

So currently this is being deserialized into just Person objects, when I beleive it should deserialize it into Worker or Contact objects. The response I am getting is going to be something like...

[{DOB : "asdasd"}, {DOB : "asdasd"}]

Instead of the objects I am looking for...

Thanks in advance for the help.

2
  • See serialization/deserialization code in this question. Commented Jun 27, 2013 at 0:24
  • The problem with that is he is not deserializing the incoming data and mapping it to classes right from the web api. Mine works if I just deserialize a string of JSON using the converter. But I cannot get the API to use the converter to run when deserializing the incoming data automatically. Thanks for the help though.
    – recneps
    Commented Jun 27, 2013 at 16:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.