JsonStringEnumConverter
Converts enumeration values to and from strings.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization.Converters;
namespace System.Text.Json.Serialization
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JsonStringEnumConverter cannot be statically analyzed and requires runtime code generation. Applications should use the generic JsonStringEnumConverter<TEnum> instead.")]
public class JsonStringEnumConverter : JsonConverterFactory
{
private readonly JsonNamingPolicy _namingPolicy;
private readonly EnumConverterOptions _converterOptions;
public JsonStringEnumConverter()
: this(null, true)
{
}
[System.Runtime.CompilerServices.NullableContext(2)]
public JsonStringEnumConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true)
{
_namingPolicy = namingPolicy;
_converterOptions = ((!allowIntegerValues) ? EnumConverterOptions.AllowStrings : (EnumConverterOptions.AllowStrings | EnumConverterOptions.AllowNumbers));
}
public sealed override bool CanConvert(Type typeToConvert)
{
return typeToConvert.IsEnum;
}
public sealed override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
if (!typeToConvert.IsEnum)
ThrowHelper.ThrowArgumentOutOfRangeException_JsonConverterFactory_TypeNotSupported(typeToConvert);
return EnumConverterFactory.Create(typeToConvert, _converterOptions, _namingPolicy, options);
}
}
}