Class JsonStringEnumFallbackConverter<TEnum>
- Namespace
- VpnHood.Net.Toolkit.Converters
- Assembly
- VpnHood.Net.Toolkit.dll
Converts an enum by its name, as JsonStringEnumConverter does, but reads a name this side does not know, one a newer app, engine or server added, as the enum's fallback instead of failing the whole document.
public class JsonStringEnumFallbackConverter<TEnum> : JsonConverter<TEnum> where TEnum : struct, Enum
Type Parameters
TEnumThe enum.
- Inheritance
-
JsonConverter<TEnum>JsonStringEnumFallbackConverter<TEnum>
- Derived
- Inherited Members
Remarks
The fallback is the enum's JsonEnumFallbackAttribute, or, for an enum of another library, a subclass's own. A name is matched ignoring case. A number is read as JsonStringEnumConverter reads it, and an undefined one falls back too, as does one beyond the enum's own type. Of an enum with FlagsAttribute, a combination of its values is read as it is, by its names or by its number, and falls back when it holds a name or a bit the enum does not have. A dictionary key is read the same way.
Constructors
JsonStringEnumFallbackConverter()
Initializes a new instance of the JsonStringEnumFallbackConverter<TEnum> class with the enum's JsonEnumFallbackAttribute.
public JsonStringEnumFallbackConverter()
Exceptions
- InvalidOperationException
The enum has no JsonEnumFallbackAttribute, or its value is not one of the enum's.
JsonStringEnumFallbackConverter(TEnum)
Initializes a new instance of the JsonStringEnumFallbackConverter<TEnum> class with a fallback, for an enum of another library, which a subclass names.
protected JsonStringEnumFallbackConverter(TEnum fallback)
Parameters
fallbackTEnumThe value read for a name or a number the enum does not have.
Exceptions
- ArgumentException
fallbackis not one of the enum's values.
Properties
Fallback
Gets the value read for a name or a number the enum does not have.
public TEnum Fallback { get; }
Property Value
- TEnum
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type T.
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- TEnum
The converted value.
Exceptions
- JsonException
The value is neither a string nor a number.
ReadAsPropertyName(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads a dictionary key from a JSON property name.
public override TEnum ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe Utf8JsonReader to read from.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsThe options to use when reading the value.
Returns
- TEnum
The value that was converted.
Write(Utf8JsonWriter, TEnum, JsonSerializerOptions)
Writes a specified value as JSON.
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valueTEnumThe value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
WriteAsPropertyName(Utf8JsonWriter, TEnum, JsonSerializerOptions)
Writes a dictionary key as a JSON property name.
public override void WriteAsPropertyName(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe Utf8JsonWriter to write to.
valueTEnumThe value to convert. The value of HandleNull determines if the converter handles null values.
optionsJsonSerializerOptionsThe options to use when writing the value.