This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Added snake case naming policy to the JSON serializer #41354
Added snake case naming policy to the JSON serializer #41354
Changes from 4 commits
3b82d9d
76b5503
d5d4bc3
9c8dd6b
5ef51a3
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ahsonkhan Out of curiosity, when method already contains the return type name is it okay to use
var
on the left side?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so (but it is a bit of a grey zone). Use
var
when the RHS hasnew
or otherwise it is clear what the type is from reading that line (without having prior knowledge of what the calling API is doing - such as an explicit cast).I can see the argument that GetX methods return X and hence it is clear, so this comes down to what is considered "obvious" (and probably personal preference). What if GetX was returning an interface/abstract type (like IX)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why we need to special case TitlecaseLetter. Can we ignore them?
https://www.fileformat.info/info/unicode/category/Lt/list.htm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that they cannot be lowercased? I haven't worked with Unicode a lot, so it's out of my knowledge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am also not super familiar with all the categories. I am asking if you had a test case or scenario that motivated including
TitlecaseLetter
in the implementation to begin with. It looks like they do have lower-case counterparts. Again, adding tests where we exercise thisUnicodeCategory
more would be useful. I believe, if we remove this switch case, all the existing tests would still pass, which means there is a test gap.@GrabYourPitchforks - do you have some thoughts on the unicode categories we are special casing here for snake casing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am primarily asking for adding more tests so that we can more confidently optimize the implementation later. I want to avoid removing some code path in the future to improve perf that would end up breaking functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked. They all can be lowered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Put brackets around the && conditions to make the ordering of operations clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the rational for special casing
DecimalDigitNumber
? Can you share/add a test case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This category contains a bunch of characters. Is 0-9 sufficient?
https://www.fileformat.info/info/unicode/category/Nd/list.htm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests already exists for numbers in names. One of them you have used in benchmarks (:
Since the policy should put underscores and not remove any characters and numbers including non-ASCII, this category should be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I see the test case in the bug npgsql/npgsql#2152.
ABC123 -> abc123
clearly indicates the need for using 0-9 (ab_c123
is clearly wrong). I see the numbers tests now :)To be explicit, having non-ASCII digit tests would be good too (i.e. pick others from the Nd list).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's about non-decimal numbers, math symbols and other characters? Maybe invert the logic and write everything except punctuation and spaces?
/cc @GrabYourPitchforks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me.
Not sure how accurate this is, but it is a data point (from https://capitalizemytitle.com/camel-case/):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Add braces around the if since this is in the nested scope.
From https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md#c-coding-style
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding tests for invalid surrogate characters too (which is possible by doing a substring in between a surrogate pair of characters or crafting one using chars).
So:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already have one of them. It's 42.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The special
42
contains valid surrogate pairs (https://www.fileformat.info/info/unicode/char/1d7dc/index.htm).I am talking about a string that is invalid (i.e. doesn't contain the correct pair of surrogate characters).
For example:
corefx/src/System.Text.Json/tests/JsonDocumentTests.cs
Lines 3545 to 3546 in 5cee7c9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed word "invalid", sorry. Will do (:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here (braces).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-ascii tests like these should be written using escaped/hex characters.
See:
corefx/src/System.Text.Json/tests/Utf8JsonReaderTests.cs
Line 1993 in 5cee7c9
So, for this case (same for the special "42" below):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is short enough to be one line, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
NamingPolictyTestClass
->NamingPolicyTestClass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
DictionaryKeyPolicy = JsonNamingPolicy.SnakeCase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DictionaryKeyPolicy
is only applied on serialization. The way to test this is to have a dictionary with keys that are some other casing like Pascal case, and make sure the serialized content is snake case.