If I have the following classes:
public class ParentClass
{
    public int ParentProperty { get; set; } = 0;
}
public class ChildClass : ParentClass
{
    public string ChildProperty { get; set; } = "Child property";
}
public class Container
{
    public double ContainerCapacity { get; set; } = 0.2;
    public List<ParentClass> ClassContainer { get; set; } = new List<ParentClass>();
}
And if I then create the following objects in Program.cs:
// Objects
var container = new Container() { ContainerCapacity = 3.14 };
var parent = new ParentClass() { ParentProperty = 5 };
var child = new ChildClass() { ParentProperty = 10, ChildProperty = "value" };
container.ClassContainer.Add(parent);
container.ClassContainer.Add(child);
// Serialization
var serializerOptions = new JsonSerializerOptions() { WriteIndented = true };
var containerJson = JsonSerializer.Serialize(container, serializerOptions);
Console.WriteLine(containerJson);
Expected output:
{
  "ContainerCapacity": 3.14,
  "ClassContainer": [
    {
      "ParentProperty": 5
    },
    {
      "ChildProperty": "value",
      "ParentProperty": 10
    }
  ]
}
Actual output:
{
  "ContainerCapacity": 3.14,
  "ClassContainer": [
    {
      "ParentProperty": 5
    },
    {
      "ParentProperty": 10
    }
  ]
}
How can I make sure that the property ChildProperty on child gets serialized as well? How would I go about it for interface polymorphism?
 
     
    