You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JsonPropertyName inheritance perserve the parent member #51165
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?
Triage - this isn't a regression from .NET 5.0 and I think it's a bit too late in the 6.0 wave to introduce a breaking change like this. We should consider in .NET 7.0.
abstract class Person
{
[JsonPropertyName("StudentName")]
public abstract string Name { get; set; }
}
class Student : Person
{
public override string Name { get; set; }
}
var json = JsonSerializer.Serialize(new Student() { Name = "Alice" });
For those who may be looking for a work around until this can be addressed. You can make use of a dummy property:
abstract class Person
{
public abstract string Name { get; set; }
}
class Student : Person
{
[JsonIgnore]
public override string Name { get => base.Name; set => base.Name = value; }
public string StudentName { get => base.Name; set => base.Name = value; }
}
Just ran into this as well. I was hoping to avoid contract customization as it feels a bit heavy for this; might try the workaround posted above instead. Would be nice if it worked out of the box though
This is causing source generation to generate invalid code when it interacts with JsonIgnoreCondition.WhenWritingDefault or JsonIgnoreCondition.WhenWritingNull.
Activity
eiriktsarpalis commentedon Apr 13, 2021
That seems like a bug to me. FWIW I can reproduce the same behaviour when I create this combination:
but the issue goes away when I remove the
JsonPropertyNameattribute altogether. cc @layomiadevsko commentedon Apr 24, 2021
@eiriktsarpalis I can have a look. Even if it's a bug - fixing this would be a breaking change.
layomia commentedon Jul 23, 2021
Triage - this isn't a regression from .NET 5.0 and I think it's a bit too late in the 6.0 wave to introduce a breaking change like this. We should consider in .NET 7.0.
2 remaining items
simon10says commentedon Jan 11, 2022
Is there any workaround?
rogerfar commentedon Jun 29, 2022
Another bug related to this issue.
Results in 2 properties:
krwq commentedon Jul 7, 2022
Unfortunately we won't have time to address this in 7.0 but contract customization can be used to workaround this. Moving to 8.0 in the meantime
kgbaum commentedon Feb 28, 2023
For those who may be looking for a work around until this can be addressed. You can make use of a dummy property:
pinkfloydx33 commentedon Mar 13, 2023
Just ran into this as well. I was hoping to avoid contract customization as it feels a bit heavy for this; might try the workaround posted above instead. Would be nice if it worked out of the box though
tanveerbadar commentedon Feb 18, 2024
This is causing source generation to generate invalid code when it interacts with
JsonIgnoreCondition.WhenWritingDefaultorJsonIgnoreCondition.WhenWritingNull.#98634
keystroke commentedon Feb 5, 2025
This bug still exists in .net8... produces completely invalid json.
doug62 commentedon Sep 16, 2025
This bug still exists in .net9... produces completely invalid json.
Jeepers - this is serious
tkononowiczsolwit commentedon May 29, 2026
Wait a minute. It has been fixed in #52036 but it wasn't merged. Why?