< Summary

Information
Class: System.Text.Json.Serialization.JsonStringEnumConverter
Assembly: System.Text.Json
File(s): C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonStringEnumConverter.cs
Line coverage
89%
Covered lines: 17
Uncovered lines: 2
Coverable lines: 19
Total lines: 122
Line coverage: 89.4%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)50%22100%
CanConvert(...)100%11100%
CreateConverter(...)50%2266.66%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonStringEnumConverter.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.CodeAnalysis;
 5using System.Text.Json.Serialization.Converters;
 6
 7namespace System.Text.Json.Serialization
 8{
 9    /// <summary>
 10    /// Converter to convert enums to and from strings.
 11    /// </summary>
 12    /// <remarks>
 13    /// Reading is case insensitive, writing can be customized via a <see cref="JsonNamingPolicy" />.
 14    /// </remarks>
 15    /// <typeparam name="TEnum">The enum type that this converter targets.</typeparam>
 16    public class JsonStringEnumConverter<TEnum> : JsonConverterFactory
 17        where TEnum : struct, Enum
 18    {
 19        private readonly JsonNamingPolicy? _namingPolicy;
 20        private readonly EnumConverterOptions _converterOptions;
 21
 22        /// <summary>
 23        /// Constructor. Creates the <see cref="JsonStringEnumConverter"/> with the
 24        /// default naming policy and allows integer values.
 25        /// </summary>
 26        public JsonStringEnumConverter() : this(namingPolicy: null, allowIntegerValues: true)
 27        {
 28            // An empty constructor is needed for construction via attributes
 29        }
 30
 31        /// <summary>
 32        /// Constructor.
 33        /// </summary>
 34        /// <param name="namingPolicy">
 35        /// Optional naming policy for writing enum values.
 36        /// </param>
 37        /// <param name="allowIntegerValues">
 38        /// True to allow undefined enum values. When true, if an enum value isn't
 39        /// defined it will output as a number rather than a string.
 40        /// </param>
 41        public JsonStringEnumConverter(JsonNamingPolicy? namingPolicy = null, bool allowIntegerValues = true)
 42        {
 43            _namingPolicy = namingPolicy;
 44            _converterOptions = allowIntegerValues
 45                ? EnumConverterOptions.AllowNumbers | EnumConverterOptions.AllowStrings
 46                : EnumConverterOptions.AllowStrings;
 47        }
 48
 49        /// <inheritdoc />
 50        public sealed override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TEnum);
 51
 52        /// <inheritdoc />
 53        public sealed override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
 54        {
 55            if (typeToConvert != typeof(TEnum))
 56            {
 57                ThrowHelper.ThrowArgumentOutOfRangeException_JsonConverterFactory_TypeNotSupported(typeToConvert);
 58            }
 59
 60            return EnumConverterFactory.Helpers.Create<TEnum>(_converterOptions, options, _namingPolicy);
 61        }
 62    }
 63
 64    /// <summary>
 65    /// Converter to convert enums to and from strings.
 66    /// </summary>
 67    /// <remarks>
 68    /// Reading is case insensitive, writing can be customized via a <see cref="JsonNamingPolicy" />.
 69    /// </remarks>
 70    [RequiresDynamicCode(
 71        "JsonStringEnumConverter cannot be statically analyzed and requires runtime code generation. " +
 72        "Applications should use the generic JsonStringEnumConverter<TEnum> instead.")]
 73    public class JsonStringEnumConverter : JsonConverterFactory
 74    {
 75        private readonly JsonNamingPolicy? _namingPolicy;
 76        private readonly EnumConverterOptions _converterOptions;
 77
 78        /// <summary>
 79        /// Constructor. Creates the <see cref="JsonStringEnumConverter"/> with the
 80        /// default naming policy and allows integer values.
 81        /// </summary>
 26982        public JsonStringEnumConverter() : this(namingPolicy: null, allowIntegerValues: true)
 26983        {
 84            // An empty constructor is needed for construction via attributes
 26985        }
 86
 87        /// <summary>
 88        /// Constructor.
 89        /// </summary>
 90        /// <param name="namingPolicy">
 91        /// Optional naming policy for writing enum values.
 92        /// </param>
 93        /// <param name="allowIntegerValues">
 94        /// True to allow undefined enum values. When true, if an enum value isn't
 95        /// defined it will output as a number rather than a string.
 96        /// </param>
 26997        public JsonStringEnumConverter(JsonNamingPolicy? namingPolicy = null, bool allowIntegerValues = true)
 26998        {
 26999            _namingPolicy = namingPolicy;
 269100            _converterOptions = allowIntegerValues
 269101                ? EnumConverterOptions.AllowNumbers | EnumConverterOptions.AllowStrings
 269102                : EnumConverterOptions.AllowStrings;
 269103        }
 104
 105        /// <inheritdoc />
 106        public sealed override bool CanConvert(Type typeToConvert)
 538107        {
 538108            return typeToConvert.IsEnum;
 538109        }
 110
 111        /// <inheritdoc />
 112        public sealed override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
 269113        {
 269114            if (!typeToConvert.IsEnum)
 0115            {
 0116                ThrowHelper.ThrowArgumentOutOfRangeException_JsonConverterFactory_TypeNotSupported(typeToConvert);
 117            }
 118
 269119            return EnumConverterFactory.Create(typeToConvert, _converterOptions, _namingPolicy, options);
 269120        }
 121    }
 122}