Class ExtensibleEnumConverter<T>
- Namespace
- Cloudflare.NET.Core.Json
- Assembly
- Cloudflare.NET.dll
A JSON converter for extensible enum types that implement IExtensibleEnum<TSelf>.
This converter serializes extensible enums as their underlying string value and deserializes JSON strings back into the appropriate extensible enum type.
public class ExtensibleEnumConverter<T> : JsonConverter<T> where T : struct, IExtensibleEnum<T>
Type Parameters
TThe extensible enum type to convert.
- Inheritance
-
ExtensibleEnumConverter<T>
- Inherited Members
Examples
// Direct attribute application
[JsonConverter(typeof(ExtensibleEnumConverter<R2LocationHint>))]
public R2LocationHint Location { get; set; }
Remarks
When deserializing, null JSON values result in default(T).
For nullable properties, consider using Nullable<T> with this converter.
This converter supports both direct application via JsonConverterAttribute and registration through Converters.
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type T.
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- T
The converted value.
Write(Utf8JsonWriter, T, JsonSerializerOptions)
Writes a specified value as JSON.
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valueTThe value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.