< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 70
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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(...)100%110%
Create(...)0%220%
IsSupportedTypeCode(...)0%220%
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    {
 013        public EnumConverterFactory()
 014        {
 015        }
 16
 17        public override bool CanConvert(Type type)
 018        {
 019            return type.IsEnum;
 020        }
 21
 22        public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
 023        {
 024            Debug.Assert(CanConvert(type));
 025            return Create(type, EnumConverterOptions.AllowNumbers, namingPolicy: null, options);
 026        }
 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
 032        {
 033            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
 039            Type converterType = typeof(EnumConverter<>).MakeGenericType(enumType);
 040            return (JsonConverter)converterType.CreateInstanceNoWrapExceptions(
 041                parameterTypes: [typeof(EnumConverterOptions), typeof(JsonNamingPolicy), typeof(JsonSerializerOptions)],
 042                parameters: [converterOptions, namingPolicy, options])!;
 043        }
 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)
 051            {
 052                return typeCode is TypeCode.SByte or TypeCode.Int16 or TypeCode.Int32 or TypeCode.Int64
 053                                or TypeCode.Byte or TypeCode.UInt16 or TypeCode.UInt32 or TypeCode.UInt64;
 054            }
 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