-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JsonSerializer.Serialize produces invalid JSON for [JsonExtensionData] property #97225
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionWhen serializing object containing property marked with Reproduction StepsRun the following code var mix = new Mix
{
Id = 1,
Extra = new() { ["nested"] = true, }
};
var text = System.Text.Json.JsonSerializer.Serialize(mix);
Console.WriteLine(text);
// output {"Id":1,{"nested":true}}
public class Mix
{
public int Id { get; set; }
[System.Text.Json.Serialization.JsonExtensionData]
public System.Text.Json.Nodes.JsonObject? Extra { get; set; }
} Expected behaviorCorrect JSON like Actual behaviorInvalid JSON Regression?Reproducible at least on .Net 6 to .Net 8 Known WorkaroundsNo response Configuration.Net 8 Other informationNo response
|
Can confirm that this occurs. It seems we never added testing for the serialization scenario -- deserialization appears to be working as expected. |
There is a related question as to whether this scenario is supposed to be allowed or not. Because the documentation for JsonExtensionDataAttribute states:
However, the example code in the report uses So, basically the problem is either
Does this then mean the documentation for JsonExtensionDataAttribute is wrong about the types allowed for extension data properties? |
The documentation is correctly stating that |
You seem to be mistaken. Where does the documentation for JsonExtensionDataAttribute state this? The related documentation page "How to handle overflow JSON or use JsonElement or JsonNode in System.Text.Json" also does not support your claim:
|
You're right, I misread the documentation which appears to be out of date. The correct statement on supported types can actually be found in the error messages of the implementation itself: runtime/src/libraries/System.Text.Json/gen/Resources/Strings.resx Lines 150 to 152 in 2d751ca
In other words, what I mentioned earlier holds. |
Okay. Regarding the documentation being out of date, should i file an issue report with the docs repo, or do you handle this internally between teams? |
It would help if you could file a separate issue in dotnet-api-docs. Thanks! |
Description
When serializing object containing property marked with
[JsonExtensionData]
attribute, serializer produces invalid JSON like this one:{"Id":1,{"nested":true}}
Reproduction Steps
Run the following code
Expected behavior
Correct JSON like
{"Id":1,"nested":true}
or at least valid JSON as if there was not[JsonExtensionData]
attribute ({"Id":1,"Extra":{"nested":true}}
).Actual behavior
Invalid JSON
{"Id":1,{"nested":true}}
Regression?
Reproducible at least on .Net 6 to .Net 8
Known Workarounds
No response
Configuration
.Net 8
Windows 11
x64
Other information
No response
The text was updated successfully, but these errors were encountered: