What's the correct way to translate interface-type properties to concrete types?

Aug 31, 2012 at 9:01 AM
Edited Aug 31, 2012 at 9:03 AM

My problem is that I have a class I need to deserialize JSON objects to, that has an IEnumerable of child objects of an interface type and I don't have direct access to the JSON.Net deserializer as it is handled by framework code (HttpContent.ReadAsAsync<T>). How do I best specify the translation between the interface type and a concrete type?

I've tried setting the JsonConverter attribute on the interface definition, but this results in my JsonConverter being invoked to deserialize also for the concrete implementation of this interface :( The below code snippet shows a test console program that attempts to deserialize to a class 'Parent' that has an 'IEnumerable<IChild>', where IChild should be deserialized to the Child type. But, a stack overflow exception occurs due to ChildConverter also being invoked for the concrete Child type. If instead of the 'JsonConverter' attribute on IChild, I add ChildConverter to serializer.Converters however, the program works. See also my stackoverflow question regarding this issue.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;

namespace TestConsole
{
    [JsonConverter(typeof(ChildConverter))]
    interface IChild
    {
        string Name { get; set; }
    }

    class Child : IChild
    {
        public string Name { get; set; }
    }

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

    class ChildConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(IChild);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return serializer.Deserialize<Child>(reader);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var json = "{'Children': [{'Name': 'Child1'}, {'Name': 'Child2'}]}";
            var serializer = new JsonSerializer();
            //serializer.Converters.Add(new ChildConverter());
            var obj = serializer.Deserialize(new StringReader(json), typeof(Parent));
            Console.WriteLine(obj);
        }
    }
}