< Summary

Information
Line coverage
72%
Covered lines: 21
Uncovered lines: 8
Coverable lines: 29
Total lines: 69
Line coverage: 72.4%
Branch coverage
33%
Covered branches: 2
Total branches: 6
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
CanConvert(...)100%11100%
CreateConverter(...)100%11100%
Create(...)50%2277.77%
IsSupportedTypeCode(...)50%22100%
Create(...)0%220%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Value\EnumConverterFactory.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.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Reflection;
 7
 8namespace System.Text.Json.Serialization.Converters
 9{
 10    [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)]
 11    internal sealed class EnumConverterFactory : JsonConverterFactory
 12    {
 113        public EnumConverterFactory()
 114        {
 115        }
 16
 17        public override bool CanConvert(Type type)
 418418        {
 418419            return type.IsEnum;
 418420        }
 21
 22        public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
 26923        {
 26924            Debug.Assert(CanConvert(type));
 26925            return Create(type, EnumConverterOptions.AllowNumbers, namingPolicy: null, options);
 26926        }
 27
 28        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2071:UnrecognizedReflectionPattern",
 29            Justification = "'EnumConverter<T> where T : struct' implies 'T : new()', so the trimmer is warning calling 
 30            "But EnumConverter doesn't call new T(), so this is safe.")]
 31        public static JsonConverter Create(Type enumType, EnumConverterOptions converterOptions, JsonNamingPolicy? namin
 53832        {
 53833            if (!Helpers.IsSupportedTypeCode(Type.GetTypeCode(enumType)))
 034            {
 35                // Char-backed enums are valid in IL and F# but are not supported by System.Text.Json.
 036                return UnsupportedTypeConverterFactory.CreateUnsupportedConverterForType(enumType);
 37            }
 38
 53839            Type converterType = typeof(EnumConverter<>).MakeGenericType(enumType);
 53840            return (JsonConverter)converterType.CreateInstanceNoWrapExceptions(
 53841                parameterTypes: [typeof(EnumConverterOptions), typeof(JsonNamingPolicy), typeof(JsonSerializerOptions)],
 53842                parameters: [converterOptions, namingPolicy, options])!;
 53843        }
 44
 45        // Some of the static methods are in a separate class so that the
 46        // RequiresDynamicCode annotation on EnumConverterFactory doesn't apply
 47        // to them.
 48        internal static class Helpers
 49        {
 50            public static bool IsSupportedTypeCode(TypeCode typeCode)
 107651            {
 107652                return typeCode is TypeCode.SByte or TypeCode.Int16 or TypeCode.Int32 or TypeCode.Int64
 107653                                or TypeCode.Byte or TypeCode.UInt16 or TypeCode.UInt32 or TypeCode.UInt64;
 107654            }
 55
 56            public static JsonConverter<T> Create<T>(EnumConverterOptions converterOptions, JsonSerializerOptions option
 57                where T : struct, Enum
 058            {
 059                if (!IsSupportedTypeCode(Type.GetTypeCode(typeof(T))))
 060                {
 61                    // Char-backed enums are valid in IL and F# but are not supported by System.Text.Json.
 062                    return new UnsupportedTypeConverter<T>();
 63                }
 64
 065                return new EnumConverter<T>(converterOptions, namingPolicy, options);
 066            }
 67        }
 68    }
 69}