< Summary

Information
Class: System.Text.Json.Serialization.JsonStringEnumConverter<T>
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
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 122
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%
.ctor(...)0%220%
CanConvert(...)100%110%
CreateConverter(...)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\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>
 026        public JsonStringEnumConverter() : this(namingPolicy: null, allowIntegerValues: true)
 027        {
 28            // An empty constructor is needed for construction via attributes
 029        }
 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>
 041        public JsonStringEnumConverter(JsonNamingPolicy? namingPolicy = null, bool allowIntegerValues = true)
 042        {
 043            _namingPolicy = namingPolicy;
 044            _converterOptions = allowIntegerValues
 045                ? EnumConverterOptions.AllowNumbers | EnumConverterOptions.AllowStrings
 046                : EnumConverterOptions.AllowStrings;
 047        }
 48
 49        /// <inheritdoc />
 050        public sealed override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TEnum);
 51
 52        /// <inheritdoc />
 53        public sealed override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
 054        {
 055            if (typeToConvert != typeof(TEnum))
 056            {
 057                ThrowHelper.ThrowArgumentOutOfRangeException_JsonConverterFactory_TypeNotSupported(typeToConvert);
 58            }
 59
 060            return EnumConverterFactory.Helpers.Create<TEnum>(_converterOptions, options, _namingPolicy);
 061        }
 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>
 82        public JsonStringEnumConverter() : this(namingPolicy: null, allowIntegerValues: true)
 83        {
 84            // An empty constructor is needed for construction via attributes
 85        }
 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>
 97        public JsonStringEnumConverter(JsonNamingPolicy? namingPolicy = null, bool allowIntegerValues = true)
 98        {
 99            _namingPolicy = namingPolicy;
 100            _converterOptions = allowIntegerValues
 101                ? EnumConverterOptions.AllowNumbers | EnumConverterOptions.AllowStrings
 102                : EnumConverterOptions.AllowStrings;
 103        }
 104
 105        /// <inheritdoc />
 106        public sealed override bool CanConvert(Type typeToConvert)
 107        {
 108            return typeToConvert.IsEnum;
 109        }
 110
 111        /// <inheritdoc />
 112        public sealed override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
 113        {
 114            if (!typeToConvert.IsEnum)
 115            {
 116                ThrowHelper.ThrowArgumentOutOfRangeException_JsonConverterFactory_TypeNotSupported(typeToConvert);
 117            }
 118
 119            return EnumConverterFactory.Create(typeToConvert, _converterOptions, _namingPolicy, options);
 120        }
 121    }
 122}