YamlReaderWriterBase Class

Shared base class for YamlReader and YamlWriter.

public abstract class YamlReaderWriterBase

Remarks

This type centralizes option access and caches frequently computed values (naming policy conversions and converter resolution) to reduce repeated runtime work during serialization and deserialization.

Constructors

NameDescription
YamlReaderWriterBase(YamlSerializerOptions)Initializes a new instance of the YamlReaderWriterBase class.

YamlReaderWriterBase(YamlSerializerOptions)

YamlReaderWriterBase.cs

Initializes a new instance of the YamlReaderWriterBase class.

protected YamlReaderWriterBase(YamlSerializerOptions options)

Parameters

options YamlSerializerOptions
The options associated with this reader or writer.

Exceptions

ArgumentNullException
options is null.

Properties

NameDescription
OptionsGets the options associated with this reader or writer instance.

Gets the options associated with this reader or writer instance.

public YamlSerializerOptions Options { get; }

Property value

YamlSerializerOptions

Methods

NameDescription
ConvertDictionaryKey(string)Converts a dictionary key into a YAML key using YamlSerializerOptions.DictionaryKeyPolicy.
ConvertName(string)Converts a CLR member name into a YAML member name using YamlSerializerOptions.PropertyNamingPolicy.
GetConverter(Type)Gets a converter that can handle typeToConvert.
TryGetCustomConverter(Type, YamlConverter?)Attempts to resolve a custom converter for typeToConvert from YamlSerializerOptions.Converters.

ConvertDictionaryKey(string)

YamlReaderWriterBase.cs

Converts a dictionary key into a YAML key using YamlSerializerOptions.DictionaryKeyPolicy.

public string ConvertDictionaryKey(string key)

Parameters

key string
The dictionary key.

Returns

string The converted YAML key.

Exceptions

ArgumentNullException
key is null.

ConvertName(string)

YamlReaderWriterBase.cs

Converts a CLR member name into a YAML member name using YamlSerializerOptions.PropertyNamingPolicy.

public string ConvertName(string name)

Parameters

name string
The CLR member name.

Returns

string The converted YAML member name.

Exceptions

ArgumentNullException
name is null.

GetConverter(Type)

YamlReaderWriterBase.cs

Gets a converter that can handle typeToConvert.

public YamlConverter GetConverter(Type typeToConvert)

Parameters

typeToConvert Type
The CLR type to resolve.

Returns

YamlConverter The converter for typeToConvert.

Exceptions

ArgumentNullException
typeToConvert is null.
NotSupportedException
No converter can handle typeToConvert.

TryGetCustomConverter(Type, YamlConverter?)

YamlReaderWriterBase.cs

Attempts to resolve a custom converter for typeToConvert from YamlSerializerOptions.Converters.

public bool TryGetCustomConverter(Type typeToConvert, out YamlConverter? converter)

Parameters

typeToConvert Type
The CLR type to resolve.
converter YamlConverter?
When successful, receives the converter instance.

Returns

bool true when a custom converter was resolved; otherwise false.

Exceptions

ArgumentNullException
typeToConvert is null.
InvalidOperationException
A converter factory returned null or returned a converter that does not handle typeToConvert.