Table of Contents

Class EnumDescriptionJsonConverter<T>

Namespace
Vonage.Common.Serialization
Assembly
Vonage.dll

Represents a custom converter from Enum description to Json.

public class EnumDescriptionJsonConverter<T> : JsonConverter<T> where T : struct, Enum

Type Parameters

T

Type of the enum.

Inheritance
EnumDescriptionJsonConverter<T>
Inherited Members

Examples

// Given an enum with Description attributes:
// public enum Status
// {
//     [Description("pending")]
//     Pending,
//     [Description("completed")]
//     Completed
// }

var options = new JsonSerializerOptions();
options.Converters.Add(new EnumDescriptionJsonConverter<Status>());

string json = JsonSerializer.Serialize(Status.Completed, options);
// Produces: "completed" (uses the Description attribute value)

Properties

HandleNull

Gets a value that indicates whether null should be passed to the converter on serialization, and whether Null should be passed on deserialization.

public override bool HandleNull { get; }

Property Value

bool

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

reader Utf8JsonReader

The reader.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

An 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

writer Utf8JsonWriter

The writer to write to.

value T

The value to convert to JSON.

options JsonSerializerOptions

An object that specifies serialization options to use.