I have the following piece of code creating an object and writing into a file and reading from that file and trying to deserialize it into the same object(The code may look pointless but I simplified the large code into a small sample code to highlight the issue):
internal class Program
{
private static void Main(string[] args)
{
string filePath = Path.Combine(@"C:\Users\user1", "TestFile.txt");
TemplateA templateA = new TemplateA();
templateA.objectsList = new List<TemplateX>();
TemplateX templateX = new TemplateX();
templateX.property1 = "Sample Value X1";
TemplateY templateY = new TemplateY();
templateY.property1 = "Sample value Y1";
templateY.property2 = "Sample value Y2";
templateA.objectsList.Add(templateX);
templateA.objectsList.Add(templateY);
string json = JsonConvert.SerializeObject(templateA, Formatting.Indented);
File.WriteAllText(filePath, json);
string jsonString = File.ReadAllText(filePath);
TemplateA templateACopy = JsonConvert.DeserializeObject<TemplateA>(jsonString);
}
}
internal class TemplateA
{
[JsonProperty(PropertyName = "objectsList")]
public List<TemplateX> objectsList;
}
internal class TemplateX
{
[JsonProperty(PropertyName = "property1")]
public string property1;
}
internal class TemplateY : TemplateX
{
[JsonProperty(PropertyName = "property2")]
public string property2;
}
When I read back the same object templateA written to TextFile.txt into templateACopy, it is loosing the information of property Y2("Sample value Y2"). That is templateACopy has:
This can be corrected by manually checking the string if it has elements of Class TemplateY and deserializing with the appropriate object type. But is there a way to autodetect the object is of an inherited type and deserialize into the appropriate object by the Newtonsoft JsonConvert's functions itself? (It is not known prior whether the json string has objects of TemplateX or TemplateY type. This can change at runtime.)