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
JsonSerializer.Serialize produces invalid JSON for [JsonExtensionData] property #97225
When serializing object containing property marked with [JsonExtensionData] attribute, serializer produces invalid JSON like this one: {"Id":1,{"nested":true}}
However, the example code in the report uses JsonObject as extension data property - an IDictionary<string, JsonNode>, which according to the JsonExtensionDataAttribute documentation is not an allowed dictionary type.
So, basically the problem is either
the documentation is correct, but the (de)serializer fails to respond with an appropriate error/exception when encountering an unsupported extension data property type.
IDictionary<string, JsonNode> is supposed to be supported as extension data property type, in which case there is both a bug in the implementation and an error in the JsonExtensionDataAttribute documentation.
The documentation is correctly stating that JsonObject is one of the supported types. However there is a bug specifically impacting serialization for the particular type (FWIW JsonExtensionData is a feature primarily oriented towards dserialization).
To capture extra data such as these properties, apply the [JsonExtensionData] attribute to a property of type Dictionary<string,object> or Dictionary<string,JsonElement>.
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:
<value>The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary<string, JsonElement>' or 'IDictionary<string, object>', or be 'JsonObject'.</value>
</data>
In other words, what I mentioned earlier holds. JsonObjectis supported and there is a bug specifically concerning serialization.
Activity
eiriktsarpalis commentedon Jan 19, 2024
Can confirm that this occurs. It seems we never added testing for the serialization scenario -- deserialization appears to be working as expected.
elgonzo commentedon Jan 19, 2024
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
JsonObjectas extension data property - anIDictionary<string, JsonNode>, which according to the JsonExtensionDataAttribute documentation is not an allowed dictionary type.So, basically the problem is either
IDictionary<string, JsonNode>is supposed to be supported as extension data property type, in which case there is both a bug in the implementation and an error in theJsonExtensionDataAttributedocumentation.@eiriktsarpalis
Does this then mean the documentation for JsonExtensionDataAttribute is wrong about the types allowed for extension data properties?
eiriktsarpalis commentedon Jan 19, 2024
The documentation is correctly stating that
JsonObjectis one of the supported types. However there is a bug specifically impacting serialization for the particular type (FWIWJsonExtensionDatais a feature primarily oriented towards dserialization).elgonzo commentedon Jan 19, 2024
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:
eiriktsarpalis commentedon Jan 19, 2024
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.
JsonObjectis supported and there is a bug specifically concerning serialization.elgonzo commentedon Jan 19, 2024
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?
eiriktsarpalis commentedon Jan 19, 2024
It would help if you could file a separate issue in dotnet-api-docs. Thanks!
RutulPatel8 commentedon Jan 20, 2024
Yes I have tested with Dot net core 6 version.

It is reproducible.
TheArcaneBrony commentedon Mar 23, 2025
Still reproducible in dotnet 9
Fix JsonSerializer.Serialize producing invalid JSON for [JsonExtensio…