Skip to content

JsonPropertyName inheritance perserve the parent member #51165

@goyzhang

Description

@goyzhang

When JsonPropertyName is applied to a sub-class override member, the original name in the parent class is preserved in the output of JsonSerializer.Serialize, as a result, that's two JSON field. I don't think this is by design?

Description

abstract class Person
{
    public abstract string Name { get; set; }
}
class Student : Person
{
    [JsonPropertyName("StudentName")]
    public override string Name { get; set; }
}

var json = JsonSerializer.Serialize(new Student() { Name = "Alice" });
  • Expected:
    {"StudentName":"Alice"}
  • Actual:
    {"StudentName":"Alice","Name":"Alice"}

Configuration

  • Which version of .NET is the code running on?
    .NET 5.0.2
  • What OS and version, and what distro if applicable?
    Windows 10 2004
  • What is the architecture (x64, x86, ARM, ARM64)?
    X64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions