Skip to content

Adding JsonIgnoreAttribute to new property fails with ArgumentException #76268

Closed

Description

Description

using System.Text.Json;
using System.Text.Json.Serialization;

ClassB b = new ClassB();
var test = JsonSerializer.Serialize(b);
Console.ReadLine();

public class ClassA
{
    [JsonIgnore]
    public string test { get; set; }
}

public class ClassB : ClassA
{
    [JsonIgnore]
    public new string test { get; set; }
}

The above code will throw an exception: System.ArgumentException:“An item with the same key has already been added. Key: test”

When both base class and derived class has a property with the same name (derived class using new to override property in base class), and both of this two properties marked as [JsonIgnore], Serialize function throw this exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions