< Summary

Line coverage
72%
Covered lines: 21
Uncovered lines: 8
Coverable lines: 29
Total lines: 70
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)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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)
 156718        {
 156719            return type.IsEnum;
 156720        }
 21
 22        public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
 8623        {
 8624            Debug.Assert(CanConvert(type));
 8625            return Create(type, EnumConverterOptions.AllowNumbers, namingPolicy: null, options);
 8626        }
 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
 17232        {
 17233            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
 17239            Type converterType = typeof(EnumConverter<>).MakeGenericType(enumType);
 17240            return (JsonConverter)converterType.CreateInstanceNoWrapExceptions(
 17241                parameterTypes: [typeof(EnumConverterOptions), typeof(JsonNamingPolicy), typeof(JsonSerializerOptions)],
 17242                parameters: [converterOptions, namingPolicy, options])!;
 17243        }
 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)
 34451            {
 34452                return typeCode is TypeCode.SByte or TypeCode.Int16 or TypeCode.Int32 or TypeCode.Int64
 34453                                or TypeCode.Byte or TypeCode.UInt16 or TypeCode.UInt32 or TypeCode.UInt64;
 34454            }
 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}
 70