Skip to content

Serialization of properties from inherited interfaces vs abstract base classes  #41749

@qsdfplkj

Description

@qsdfplkj

I would expect that inherited properties of interfaces would be serialized similar to (abstract) base classes.

Given:

public interface IBase
{
    string BaseProperty { get; set; }
}

public interface IDerived : IBase
{
    string DerivedProperty { get; set; }
}

public class Derived : IDerived
{
    public string BaseProperty { get; set; }
    public string DerivedProperty { get; set; }
}

public class Data 
{
    public IDerived Derived { get; set; }
}

Data data = new Data 
{ 
    Derived = new Derived
    { 
        BaseProperty = "B", 
        DerivedProperty = "D" 
    } 
};
string json = JsonSerializer.Serialize(data);

Where the json string is missing the BaseProperty:

{"Derived":{"DerivedProperty":"D"}}

replacing the interface with an abstract class results in:

{"Derived":{"DerivedProperty":"D","BaseProperty":"B"}}

public abstract class  IBase
{
    public string BaseProperty { get; set; }
}

public abstract class IDerived : IBase
{
    public string DerivedProperty { get; set; }
}

public class Derived : IDerived
{
}

public class Data 
{
    public IDerived Derived { get; set; }
}

Metadata

Metadata

Labels

Priority:2Work that is important, but not critical for the releasearea-System.Text.Jsonpartner-impactThis issue impacts a partner who needs to be kept updated

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions