< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 74
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 9
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)0%220%
ResolveNamingPolicy(...)0%770%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonNamingPolicyAttribute.cs

#LineLine coverage
 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
 4namespace 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>
 031        public JsonNamingPolicyAttribute(JsonKnownNamingPolicy namingPolicy)
 032        {
 033            NamingPolicy = ResolveNamingPolicy(namingPolicy);
 034        }
 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>
 044        protected JsonNamingPolicyAttribute(JsonNamingPolicy namingPolicy)
 045        {
 046            if (namingPolicy is null)
 047            {
 048                ThrowHelper.ThrowArgumentNullException(nameof(namingPolicy));
 49            }
 50
 051            NamingPolicy = namingPolicy;
 052        }
 53
 54        /// <summary>
 55        /// Gets the naming policy to use for name conversion.
 56        /// </summary>
 057        public JsonNamingPolicy NamingPolicy { get; }
 58
 59        internal static JsonNamingPolicy ResolveNamingPolicy(JsonKnownNamingPolicy namingPolicy)
 060        {
 061            return namingPolicy switch
 062            {
 063                JsonKnownNamingPolicy.CamelCase => JsonNamingPolicy.CamelCase,
 064                JsonKnownNamingPolicy.SnakeCaseLower => JsonNamingPolicy.SnakeCaseLower,
 065                JsonKnownNamingPolicy.SnakeCaseUpper => JsonNamingPolicy.SnakeCaseUpper,
 066                JsonKnownNamingPolicy.KebabCaseLower => JsonNamingPolicy.KebabCaseLower,
 067                JsonKnownNamingPolicy.KebabCaseUpper => JsonNamingPolicy.KebabCaseUpper,
 068                JsonKnownNamingPolicy.PascalCase => JsonNamingPolicy.PascalCase,
 069                _ => throw new ArgumentOutOfRangeException(nameof(namingPolicy)),
 070            };
 071        }
 72    }
 73}
 74