YamlSerializerOptions Class
Configures the behavior of YamlSerializer operations.
public sealed class YamlSerializerOptions : IEquatable<YamlSerializerOptions>- Implements
Constructors
| Name | Description |
|---|---|
| YamlSerializerOptions() |
YamlSerializerOptions()
YamlSerializerOptions.cspublic YamlSerializerOptions()No documentation is available.
Properties
| Name | Description |
|---|---|
| AllowAliases | Gets or sets a value indicating whether YAML aliases (*name) are allowed during deserialization. |
| AllowAnchors | Gets or sets a value indicating whether YAML anchors (&name) are allowed during deserialization. |
| BlockSequenceMappingStyle | Gets or sets how mappings are emitted when they appear as items in block sequences. |
| BlockSequenceSequenceStyle | Gets or sets how nested sequences are emitted when they appear as items in block sequences. |
| Converters | Gets the custom converters. |
| Default | Gets a default options instance. |
| DefaultIgnoreCondition | Gets or sets the default ignore condition for null/default values. |
| DictionaryKeyPolicy | Gets or sets the policy used to convert dictionary keys during serialization. |
| DuplicateKeyHandling | Gets or sets behavior when duplicate mapping keys are encountered while reading. |
| IgnoreReadOnlyFields | Gets or sets a value indicating whether read-only fields are ignored during serialization. |
| IgnoreReadOnlyProperties | Gets or sets a value indicating whether read-only properties are ignored during serialization. |
| IncludeFields | Gets or sets a value indicating whether public fields are included during serialization and deserialization. |
| IndentBlockSequences | Gets or sets a value indicating whether a block sequence that is the value of a mapping key is indented relative to that mapping. |
| IndentSize | Gets or sets the number of spaces to use when YamlSerializerOptions.WriteIndented is enabled. |
| MappingOrder | Gets or sets member ordering behavior for emitted mappings. |
| MaxAliasExpansionNodeCount | Gets or sets the maximum number of nodes that aliases are allowed to materialize while building a YamlNode document object model. |
| MaxDepth | Gets or sets the maximum allowed nesting depth for YAML mappings and sequences during serialization and deserialization. |
| PolymorphismOptions | Gets polymorphism options. |
| PreferredObjectCreationHandling | Gets or sets the preferred object creation handling for properties and fields during deserialization. |
| PropertyNameCaseInsensitive | Gets or sets a value indicating whether property name matching is case-insensitive. |
| PropertyNamingPolicy | Gets or sets the policy used to convert CLR property names. |
| ReferenceHandling | Gets or sets object reference handling behavior. |
| RespectNullableAnnotations | Gets or sets a value indicating whether nullable annotations are enforced during serialization and deserialization. |
| RespectRequiredConstructorParameters | Gets or sets a value indicating whether required constructor parameters must be present during deserialization. |
| ScalarStylePreferences | Gets scalar style preferences for serialization. |
| Schema | Gets or sets the schema used for scalar resolution. |
| SourceName | Gets or sets an optional name for the YAML source. |
| TypeClassifiers | Gets the type classifier factories used to select a C# union case from a YAML payload. |
| TypeInfoResolver | Gets or sets a metadata resolver used to retrieve YamlTypeInfo instances. |
| UnmappedMemberHandling | Gets or sets how unmapped YAML members are handled during object deserialization. |
| UnsafeAllowDeserializeFromTagTypeName | Gets or sets a value indicating whether unregistered runtime type names from YAML tags are allowed during deserialization. |
| UseSchema | Gets or sets a value indicating whether scalar deserialization should resolve through YamlSerializerOptions.Schema. |
| WriteIndented | Gets or sets a value indicating whether output should be indented. |
AllowAliases
YamlSerializerOptions.csGets or sets a value indicating whether YAML aliases (*name) are allowed during deserialization.
public bool AllowAliases { get; init; }AllowAnchors
YamlSerializerOptions.csGets or sets a value indicating whether YAML anchors (&name) are allowed during deserialization.
public bool AllowAnchors { get; init; }Property value
Remarks
When false, encountering a node that declares an anchor throws a YamlException.
BlockSequenceMappingStyle
YamlSerializerOptions.csGets or sets how mappings are emitted when they appear as items in block sequences.
public YamlSequenceItemStyle BlockSequenceMappingStyle { get; init; }Property value
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.csGets or sets how nested sequences are emitted when they appear as items in block sequences.
public YamlSequenceItemStyle BlockSequenceSequenceStyle { get; init; }Property value
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.
Converters
YamlSerializerOptions.csGets the custom converters.
public IReadOnlyList<YamlConverter> Converters { get; init; }Property value
Exceptions
ArgumentNullException- Value is
null. ArgumentException- A converter entry is
null.
Remarks
Converters are evaluated in order and take precedence over built-in converters.
Default
YamlSerializerOptions.csDefaultIgnoreCondition
YamlSerializerOptions.csGets or sets the default ignore condition for null/default values.
public YamlIgnoreCondition DefaultIgnoreCondition { get; init; }Property value
DictionaryKeyPolicy
YamlSerializerOptions.csGets or sets the policy used to convert dictionary keys during serialization.
public YamlNamingPolicy? DictionaryKeyPolicy { get; init; }Property value
DuplicateKeyHandling
YamlSerializerOptions.csGets or sets behavior when duplicate mapping keys are encountered while reading.
public YamlDuplicateKeyHandling DuplicateKeyHandling { get; init; }Property value
IgnoreReadOnlyFields
YamlSerializerOptions.csIgnoreReadOnlyProperties
YamlSerializerOptions.csIncludeFields
YamlSerializerOptions.csIndentBlockSequences
YamlSerializerOptions.csGets 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
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.
IndentSize
YamlSerializerOptions.csGets or sets the number of spaces to use when YamlSerializerOptions.WriteIndented is enabled.
public int IndentSize { get; init; }MappingOrder
YamlSerializerOptions.csGets or sets member ordering behavior for emitted mappings.
public YamlMappingOrderPolicy MappingOrder { get; init; }Property value
MaxAliasExpansionNodeCount
YamlSerializerOptions.csGets 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
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.
MaxDepth
YamlSerializerOptions.csGets or sets the maximum allowed nesting depth for YAML mappings and sequences during serialization and deserialization.
public int MaxDepth { get; init; }Property value
Exceptions
ArgumentOutOfRangeException- Value is less than 0.
Remarks
A value of 0 uses the default limit of 64.
PolymorphismOptions
YamlSerializerOptions.csGets polymorphism options.
public YamlPolymorphismOptions PolymorphismOptions { get; init; }Property value
Exceptions
ArgumentNullException- Value is
null. ArgumentException- A derived type mapping has a
nulllist or anullentry.
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.csGets or sets the preferred object creation handling for properties and fields during deserialization.
public YamlObjectCreationHandling PreferredObjectCreationHandling { get; init; }Property value
Remarks
A member-level or type-level YamlObjectCreationHandlingAttribute overrides this setting. The default behavior is YamlObjectCreationHandling.Replace.
PropertyNameCaseInsensitive
YamlSerializerOptions.csPropertyNamingPolicy
YamlSerializerOptions.csGets or sets the policy used to convert CLR property names.
public YamlNamingPolicy? PropertyNamingPolicy { get; init; }Property value
ReferenceHandling
YamlSerializerOptions.csGets or sets object reference handling behavior.
public YamlReferenceHandling ReferenceHandling { get; init; }Property value
RespectNullableAnnotations
YamlSerializerOptions.csRespectRequiredConstructorParameters
YamlSerializerOptions.csScalarStylePreferences
YamlSerializerOptions.csGets scalar style preferences for serialization.
public YamlScalarStylePreferences ScalarStylePreferences { get; init; }Schema
YamlSerializerOptions.csGets or sets the schema used for scalar resolution.
public YamlSchemaKind Schema { get; init; }Property value
SourceName
YamlSerializerOptions.csGets or sets an optional name for the YAML source.
public string? SourceName { get; init; }Property value
Remarks
This value is used to annotate YamlException messages with a source name (for example, a file path) when reporting parse errors.
TypeClassifiers
YamlSerializerOptions.csGets 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.csGets or sets a metadata resolver used to retrieve YamlTypeInfo instances.
public IYamlTypeInfoResolver? TypeInfoResolver { get; init; }Property value
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.csGets or sets how unmapped YAML members are handled during object deserialization.
public YamlUnmappedMemberHandling UnmappedMemberHandling { get; init; }Property value
Remarks
Extension data members still capture unmatched properties when present.
UnsafeAllowDeserializeFromTagTypeName
YamlSerializerOptions.csGets or sets a value indicating whether unregistered runtime type names from YAML tags are allowed during deserialization.
public bool UnsafeAllowDeserializeFromTagTypeName { get; init; }Property value
Remarks
Enabling this option allows tag-based type name activation and should only be used with trusted YAML input.
UseSchema
YamlSerializerOptions.csGets or sets a value indicating whether scalar deserialization should resolve through YamlSerializerOptions.Schema.
public bool UseSchema { get; init; }Property value
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.
WriteIndented
YamlSerializerOptions.csGets or sets a value indicating whether output should be indented.
public bool WriteIndented { get; init; }Property value
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
| Name | Description |
|---|---|
| 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)No documentation is available.
Equals(object?)
GetHashCode()
GetTypeInfo<T>()
YamlSerializerOptions.csGets 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()
TryGetTypeInfo<T>(YamlTypeInfo<T>?)
YamlSerializerOptions.csAttempts 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
typeInfoYamlTypeInfo<T>?- The metadata resolved for
T, when available.
Returns
bool true when metadata is available for T; otherwise false.
Operators
| Name | Description |
|---|---|
| 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.