Skip to content

Adding JsonIgnoreAttribute to new property fails with ArgumentException #76268

Closed

Description

@liuliang-wt

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.

Activity

  1. transferred this issue fromdotnet/coreon Sep 27, 2022
  2. ghost added
    untriaged
    on Sep 27, 2022
  3. added and removed
    untriaged
    on Sep 28, 2022
  4. added this to the 8.0.0 milestone on Sep 28, 2022
  5. eiriktsarpalis commented on Sep 28, 2022

    @eiriktsarpalis
    Member

    I can reproduce -- this belongs in the same class of bugs as #73255. This one is more interesting as it's actually failing with an exception during contract resolution.

  6. changed the title [-]System.Text.Json.JsonSerializer.Serialize bug[/-] [+]adding JsonIgnoreAttribute to `new` property fails with ArgumentException[/+] on Sep 28, 2022
  7. changed the title [-]adding JsonIgnoreAttribute to `new` property fails with ArgumentException[/-] [+]Adding JsonIgnoreAttribute to `new` property fails with ArgumentException[/+] on Sep 28, 2022
  8. modified the milestones: 8.0.0, Future on Sep 28, 2022
  9. ghost added
    in-pr
    on Apr 13, 2023
  10. ghost removed
    in-pr
    on Jun 5, 2023
  11. ghost locked as resolved and limited conversation to collaborators on Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions