Table of Contents

YamlSerializerOptions Class

Definition

Namespace
Codebelt.Extensions.YamlDotNet
Assemblies
Codebelt.Extensions.YamlDotNet.dll
Source
src/Codebelt.Extensions.YamlDotNet/YamlSerializerOptions.cs

Configuration options for YamlDotNet.Serialization.SerializerBuilder and YamlDotNet.Serialization.DeserializerBuilder.

public class YamlSerializerOptions : EncodingOptions, IEncodingOptions, IValidatableParameterObject, IParameterObject
Inheritance
YamlSerializerOptions
Implements
Inherited Members

Examples

The following example demonstrates how to configure YamlSerializerOptions to customize YAML serialization behavior for the SerializerBuilder and DeserializerBuilder.

using System;
using System.IO;
using Codebelt.Extensions.YamlDotNet;
using Codebelt.Extensions.YamlDotNet.Converters;
using Codebelt.Extensions.YamlDotNet.Formatters;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace ExampleNamespace;

public class Person
{
    public required string FirstName { get; set; }
    public required string LastName { get; set; }
}

class Program
{
    static void Main()
    {
        // Create and configure serializer options
        var options = new YamlSerializerOptions
        {
            // Use camelCase for property names in YAML
            NamingConvention = CamelCaseNamingConvention.Instance,
            
            // Configure indentation
            WhiteSpaceIndentation = 2,
            
            // Do not use YAML aliases for duplicate objects
            UseAliases = false,
            
            // Ensure data roundtrips correctly
            EnsureRoundtrip = false
        };
        
        // Add custom converters
        options.Converters.AddFailureConverter();
        
        // Create a formatter using these options
        var yamlFormatter = new Codebelt.Extensions.YamlDotNet.Formatters.YamlFormatter(new YamlFormatterOptions { Settings = options });
        
        var person = new Person { FirstName = "John", LastName = "Doe" };
        var yaml = yamlFormatter.Serialize(person, typeof(Person));
        
        using (var reader = new StreamReader(yaml))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}

Constructors

Name Description
YamlSerializerOptions()

Initializes a new instance of the YamlSerializerOptions class.

Properties

Name Description
Converters

Gets a YamlConverter collection that will be used during serialization.

EnsureRoundtrip

Gets or sets a value that will force the emission of tags and emit only properties with setters. The default value is false.

EnumNamingConvention

Gets or sets the YamlDotNet.Serialization.INamingConvention used when handling enum values. The default value is YamlDotNet.Serialization.NamingConventions.NullNamingConvention.Instance (unaltered).

FormatProvider

Gets or sets the culture-specific formatting information used when writing YAML. The default is InvariantCulture.

Formatter

Gets or sets formatting options for the generated YAML. Defaults to YamlDotNet.Serialization.YamlFormatter.Default.

IndentSequences

Gets or sets a value indicating whether to indent sequences/arrays in the generated YAML. The default value is true.

IsCanonical

Gets or sets a value indicating whether the output is in a canonical form.

MaximumRecursion

Gets or sets the maximum recursion that is allowed while traversing the object graph. The default value is 50.

NamingConvention

Gets or sets the YamlDotNet.Serialization.INamingConvention used to convert a property's name on an object to another format. The default value is YamlDotNet.Serialization.NamingConventions.NullNamingConvention.Instance (unaltered).

NewLine

Gets or sets the new line character to use when serializing to YAML. Default is NewLine.

ReflectionRules

Gets or sets the binding constraints for reflection based member searching.

ScalarStyle

Gets or sets the default quoting style for scalar values. The default value is YamlDotNet.Core.ScalarStyle.Any.

TextWidth

Gets or sets the preferred width of text in the generated YAML.

UseAliases

Gets or sets a value indicating whether to allow use of aliases in the generated YAML. The default value is false.

ValuesHandling

Gets or sets how properties with default and null values should be handled. The default value is YamlDotNet.Serialization.DefaultValuesHandling.Preserve.

WhiteSpaceIndentation

Gets or sets the white space indentation.

Methods

Name Description
ValidateOptions()

Determines whether the public read-write properties of this instance are in a valid state.