I have a Parent class and a Child class, the json string is containing properties of Child class but when I do a DeserializeObject I can't get those properties because Child became a Parent.
For example :
class Data {
public List<Parent> Parents;
}
class Parent {
public string Foo;
}
class Child : Parent {
public string Bar;
}
var data = new Data { Parents = new List<Parent> { new Child{Bar = "a"} } };
Console.WriteLine(data.Parents.Count); // <= it prints 1
Console.WriteLine(data.Parents.OfType<Child>().Count()); // <= it prints 1
var dataJson = JsonConvert.SerializeObject(data);
data = JsonConvert.DeserializeObject<Data>(dataJson);
Console.WriteLine(data.Parents.Count); // <= it prints 1
Console.WriteLine(data.Parents.OfType<Child>().Count()); // <= it prints 0 instead of 1
What can I do about that ? Newtonsoft.Json can manage to do what I want ?