I have the following classes (names and property names have been changed):
[KnownType(typeof(Module))]
[DataContract]
public class BaseModule
{
[DataMember]
public int Id { get; set; }
}
[DataContract]
public class Module : BaseModule
{
[DataMember]
public string Name { get; set; }
}
[DataContract]
public class ModuleContainer
{
[DataMember]
public ModuleBase mod { get; set; }
}
So, when using:
var json = @"{\"mod\":{
\"Name\":\"The Name\",
\"Id\":10
}}";
var result = JsonConvert.DeserializeObject<ModuleContainer>(json);
The resulting object's "mod" member can't be successfully converted to type Module, and serializing the object again yields no "Name" member. It seems like Json.Net is unable to recognize the inherited type (Module) and instead gives me a "ModuleBase" object.
I have a fairly large number of inherited classes (all different, and all covered by KnownType on ModuleBase). Is there a way in which I can convince JsonConvert to grab the appropriate type?
Let me know if you need more information, or that was confusing.