Table of Contents

YamlConverter<T> Class

Definition

Namespace
Bodu.Text.Yaml.Serialization
Assembly
Bodu.Text.Yaml.dll
Package
Bodu.Text.Yaml 1.0.0
Source
YamlConverter{T}.cs

Serves as the strongly-typed base for custom YAML converters that read and write values of type T. This is the base authors derive to customize how a specific type is serialized.

public abstract class YamlConverter<T> : YamlConverter

Type Parameters

T

The type handled by the converter.

Inheritance
YamlConverter<T>
Inherited Members
Extension Methods

Examples

public sealed class VersionConverter : YamlConverter<Version>
{
    public override Version Read(ref Utf8YamlReader reader, Type typeToConvert, YamlSerializerOptions options) =>
        Version.Parse(reader.GetString());

    public override void Write(Utf8YamlWriter writer, Version value, YamlSerializerOptions options) =>
        writer.WriteString(value.ToString());
}

Constructors

YamlConverter()

protected YamlConverter()

Methods

CanConvert(Type)

Determines whether the converter can convert the specified type.

public override bool CanConvert(Type typeToConvert)

Parameters

typeToConvert Type

The type to test.

Returns

bool

true when the type is T.

Read(ref Utf8YamlReader, Type, YamlSerializerOptions)

Reads and converts a value of type T from the reader.

public abstract T Read(ref Utf8YamlReader reader, Type typeToConvert, YamlSerializerOptions options)

Parameters

reader Utf8YamlReader

The reader positioned on the first token of the value.

typeToConvert Type

The requested type.

options YamlSerializerOptions

The serializer options in effect.

Returns

T

The converted value.

Remarks

On entry the reader is positioned on the value's first token. On return it must be positioned on the value's last token, so the caller can advance past the value with a single Read().

Write(Utf8YamlWriter, T, YamlSerializerOptions)

Writes a value of type T as YAML.

public abstract void Write(Utf8YamlWriter writer, T value, YamlSerializerOptions options)

Parameters

writer Utf8YamlWriter

The writer to emit into.

value T

The value to write.

options YamlSerializerOptions

The serializer options in effect.

Applies to

ProductVersions
.NET8, 10