Utf8Json icon indicating copy to clipboard operation
Utf8Json copied to clipboard

Properties not properly serialized when serializing polymorphic types in a List

Open mindcrash opened this issue 5 years ago • 7 comments

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.

mindcrash avatar Feb 14 '19 14:02 mindcrash