This project has moved. For the latest updates, please go here.

TypeNameHandling and generics

Apr 13, 2011 at 12:17 AM

I am trying to serialize an generic list like the following:


    class Test {
        [JsonProperty(TypeNameHandling = TypeNameHandling.All)]
        public List<Base> Listing { get; set; }

    public interface Base { }

    public class Derived1 : Base {

Test t = new Test();
t.Listing = new List<Base>{new Derived1());

When serializing an instance of Test class, it stores the type info of Base (instead of Derived1) in the Json string; thus deserialization will fail. I searched the forum and found that the Dictionary<string,object> will work. But anything like Dictionary<string,Base> will not work during deserialization. Do I need to use JsonConvert to custom convert my classes, or is there an easy way to do it? I tried all combinations of TypeNameHandling and none works.

Apr 13, 2011 at 12:18 AM

Forgot to mention this is on .NET 4.0 with Json.NET 4.0 release 1.