-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add JsonNamingPolicyAttribute for member-level naming policies in System.Text.Json #124645
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3f50173
Initial plan
Copilot 338a49f
Add JsonNamingPolicyAttribute with runtime resolver and source genera…
Copilot 02dc007
Fix null naming policy regression and add tests for JsonNamingPolicyA…
Copilot 95b0107
Address review: resolve type-level naming policy once per type, handl…
Copilot 6d8bfda
Add tests for custom derived JsonNamingPolicyAttribute in both reflec…
Copilot 3a42322
Use triple-quote strings and Assert.Equal on full JSON in tests
Copilot d6fe147
Merge branch 'main' into copilot/add-member-level-naming-policy
eiriktsarpalis d2ab2e7
Potential fix for pull request finding
eiriktsarpalis 29b31c4
Merge branch 'main' into copilot/add-member-level-naming-policy
eiriktsarpalis f5934fd
Add source gen tests verifying custom derived attribute prevents glob…
Copilot cae477e
Merge branch 'main' into copilot/add-member-level-naming-policy
eiriktsarpalis 8c5da66
Merge branch 'main' into copilot/add-member-level-naming-policy
Copilot dd92d60
Merge branch 'main' into copilot/add-member-level-naming-policy
eiriktsarpalis 0a46ef3
Merge branch 'main' into copilot/add-member-level-naming-policy
eiriktsarpalis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...stem.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonNamingPolicyAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Text.Json.Serialization | ||
| { | ||
| /// <summary> | ||
| /// When placed on a type, property, or field, indicates what <see cref="JsonNamingPolicy"/> | ||
| /// should be used to convert property names. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// When placed on a property or field, the naming policy specified by this attribute | ||
| /// takes precedence over the type-level attribute and the <see cref="JsonSerializerOptions.PropertyNamingPolicy"/>. | ||
| /// When placed on a type, the naming policy specified by this attribute takes precedence | ||
| /// over the <see cref="JsonSerializerOptions.PropertyNamingPolicy"/>. | ||
| /// The <see cref="JsonPropertyNameAttribute"/> takes precedence over this attribute. | ||
| /// </remarks> | ||
| [AttributeUsage( | ||
| AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | | ||
| AttributeTargets.Property | AttributeTargets.Field, | ||
| AllowMultiple = false)] | ||
| public class JsonNamingPolicyAttribute : JsonAttribute | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="JsonNamingPolicyAttribute"/> | ||
| /// with the specified known naming policy. | ||
| /// </summary> | ||
| /// <param name="namingPolicy">The known naming policy to use for name conversion.</param> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// The specified <paramref name="namingPolicy"/> is not a valid known naming policy value. | ||
| /// </exception> | ||
| public JsonNamingPolicyAttribute(JsonKnownNamingPolicy namingPolicy) | ||
| { | ||
| NamingPolicy = ResolveNamingPolicy(namingPolicy); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="JsonNamingPolicyAttribute"/> | ||
| /// with a custom naming policy. | ||
| /// </summary> | ||
| /// <param name="namingPolicy">The naming policy to use for name conversion.</param> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// <paramref name="namingPolicy"/> is <see langword="null"/>. | ||
| /// </exception> | ||
| protected JsonNamingPolicyAttribute(JsonNamingPolicy namingPolicy) | ||
| { | ||
| if (namingPolicy is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(nameof(namingPolicy)); | ||
| } | ||
|
|
||
| NamingPolicy = namingPolicy; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the naming policy to use for name conversion. | ||
| /// </summary> | ||
| public JsonNamingPolicy NamingPolicy { get; } | ||
|
|
||
| internal static JsonNamingPolicy ResolveNamingPolicy(JsonKnownNamingPolicy namingPolicy) | ||
| { | ||
| return namingPolicy switch | ||
| { | ||
| JsonKnownNamingPolicy.CamelCase => JsonNamingPolicy.CamelCase, | ||
| JsonKnownNamingPolicy.SnakeCaseLower => JsonNamingPolicy.SnakeCaseLower, | ||
| JsonKnownNamingPolicy.SnakeCaseUpper => JsonNamingPolicy.SnakeCaseUpper, | ||
| JsonKnownNamingPolicy.KebabCaseLower => JsonNamingPolicy.KebabCaseLower, | ||
| JsonKnownNamingPolicy.KebabCaseUpper => JsonNamingPolicy.KebabCaseUpper, | ||
| _ => throw new ArgumentOutOfRangeException(nameof(namingPolicy)), | ||
| }; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.