2

I am consuming a RESTful Web service sending JSON, that I try to deserialize using HttpContent.ReadAsAsync<T>. The type I try to deserialize to declares a property that returns an IEnumerable containing an interface type. This code snippet demonstrates the kind of type I'm trying to deserialize to:

public class Data
{
    public IEnumerable<IChild> Children { get; set; };
}

The problem is that Newtonsoft.Json, underlying HttpContent.ReadAsAsync<T> doesn't understand how to deserialize objects of type IChild, the latter being an interface. How can I specify to Newtonsoft.Json how to deserialize IChild to a concrete type?

1
  • @L.B Does it matter in this case? The problem is just how to tell Newtonsoft.Json to convert the interface (IChild) to a concrete type, f.e.x Child. Commented Aug 30, 2012 at 13:32

2 Answers 2

4

You can use a custom Converter to tell JSON.NET how to deserialize types of that interface. The code below shows an example.

public class StackOverflow_12197892
{
    public class Data
    {
        public IEnumerable<IChild> Children { get; set; }

        public override string ToString()
        {
            return string.Format("Data{{Children=[{0}]}}",
                string.Join(", ", Children.Select(c => string.Format("{0}/{1}", c.Name, c.IsFemale ? "girl" : "boy"))));
        }
    }
    public interface IChild
    {
        string Name { get; }
        bool IsFemale { get; }
    }
    public class Son : IChild
    {
        public Son(string name)
        {
            this.Name = name;
        }

        public string Name { get; private set; }
        public bool IsFemale { get { return false; } }
    }
    public class Daughter : IChild
    {
        public Daughter(string name)
        {
            this.Name = name;
        }

        public string Name { get; private set; }
        public bool IsFemale { get { return true; } }
    }
    class ChildConverter : Newtonsoft.Json.JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(IChild).IsAssignableFrom(objectType);
        }

        public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
        {
            JObject obj = serializer.Deserialize<JToken>(reader) as JObject;
            if (obj != null)
            {
                bool isFemale = obj["isFemale"].ToObject<bool>();
                string name = obj["name"].ToObject<string>();
                if (isFemale)
                {
                    return new Daughter(name);
                }
                else
                {
                    return new Son(name);
                }
            }
            else
            {
                return null;
            }
        }

        public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
    public static void Test()
    {
        Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
        serializer.Converters.Add(new ChildConverter());
        string json = "{'Children':[{'name':'John',isFemale:false},{'name':'Mary',isFemale:true}]}".Replace('\'', '\"');
        var obj = serializer.Deserialize(new StringReader(json), typeof(Data));
        Console.WriteLine(obj);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. How would you integrate the converter with HttpContent.ReadAsAsync<T> though, as this calls Newtonsoft.Json indirectly?
You'd need to set the converter in the formatter on the content.
I configured the converter by setting [ConverterAttribute] on IChild.
0

It is quite simple and out of the box support provided by json.net, you just have to use the following JsonSettings while serializing and Deserializing:

JsonConvert.SerializeObject(graph,Formatting.None, new JsonSerializerSettings()
                                                                                               {
                                                                                                 TypeNameHandling = TypeNameHandling.Objects,
                                                                                               TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
                                                                                           });

and for Deserialzing use the below code:

JsonConvert.DeserializeObject(Encoding.UTF8.GetString(bData),type,
                                                       new JsonSerializerSettings()
                                                           {TypeNameHandling = TypeNameHandling.Objects});

Just take a note of the JsonSerializerSettings object initializer, that is important for you.

Comments

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.