Utf8Json
Utf8Json copied to clipboard
Properties not properly serialized when serializing polymorphic types in a List
Given the following type structure:
public abstract class Setting
{
public string Name { get; set; }
public abstract string Kind { get; }
}
public class TextSetting : Setting
{
public override string Kind => "text";
public string Value { get; set; }
}
When putting these in a list:
settings = new List<Setting>()
{
new TextSetting(){ Name = "Alpha", Value = "a" }
}
And serializing them to a output stream:
JsonSerializer.SerializeAsync(context.Response.Body, settings, resolver)
The output is:
[{"name":"Alpha","kind":"text"}]
But I expect it to be:
[{"name":"Alpha", "value": "a", "kind":"text"}]
When serializing one item:
JsonSerializer.SerializeAsync(context.Response.Body, new TextSetting(){ Name = "Alpha", Value = "a" }, resolver)
The output is correct:
{"name":"Alpha", "value": "a", "kind":"text"}
So there is definitely a issue within the serializer responsible for serializing iterable types and how it deals with its content.