-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Open
Labels
area-System.Text.Jsonbugin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Milestone
Description
Description
System.text.json serializes property decorated with JsonPropertyName attribute in the base class is recording 2 records in the output json , one with property name as key and a second with name specified in the attribute.
Reproduction Steps
Console.WriteLine(JsonSerializer.Serialize(new Derived()));
public class Base
{
[JsonPropertyName("test")]
public virtual string Id { get => "base"; set { } }
}
public class Derived : Base
{
public override string Id { get => "derived"; set { } }
}Expected behavior
only single entry with name specified in the JsonPropertyName should be adde to json.
Actual behavior
{"Id":"derived","test":"derived"} -> added twice with different names.
Regression?
We have seen this since the initial version.
Known Workarounds
I see two possible workarounds:
- Try using the base type as the generic parameter when creating your container object. Unlike Json.NET, STJ is sensitive when it comes to what type is being declared statically at the serialization callsite, or
- Replicate the JsonPropertyName property in the overrides. It's more boilerplate, but at least it's the best way to get the desired behavior while we ship a fix.
Configuration
.NET 7
Other information
No response
Xor-el, ravindUwU, neon-sunset, KubaZ2, KalebAxon and 1 moreCopilot
Metadata
Metadata
Assignees
Labels
area-System.Text.Jsonbugin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged