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

Add JSON snake_case naming policy #54128

Closed
wants to merge 2 commits into from

Conversation

FiniteReality
Copy link

Not much attention has been paid to how JSON.Net implements its snake_case policy, but this should be mostly compatible. Some rudimentary tests have been added, copied from CamelCaseUnitTests and tweaked to remove some unreasonable test cases.

Fixes #782

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Jun 13, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

Not much attention has been paid to how JSON.Net implements its snake_case policy, but this should be mostly compatible. Some rudimentary tests have been added, copied from CamelCaseUnitTests and tweaked to remove some unreasonable test cases.

Fixes #782

Author: FiniteReality
Assignees: -
Labels:

area-System.Text.Json, new-api-needs-documentation

Milestone: -

return name.ToLowerInvariant();
}

var result = new ValueStringBuilder(2 * name.Length);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure 2 * length is a reasonable heuristic here, but I wasn't sure how much bigger the "average" property name would be, so I felt just doubling was safer.

Copy link

@AmrAlSayed0 AmrAlSayed0 Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ABC would turn into a_b_c so the absolute maximum would be (2 * name.Length) - 1

Copy link
Author

@FiniteReality FiniteReality Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ABC should turn into abc since the entire string is all the same case; it should be almost the case as ID

Copy link

@AmrAlSayed0 AmrAlSayed0 Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so AbCdEf would be ab_cd_ef which gives us name.Length + (int)Math.Ceiling(name.Length/(double)2) - 1

Edit: Actually it is more complicated than that. There are also single char numbers. I would still say that (2 * name.Length) - 1 is the absolute maximum in all cases.


result.Append(current);

if (x < name.Length - 1)
Copy link
Author

@FiniteReality FiniteReality Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it more, this condition will likely accidentally trigger if two or more numbers are hit in sequence. In this scenario, what would a user expect the name to be converted to? I know abc_1__2_def is most likely incorrect, but should it be abc_12_def, or abc_1_2_def?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I've just left this as abc_1_2_def, and have added a relevant test case.

@FiniteReality
Copy link
Author

Gah, rebasing is a pain sometimes. Excuse the force push; I'm resolving conflicts 😅

Not much attention has been paid to how JSON.Net implements its
snake_case policy, but this should be mostly compatible. Some
rudimentary tests have been added, copied from CamelCaseUnitTests.

Fixes dotnet#782
@terrajobst terrajobst added the community-contribution Indicates that the PR has been added by a community member label Jul 19, 2021
@layomia
Copy link
Contributor

layomia commented Jul 22, 2021

Thanks for the PR. Per #782 (comment) and other design-related comments on that thread, this feature needs a bit more work. To do it properly, we should try again in .NET 7. We'll ping this PR at that time.

@layomia layomia closed this Jul 22, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Aug 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add snake_case support for System.Text.Json
5 participants