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
When serializing a class that inherits from an abstract or virtual base class using System.Text.Json, both the properties from the parent class and the child class are serialized, even when the child class overrides or hides the parent properties using the new or override keywords. This behavior leads to duplicate entries for the same property in the serialized JSON output, which is undesirable and unexpected.
Example Code:
using System;using System.Text.Json;using System.Text.Json.Serialization;publicabstractclassParent{publicstringName{get;set;}publicintAge{get;set;}}publicclassChild:Parent{[JsonPropertyName("full_name")]publicnewstringName{get;set;}// Hides the parent property[JsonPropertyName("child_age")]publicoverrideintAge{get;set;}// Overrides the parent property}classProgram{staticvoidMain(string[]args){Childchild=new Child
{Name="John Doe",Age=10};varoptions=new JsonSerializerOptions
{WriteIndented=true};stringjson= JsonSerializer.Serialize(child, options);
Console.WriteLine(json);}}
Reproduction Steps
Steps to Reproduce:
Create an abstract base class (Parent) with properties.
Create a child class (Child) that inherits from the base class and either:
Uses the new keyword to hide the base class properties.
Uses the override keyword to override base class properties.
Apply the [JsonPropertyName] attribute to the properties in the child class to customize their names in the JSON output.
Serialize an instance of the Child class using System.Text.Json.
Expected behavior
Only the properties from the child class should be serialized, with the custom names provided by the [JsonPropertyName] attribute. The parent properties should not be serialized if they are hidden or overridden by the child class.
Expected JSON output:
{
"full_name": "John Doe", // Only the child property should be serialized"child_age": 10// Only the child property should be serialized
}
Actual behavior
The JSON output contains both the parent and child properties, leading to duplication:
{
"full_name": "John Doe", // From child class (expected)"Name": "John Doe", // From parent class (unexpected)"child_age": 10, // From child class (expected)"Age": 10// From parent class (unexpected)
}
Regression?
No response
Known Workarounds
No response
Configuration
Using .NET8 on Windows 11
Other information
It is simple app, but in our case we want to use JsonPropertyName to rename our properties during the serialization. But because of back compatibility we can't touch or ignore parent properties, that is why we trying to override the properties in new version child class so both can work at the same time.
The text was updated successfully, but these errors were encountered:
Having difference property understandable, but why override properties duplicate? Without using jsonpropertyname there is no duplicate because both has the same key name. Json property name cause having different name and serialization use both name and duplicate the property with different name. And I don't think this expected behavior.
Description
When serializing a class that inherits from an abstract or virtual base class using System.Text.Json, both the properties from the parent class and the child class are serialized, even when the child class overrides or hides the parent properties using the new or override keywords. This behavior leads to duplicate entries for the same property in the serialized JSON output, which is undesirable and unexpected.
Example Code:
Reproduction Steps
Steps to Reproduce:
Expected behavior
Only the properties from the child class should be serialized, with the custom names provided by the
[JsonPropertyName]
attribute. The parent properties should not be serialized if they are hidden or overridden by the child class.Expected JSON output:
Actual behavior
The JSON output contains both the parent and child properties, leading to duplication:
Regression?
No response
Known Workarounds
No response
Configuration
Using .NET8 on Windows 11
Other information
It is simple app, but in our case we want to use JsonPropertyName to rename our properties during the serialization. But because of back compatibility we can't touch or ignore parent properties, that is why we trying to override the properties in new version child class so both can work at the same time.
The text was updated successfully, but these errors were encountered: