| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | namespace System.Text.Json.Serialization |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// When placed on a type, property, or field, indicates what <see cref="JsonNamingPolicy"/> |
| | | 8 | | /// should be used to convert property names. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// When placed on a property or field, the naming policy specified by this attribute |
| | | 12 | | /// takes precedence over the type-level attribute and the <see cref="JsonSerializerOptions.PropertyNamingPolicy"/>. |
| | | 13 | | /// When placed on a type, the naming policy specified by this attribute takes precedence |
| | | 14 | | /// over the <see cref="JsonSerializerOptions.PropertyNamingPolicy"/>. |
| | | 15 | | /// The <see cref="JsonPropertyNameAttribute"/> takes precedence over this attribute. |
| | | 16 | | /// </remarks> |
| | | 17 | | [AttributeUsage( |
| | | 18 | | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | |
| | | 19 | | AttributeTargets.Property | AttributeTargets.Field, |
| | | 20 | | AllowMultiple = false)] |
| | | 21 | | public class JsonNamingPolicyAttribute : JsonAttribute |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of <see cref="JsonNamingPolicyAttribute"/> |
| | | 25 | | /// with the specified known naming policy. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="namingPolicy">The known naming policy to use for name conversion.</param> |
| | | 28 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 29 | | /// The specified <paramref name="namingPolicy"/> is not a valid known naming policy value. |
| | | 30 | | /// </exception> |
| | 0 | 31 | | public JsonNamingPolicyAttribute(JsonKnownNamingPolicy namingPolicy) |
| | 0 | 32 | | { |
| | 0 | 33 | | NamingPolicy = ResolveNamingPolicy(namingPolicy); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of <see cref="JsonNamingPolicyAttribute"/> |
| | | 38 | | /// with a custom naming policy. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="namingPolicy">The naming policy to use for name conversion.</param> |
| | | 41 | | /// <exception cref="ArgumentNullException"> |
| | | 42 | | /// <paramref name="namingPolicy"/> is <see langword="null"/>. |
| | | 43 | | /// </exception> |
| | 0 | 44 | | protected JsonNamingPolicyAttribute(JsonNamingPolicy namingPolicy) |
| | 0 | 45 | | { |
| | 0 | 46 | | if (namingPolicy is null) |
| | 0 | 47 | | { |
| | 0 | 48 | | ThrowHelper.ThrowArgumentNullException(nameof(namingPolicy)); |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | NamingPolicy = namingPolicy; |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the naming policy to use for name conversion. |
| | | 56 | | /// </summary> |
| | 0 | 57 | | public JsonNamingPolicy NamingPolicy { get; } |
| | | 58 | | |
| | | 59 | | internal static JsonNamingPolicy ResolveNamingPolicy(JsonKnownNamingPolicy namingPolicy) |
| | 0 | 60 | | { |
| | 0 | 61 | | return namingPolicy switch |
| | 0 | 62 | | { |
| | 0 | 63 | | JsonKnownNamingPolicy.CamelCase => JsonNamingPolicy.CamelCase, |
| | 0 | 64 | | JsonKnownNamingPolicy.SnakeCaseLower => JsonNamingPolicy.SnakeCaseLower, |
| | 0 | 65 | | JsonKnownNamingPolicy.SnakeCaseUpper => JsonNamingPolicy.SnakeCaseUpper, |
| | 0 | 66 | | JsonKnownNamingPolicy.KebabCaseLower => JsonNamingPolicy.KebabCaseLower, |
| | 0 | 67 | | JsonKnownNamingPolicy.KebabCaseUpper => JsonNamingPolicy.KebabCaseUpper, |
| | 0 | 68 | | JsonKnownNamingPolicy.PascalCase => JsonNamingPolicy.PascalCase, |
| | 0 | 69 | | _ => throw new ArgumentOutOfRangeException(nameof(namingPolicy)), |
| | 0 | 70 | | }; |
| | 0 | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |