Skip to content
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

JsonElement does not respect JsonSerializerOptions #61843

Closed
iinuwa opened this issue Nov 19, 2021 · 2 comments
Closed

JsonElement does not respect JsonSerializerOptions #61843

iinuwa opened this issue Nov 19, 2021 · 2 comments
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner

Comments

@iinuwa
Copy link
Contributor

iinuwa commented Nov 19, 2021

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.

Reproduction Steps

using System.Text;
using System.Text.Json;

var json = @"{""ShouldWindUpAsCamelCase"":""does it?""}";
var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
var element = JsonElement.ParseValue(ref reader);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var serialized = JsonSerializer.Serialize(element, options);
Console.WriteLine(serialized);
// returns {"ShouldWindUpAsCamelCase":"does it?"} instead of {"shouldWindUpAsCamelCase":"does it?"}

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

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Nov 19, 2021
@ghost
Copy link

ghost commented Nov 19, 2021

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.

Reproduction Steps

using System.Text;
using System.Text.Json;

var json = @"{""ShouldWindUpAsCamelCase"":""does it?""}";
var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
var element = JsonElement.ParseValue(ref reader);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var serialized = JsonSerializer.Serialize(element, options);
Console.WriteLine(serialized);
// returns {"ShouldWindUpAsCamelCase":"does it?"} instead of {"shouldWindUpAsCamelCase":"does it?"}

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

Author: iinuwa
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

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.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants