Table of Contents

YamlFormatter Class

Definition

Serializes and deserializes an object, in YAML format.

public class YamlFormatter : StreamFormatter<YamlFormatterOptions>, IConfigurable<YamlFormatterOptions>
Inheritance
YamlFormatter
Implements
Inherited Members

Examples

This example demonstrates how to serialize and deserialize objects using YamlFormatter with custom encoding settings. It shows the complete workflow: creating an application configuration object, instantiating the formatter with UTF-8 encoding configuration, serializing the config to YAML, displaying the YAML text, then deserializing the YAML back to the original object type. The observable outcome is that the object can be round-tripped (serialized and deserialized) while maintaining its structure and data integrity.

using System;
using System.IO;
using System.Text;
using Codebelt.Extensions.YamlDotNet.Formatters;

namespace ExampleNamespace;

public class AppConfiguration
{
    public string ApplicationName { get; set; }
    public string Version { get; set; }
    public int MaxConnections { get; set; }
}

class Program
{
    static void Main()
    {
        // Create and configure the formatter
        var formatter = new YamlFormatter(options =>
        {
            options.Settings.Encoding = Encoding.UTF8;
        });
        
        // Serialize an object to YAML
        var config = new AppConfiguration
        {
            ApplicationName = "MyApplication",
            Version = "1.0.0",
            MaxConnections = 100
        };
        
        var yamlStream = formatter.Serialize(config, typeof(AppConfiguration));
        var yamlText = new StreamReader(yamlStream).ReadToEnd();
        Console.WriteLine("Serialized YAML:");
        Console.WriteLine(yamlText);
        
        // Reset stream for deserialization
        yamlStream = new MemoryStream(Encoding.UTF8.GetBytes(yamlText));
        
        // Deserialize YAML back to object
        var deserializedConfig = (AppConfiguration)formatter.Deserialize(yamlStream, typeof(AppConfiguration));
        Console.WriteLine($"\nDeserialized config: {deserializedConfig.ApplicationName} v{deserializedConfig.Version}");
    }
}

Constructors

Name Description
YamlFormatter()

Initializes a new instance of the YamlFormatter class.

YamlFormatter(YamlFormatterOptions)

Initializes a new instance of the YamlFormatter class.

YamlFormatter(Action<YamlFormatterOptions>)

Initializes a new instance of the YamlFormatter class.

Methods

Name Description
Deserialize(Stream, Action<IDeserializer, Parser>)

Deserializes the specified value using delegate deserializerFactory.

Deserialize(Stream, Type)

Deserializes the specified value into an object of objectType.

DeserializeObject(Stream, Action<IDeserializer, Parser>, YamlFormatterOptions)

Deserializes the specified value using delegate deserializerFactory.

DeserializeObject(Stream, Action<IDeserializer, Parser>, Action<YamlFormatterOptions>)

Deserializes the specified value using delegate deserializerFactory.

Serialize(object, Type)

Serializes the specified source to an object of Stream.

See Also