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
While trying to serialize a JsonElement with Pascal case properties to camel case, I expected that I could just set the JsonSerializerOptions.PropertyNamingPolicy to JsonNamingPolicy.CamelCase and be done. However, according to my tests and the source code, the JsonCoverter for that type just writes out the raw JSON it parsed, without transforming property names.
Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.
Issue Details
Description
While trying to serialize a JsonElement with Pascal case properties to camel case, I expected that I could just set the JsonSerializerOptions.PropertyNamingPolicy to JsonNamingPolicy.CamelCase and be done. However, according to my tests and the source code, the JsonCoverter for that type just writes out the raw JSON it parsed, without transforming property names.
This is by design -- the JsonDocument and JsonElement converters implement serialization using the WriteTo method. This reflects the design of JsonDocument: it's a read-only wrapper around a JSON encoded buffer focusing on performance. It does validate that the encoding is valid JSON but will otherwise copy the contents essentially verbatim.
Starting with .NET 6 my recommendation is to use JsonNode instead: it's a read-writer JSON DOM representation that offers some degree customizing serialization.
Description
While trying to serialize a
JsonElement
with Pascal case properties to camel case, I expected that I could just set theJsonSerializerOptions.PropertyNamingPolicy
toJsonNamingPolicy.CamelCase
and be done. However, according to my tests and the source code, theJsonCoverter
for that type just writes out the raw JSON it parsed, without transforming property names.Reproduction Steps
Expected behavior
JsonElement serialization should respect JsonSerializerOptions, or at least, the PropertyNamingPolicy option.
Actual behavior
The serialized JsonElement just returns the case of the original JSON, ignoring all JsonSerializationOptions.
Regression?
No, I don't think this was available in previous releases.
Known Workarounds
No response
Configuration
Version: .NET 6.0, though this code was introduced in .NET 5.0.
Other information
No response
The text was updated successfully, but these errors were encountered: