< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 8
Coverable lines: 8
Total lines: 40
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
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%
CanConvert(...)100%110%
CreateConverter(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonNumberEnumConverter.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
 4using System.Text.Json.Serialization.Converters;
 5
 6namespace System.Text.Json.Serialization
 7{
 8    /// <summary>
 9    /// Converter to convert enums to and from numeric values.
 10    /// </summary>
 11    /// <typeparam name="TEnum">The enum type that this converter targets.</typeparam>
 12    /// <remarks>
 13    /// This is the default converter for enums and can be used to override
 14    /// <see cref="JsonSourceGenerationOptionsAttribute.UseStringEnumConverter"/>
 15    /// on individual types or properties.
 16    /// </remarks>
 17    public sealed class JsonNumberEnumConverter<TEnum> : JsonConverterFactory
 18        where TEnum : struct, Enum
 19    {
 20        /// <summary>
 21        /// Initializes a new instance of <see cref="JsonNumberEnumConverter{TEnum}"/>.
 22        /// </summary>
 023        public JsonNumberEnumConverter() { }
 24
 25        /// <inheritdoc />
 026        public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TEnum);
 27
 28        /// <inheritdoc />
 29        public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
 030        {
 031            if (typeToConvert != typeof(TEnum))
 032            {
 033                ThrowHelper.ThrowArgumentOutOfRangeException_JsonConverterFactory_TypeNotSupported(typeToConvert);
 34            }
 35
 036            return EnumConverterFactory.Helpers.Create<TEnum>(EnumConverterOptions.AllowNumbers, options);
 037        }
 38    }
 39}
 40