Table of Contents

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

TEnum

The enum.

Inheritance
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

fallback TEnum

The value read for a name or a number the enum does not have.

Exceptions

ArgumentException

fallback is 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

reader Utf8JsonReader

The reader.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

An 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

reader Utf8JsonReader

The Utf8JsonReader to read from.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

The 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

writer Utf8JsonWriter

The writer to write to.

value TEnum

The value to convert to JSON.

options JsonSerializerOptions

An 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

writer Utf8JsonWriter

The Utf8JsonWriter to write to.

value TEnum

The value to convert. The value of HandleNull determines if the converter handles null values.

options JsonSerializerOptions

The options to use when writing the value.