| | | 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 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Text.Json.Serialization.Converters; |
| | | 6 | | |
| | | 7 | | namespace 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> |
| | 0 | 26 | | public JsonStringEnumConverter() : this(namingPolicy: null, allowIntegerValues: true) |
| | 0 | 27 | | { |
| | | 28 | | // An empty constructor is needed for construction via attributes |
| | 0 | 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> |
| | 0 | 41 | | public JsonStringEnumConverter(JsonNamingPolicy? namingPolicy = null, bool allowIntegerValues = true) |
| | 0 | 42 | | { |
| | 0 | 43 | | _namingPolicy = namingPolicy; |
| | 0 | 44 | | _converterOptions = allowIntegerValues |
| | 0 | 45 | | ? EnumConverterOptions.AllowNumbers | EnumConverterOptions.AllowStrings |
| | 0 | 46 | | : EnumConverterOptions.AllowStrings; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | 0 | 50 | | public sealed override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TEnum); |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public sealed override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| | 0 | 54 | | { |
| | 0 | 55 | | if (typeToConvert != typeof(TEnum)) |
| | 0 | 56 | | { |
| | 0 | 57 | | ThrowHelper.ThrowArgumentOutOfRangeException_JsonConverterFactory_TypeNotSupported(typeToConvert); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | return EnumConverterFactory.Helpers.Create<TEnum>(_converterOptions, options, _namingPolicy); |
| | 0 | 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> |
| | | 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 | | } |