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

Unexpected CamelCase formatting in System.Text.Json #46445

Closed
Ilchert opened this issue Dec 29, 2020 · 6 comments
Closed

Unexpected CamelCase formatting in System.Text.Json #46445

Ilchert opened this issue Dec 29, 2020 · 6 comments
Assignees
Labels
area-System.Text.Json question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@Ilchert
Copy link

Ilchert commented Dec 29, 2020

Description

Unexpected formatting for dictionary keys using System.Text.Json.

Expected value some_3.
Actual value somE_3.

var o = new JsonSerializerOptions()
{
     DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
};
var data = new Dictionary<string, object> { { "SOME_3", "" } };
var text = JsonSerializer.Serialize(data, o); // {"somE_3":""}

Configuration

  • Which version of .NET is the code running on? NET5.0.101
  • What OS and version, and what distro if applicable? Win10
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? No

Regression?

Previously the value had not changed.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Dec 29, 2020
@huoyaoyuan
Copy link
Member

Are you using ASP.NET? This is a known change in ASP.NET Core 5.0.

@Ilchert
Copy link
Author

Ilchert commented Dec 29, 2020

@huoyaoyuan yes, aspnet. Could you provide a description of this change?

@huoyaoyuan
Copy link
Member

dotnet/docs#21067

@Ilchert
Copy link
Author

Ilchert commented Dec 29, 2020

@huoyaoyuan, thanks for the link, but my issue about upper case of char E in SOME_3 -> somE_3 transformation, not about quoted numbers.

@huoyaoyuan
Copy link
Member

The camel case handler does not have well-defind behavior for all capitalized. You should use an option to not convert the name at all. It there already such option?

@layomia
Copy link
Contributor

layomia commented Feb 3, 2021

Preliminary note that System.Text.Json has the same behavior as Newtonsoft.Json here - https://dotnetfiddle.net/ViFFmX.

We'll likely not do any work here (given it would be a non-trivial breaking change), unless something related spills out of the feature work for snake_case support (#782) in an effort to have a central algorithm between snake case, camel case, and any feature casing policies.

The work around here is to implement a custom JsonNamingPolicy which uses JsonNamingPolicy.CamelCase as the main logic, but handles edge-cases such as you've described above.

@layomia layomia self-assigned this Feb 3, 2021
@layomia layomia added this to the 6.0.0 milestone Feb 3, 2021
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Feb 3, 2021
@layomia layomia modified the milestones: 6.0.0, 7.0.0 Jul 23, 2021
@eiriktsarpalis eiriktsarpalis added the question Answer questions and provide assistance, not an issue with source code or documentation. label Oct 15, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Nov 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

5 participants