This project has moved and is read-only. For the latest updates, please go here.

Can I resolve subclass in deserialize?

Aug 10, 2013 at 4:34 AM
public interface IPerson {
  string Name { get; set; }
public class Visitor : IPerson {
public class Staff : IPerson {
    string Division { get; set; }
public class Manager : Staff {
   string[] members { get; set; }
  "subscriber": [
    {"type": "Visitor", "name": "Taro"},
    {"type": "Staff", "name": "Hanako", "division": "R&D"},
    {"type": "Manager", "name": "Kacho", "division": "R&D", members: ["Hanako"]}
I want to deserialize such case, conditional deserialization by property.
Is there any way to solve this?

I think CustomCreationConverter may help me, but cannnot understand how to use it for conditional deserialization.
Aug 15, 2013 at 9:59 PM
Edited Aug 15, 2013 at 10:00 PM
I had the exact same question (and in fact used "type" to hint at the class name), so I delved into the source code of Json.Net and I'm pretty sure it doesn't support a feature where you could say that "someproperty" is a hint to the class name, so supply this hint to some (made up) IObjectResolver so that it could instantiate an object.

Json.Net supports a "special" property "$type", but the value needs to be a fully qualified class name: "MyNamespace.MyClassName"
Aug 18, 2013 at 8:25 AM
One way to do it:
modify your (to be deserialized) JSON to generate a "$type" name instead of a "type" name then use the SerializationBinder by looking at the SerializeSerializationBinder.cs sample.