YamlSerializerOptions Class

Configures the behavior of YamlSerializer operations.

public sealed class YamlSerializerOptions : IEquatable<YamlSerializerOptions>
Implements

Constructors

NameDescription
YamlSerializerOptions()

YamlSerializerOptions()

YamlSerializerOptions.cs
public YamlSerializerOptions()

No documentation is available.

Properties

NameDescription
AllowAliasesGets or sets a value indicating whether YAML aliases (*name) are allowed during deserialization.
AllowAnchorsGets or sets a value indicating whether YAML anchors (&name) are allowed during deserialization.
BlockSequenceMappingStyleGets or sets how mappings are emitted when they appear as items in block sequences.
BlockSequenceSequenceStyleGets or sets how nested sequences are emitted when they appear as items in block sequences.
ConvertersGets the custom converters.
DefaultGets a default options instance.
DefaultIgnoreConditionGets or sets the default ignore condition for null/default values.
DictionaryKeyPolicyGets or sets the policy used to convert dictionary keys during serialization.
DuplicateKeyHandlingGets or sets behavior when duplicate mapping keys are encountered while reading.
IgnoreReadOnlyFieldsGets or sets a value indicating whether read-only fields are ignored during serialization.
IgnoreReadOnlyPropertiesGets or sets a value indicating whether read-only properties are ignored during serialization.
IncludeFieldsGets or sets a value indicating whether public fields are included during serialization and deserialization.
IndentBlockSequencesGets or sets a value indicating whether a block sequence that is the value of a mapping key is indented relative to that mapping.
IndentSizeGets or sets the number of spaces to use when YamlSerializerOptions.WriteIndented is enabled.
MappingOrderGets or sets member ordering behavior for emitted mappings.
MaxAliasExpansionNodeCountGets or sets the maximum number of nodes that aliases are allowed to materialize while building a YamlNode document object model.
MaxDepthGets or sets the maximum allowed nesting depth for YAML mappings and sequences during serialization and deserialization.
PolymorphismOptionsGets polymorphism options.
PreferredObjectCreationHandlingGets or sets the preferred object creation handling for properties and fields during deserialization.
PropertyNameCaseInsensitiveGets or sets a value indicating whether property name matching is case-insensitive.
PropertyNamingPolicyGets or sets the policy used to convert CLR property names.
ReferenceHandlingGets or sets object reference handling behavior.
RespectNullableAnnotationsGets or sets a value indicating whether nullable annotations are enforced during serialization and deserialization.
RespectRequiredConstructorParametersGets or sets a value indicating whether required constructor parameters must be present during deserialization.
ScalarStylePreferencesGets scalar style preferences for serialization.
SchemaGets or sets the schema used for scalar resolution.
SourceNameGets or sets an optional name for the YAML source.
TypeClassifiersGets the type classifier factories used to select a C# union case from a YAML payload.
TypeInfoResolverGets or sets a metadata resolver used to retrieve YamlTypeInfo instances.
UnmappedMemberHandlingGets or sets how unmapped YAML members are handled during object deserialization.
UnsafeAllowDeserializeFromTagTypeNameGets or sets a value indicating whether unregistered runtime type names from YAML tags are allowed during deserialization.
UseSchemaGets or sets a value indicating whether scalar deserialization should resolve through YamlSerializerOptions.Schema.
WriteIndentedGets or sets a value indicating whether output should be indented.

Gets or sets a value indicating whether YAML aliases (*name) are allowed during deserialization.

public bool AllowAliases { get; init; }

Property value

bool

Remarks

When false, encountering an alias throws a YamlException.

Gets or sets a value indicating whether YAML anchors (&name) are allowed during deserialization.

public bool AllowAnchors { get; init; }

Property value

bool

Remarks

When false, encountering a node that declares an anchor throws a YamlException.

BlockSequenceMappingStyle

YamlSerializerOptions.cs

Gets or sets how mappings are emitted when they appear as items in block sequences.

public YamlSequenceItemStyle BlockSequenceMappingStyle { get; init; }

Property value

YamlSequenceItemStyle

Exceptions

ArgumentOutOfRangeException
Value is not a defined YamlSequenceItemStyle.

Remarks

The default is YamlSequenceItemStyle.Compact, which emits the first mapping key on the same line as the sequence dash when possible. This option is ignored when YamlSerializerOptions.WriteIndented is disabled, because the flow style has no sequence dash.

BlockSequenceSequenceStyle

YamlSerializerOptions.cs

Gets or sets how nested sequences are emitted when they appear as items in block sequences.

public YamlSequenceItemStyle BlockSequenceSequenceStyle { get; init; }

Property value

YamlSequenceItemStyle

Exceptions

ArgumentOutOfRangeException
Value is not a defined YamlSequenceItemStyle.

Remarks

The default is YamlSequenceItemStyle.Expanded, which keeps nested sequence items on lines following the parent sequence dash. This option is ignored when YamlSerializerOptions.WriteIndented is disabled, because the flow style has no sequence dash.

Gets the custom converters.

public IReadOnlyList<YamlConverter> Converters { get; init; }

Property value

IReadOnlyList<YamlConverter>

Exceptions

ArgumentNullException
Value is null.
ArgumentException
A converter entry is null.

Remarks

Converters are evaluated in order and take precedence over built-in converters.

Gets a default options instance.

public static YamlSerializerOptions Default { get; }

Property value

YamlSerializerOptions

DefaultIgnoreCondition

YamlSerializerOptions.cs

Gets or sets the default ignore condition for null/default values.

public YamlIgnoreCondition DefaultIgnoreCondition { get; init; }

Property value

YamlIgnoreCondition

DictionaryKeyPolicy

YamlSerializerOptions.cs

Gets or sets the policy used to convert dictionary keys during serialization.

public YamlNamingPolicy? DictionaryKeyPolicy { get; init; }

Property value

YamlNamingPolicy?

DuplicateKeyHandling

YamlSerializerOptions.cs

Gets or sets behavior when duplicate mapping keys are encountered while reading.

public YamlDuplicateKeyHandling DuplicateKeyHandling { get; init; }

IgnoreReadOnlyFields

YamlSerializerOptions.cs

Gets or sets a value indicating whether read-only fields are ignored during serialization.

public bool IgnoreReadOnlyFields { get; init; }

Property value

bool

IgnoreReadOnlyProperties

YamlSerializerOptions.cs

Gets or sets a value indicating whether read-only properties are ignored during serialization.

public bool IgnoreReadOnlyProperties { get; init; }

Property value

bool

Gets or sets a value indicating whether public fields are included during serialization and deserialization.

public bool IncludeFields { get; init; }

Property value

bool

IndentBlockSequences

YamlSerializerOptions.cs

Gets or sets a value indicating whether a block sequence that is the value of a mapping key is indented relative to that mapping.

public bool IndentBlockSequences { get; init; }

Property value

bool

Remarks

A block sequence used as a mapping value does not need its own indentation level: YAML allows the sequence dashes to sit at the indentation of the parent mapping. When false, the dashes are written at the indentation of the parent mapping instead of being indented by YamlSerializerOptions.IndentSize columns.

This option only affects sequences written as a mapping value. A sequence nested inside another sequence is always indented past its parent dash, because YAML has no unindented form for it. This option is ignored when YamlSerializerOptions.WriteIndented is disabled, because the flow style has no sequence dash.

Gets or sets the number of spaces to use when YamlSerializerOptions.WriteIndented is enabled.

public int IndentSize { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
Value is less than 1.

Gets or sets member ordering behavior for emitted mappings.

public YamlMappingOrderPolicy MappingOrder { get; init; }

MaxAliasExpansionNodeCount

YamlSerializerOptions.cs

Gets or sets the maximum number of nodes that aliases are allowed to materialize while building a YamlNode document object model.

public int MaxAliasExpansionNodeCount { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
Value is less than 0.

Remarks

A value of 0 uses the default limit of 100000.

The document object model does not preserve aliases as a distinct node type, so each alias is expanded into a copy of the anchored subtree. Nesting anchors so that each level references the previous one several times makes the node count grow exponentially while the document grows linearly, which lets a very small payload exhaust memory. Only nodes produced by alias expansion count towards this limit; documents without aliases are never affected, and YamlSerializerOptions.MaxDepth does not help because the growth is in breadth rather than depth.

The serializer applies the same limit to the nodes a merge key replays when its value is an alias (<<: *defaults), as a merged mapping can itself merge other mappings.

Gets or sets the maximum allowed nesting depth for YAML mappings and sequences during serialization and deserialization.

public int MaxDepth { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
Value is less than 0.

Remarks

A value of 0 uses the default limit of 64.

PolymorphismOptions

YamlSerializerOptions.cs

Gets polymorphism options.

public YamlPolymorphismOptions PolymorphismOptions { get; init; }

Property value

YamlPolymorphismOptions

Exceptions

ArgumentNullException
Value is null.
ArgumentException
A derived type mapping has a null list or a null entry.

Remarks

The assigned instance becomes read-only: its YamlPolymorphismOptions.DerivedTypeMappings are copied, and changing them afterwards throws NotSupportedException. This keeps options that are already in use, and the copies made from them with a with expression, from changing.

PreferredObjectCreationHandling

YamlSerializerOptions.cs

Gets or sets the preferred object creation handling for properties and fields during deserialization.

public YamlObjectCreationHandling PreferredObjectCreationHandling { get; init; }

Property value

YamlObjectCreationHandling

Remarks

A member-level or type-level YamlObjectCreationHandlingAttribute overrides this setting. The default behavior is YamlObjectCreationHandling.Replace.

PropertyNameCaseInsensitive

YamlSerializerOptions.cs

Gets or sets a value indicating whether property name matching is case-insensitive.

public bool PropertyNameCaseInsensitive { get; init; }

Property value

bool

PropertyNamingPolicy

YamlSerializerOptions.cs

Gets or sets the policy used to convert CLR property names.

public YamlNamingPolicy? PropertyNamingPolicy { get; init; }

Property value

YamlNamingPolicy?

ReferenceHandling

YamlSerializerOptions.cs

Gets or sets object reference handling behavior.

public YamlReferenceHandling ReferenceHandling { get; init; }

Property value

YamlReferenceHandling

RespectNullableAnnotations

YamlSerializerOptions.cs

Gets or sets a value indicating whether nullable annotations are enforced during serialization and deserialization.

public bool RespectNullableAnnotations { get; init; }

Property value

bool

RespectRequiredConstructorParameters

YamlSerializerOptions.cs

Gets or sets a value indicating whether required constructor parameters must be present during deserialization.

public bool RespectRequiredConstructorParameters { get; init; }

Property value

bool

ScalarStylePreferences

YamlSerializerOptions.cs

Gets scalar style preferences for serialization.

public YamlScalarStylePreferences ScalarStylePreferences { get; init; }

Property value

YamlScalarStylePreferences

Exceptions

ArgumentNullException
Value is null.

Gets or sets the schema used for scalar resolution.

public YamlSchemaKind Schema { get; init; }

Property value

YamlSchemaKind

Gets or sets an optional name for the YAML source.

public string? SourceName { get; init; }

Property value

string?

Remarks

This value is used to annotate YamlException messages with a source name (for example, a file path) when reporting parse errors.

Gets the type classifier factories used to select a C# union case from a YAML payload.

public IReadOnlyList<YamlTypeClassifierFactory> TypeClassifiers { get; init; }

Property value

IReadOnlyList<YamlTypeClassifierFactory>

Exceptions

ArgumentNullException
Value is null.
ArgumentException
A factory entry is null.

Remarks

Factories are evaluated in order, and the first one accepting a type classifies it. Without a factory, union cases are selected by YAML shape alone and a payload matching several cases fails to deserialize. Register YamlUnionTypeStructuralClassifier to also tell mapping cases apart by their keys.

TypeInfoResolver

YamlSerializerOptions.cs

Gets or sets a metadata resolver used to retrieve YamlTypeInfo instances.

public IYamlTypeInfoResolver? TypeInfoResolver { get; init; }

Property value

IYamlTypeInfoResolver?

Remarks

When this is a source-generated YamlSerializerContext and this options instance differs from the context's own options, generated metadata may be initialized for this options instance. Cache and reuse the resulting YamlTypeInfo for repeated operations with the same runtime converter set.

UnmappedMemberHandling

YamlSerializerOptions.cs

Gets or sets how unmapped YAML members are handled during object deserialization.

public YamlUnmappedMemberHandling UnmappedMemberHandling { get; init; }

Property value

YamlUnmappedMemberHandling

Remarks

Extension data members still capture unmatched properties when present.

UnsafeAllowDeserializeFromTagTypeName

YamlSerializerOptions.cs

Gets or sets a value indicating whether unregistered runtime type names from YAML tags are allowed during deserialization.

public bool UnsafeAllowDeserializeFromTagTypeName { get; init; }

Property value

bool

Remarks

Enabling this option allows tag-based type name activation and should only be used with trusted YAML input.

Gets or sets a value indicating whether scalar deserialization should resolve through YamlSerializerOptions.Schema.

public bool UseSchema { get; init; }

Property value

bool

Remarks

When false, built-in converters use a fast span-based path for common YAML 1.2 scalars while still honoring quoted scalars as strings. When true, scalar resolution goes through the selected YamlSerializerOptions.Schema.

Gets or sets a value indicating whether output should be indented.

public bool WriteIndented { get; init; }

Property value

bool

Remarks

When true, collections are written using the block style and nested collections are indented by YamlSerializerOptions.IndentSize columns.

When false, collections are written using the flow style, which keeps the whole document on a single line: {a: {b: 1}, c: [2, 3]}. Block YAML expresses nesting through indentation, so the flow style is what makes unindented output possible. YamlSerializerOptions.IndentSize, YamlSerializerOptions.BlockSequenceMappingStyle, and YamlSerializerOptions.BlockSequenceSequenceStyle only apply to the block style and are ignored.

Methods

NameDescription
Equals(YamlSerializerOptions?)
Equals(object?)
GetHashCode()
GetTypeInfo<T>()Gets the YamlTypeInfo<T> contract metadata resolved by this options instance.
ToString()
TryGetTypeInfo<T>(YamlTypeInfo<T>?)Attempts to get the YamlTypeInfo<T> contract metadata resolved by this options instance.

Equals overloads

Equals(YamlSerializerOptions?)

public bool Equals(YamlSerializerOptions? other)
Parameters
other YamlSerializerOptions?
Returns

bool

No documentation is available.

Equals(object?)

public override bool Equals(object? obj)
Parameters
obj object?
Returns

bool

No documentation is available.

GetHashCode()

public override int GetHashCode()

Returns

int

No documentation is available.

GetTypeInfo<T>()

YamlSerializerOptions.cs

Gets the YamlTypeInfo<T> contract metadata resolved by this options instance.

public YamlTypeInfo<T> GetTypeInfo<T>()

Type parameters

T
The type to resolve metadata for.

Returns

YamlTypeInfo<T> The metadata resolved for T.

Exceptions

InvalidOperationException
No metadata is available for T.

ToString()

public override string ToString()

Returns

string

No documentation is available.

TryGetTypeInfo<T>(YamlTypeInfo<T>?)

YamlSerializerOptions.cs

Attempts to get the YamlTypeInfo<T> contract metadata resolved by this options instance.

public bool TryGetTypeInfo<T>(out YamlTypeInfo<T>? typeInfo)

Type parameters

T
The type to resolve metadata for.

Parameters

typeInfo YamlTypeInfo<T>?
The metadata resolved for T, when available.

Returns

bool true when metadata is available for T; otherwise false.

Operators

NameDescription
operator ==(YamlSerializerOptions?, YamlSerializerOptions?)
operator !=(YamlSerializerOptions?, YamlSerializerOptions?)

operator ==(YamlSerializerOptions?, YamlSerializerOptions?)

public static bool operator ==(YamlSerializerOptions? left, YamlSerializerOptions? right)

No documentation is available.

operator !=(YamlSerializerOptions?, YamlSerializerOptions?)

public static bool operator !=(YamlSerializerOptions? left, YamlSerializerOptions? right)

No documentation is available.