Let us say I have the following:
public class Parent
{
public string Id;
}
public class FirstChild:Parent
{
public string FirstName;
}
public class SecondChild:Parent
{
public string LastName;
}
and I have a Json of type FirstChild
or SecondChild
, how can I deserialize it to suitable type without having to check contents of serialized JSON knowing that I cann't control the serialization process?
I have tried the solution mentioned here, but what I am getting is the Parent
object (fields of children are gone).
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
var deserialized = JsonConvert.DeserializeObject<Parent>(
Serialized FirstChild/SecondChild,
settings);
A Json Sample:
{\"firstName\":\"John\",\"id\":\"1\"}