Search code examples
c#jsonserializationpolymorphismsystem.text.json

How to serialize derived classes using polymorphism to JSON


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?


Solution

  • I am seeing on the web regarding the issue and it seems it is not doable very easily. I would recommend using Newtonsoft.Json library to parse the objects as it is a mature library and handles the child-parent objects perfectly without an overhead of writing custom settings.

    Install nuget package for Newtonsoft.Json and then parse it like this:

    var containerJson = JsonConvert.SerializeObject(container, Newtonsoft.Json.Formatting.Indented);
    

    Which gives the output like this:

    {
      "ContainerCapacity": 3.14,
      "ClassContainer": [
        {
          "ParentProperty": 5
        },
        {
          "ChildProperty": "value",
          "ParentProperty": 10
        }
      ]
    }
    

    Update: Apologies for suggesting Netwonsoft.json instead of System.Text.Json.

    I would use the following for the System.Text.Json:

    var containerJson = System.Text.Json.JsonSerializer.Serialize(container, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });