Table of Contents

Class YamlFormatter

Namespace
Codebelt.Extensions.YamlDotNet.Formatters
Assembly
Codebelt.Extensions.YamlDotNet.dll

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

YamlFormatter()

Initializes a new instance of the YamlFormatter class.

public YamlFormatter()

YamlFormatter(YamlFormatterOptions)

Initializes a new instance of the YamlFormatter class.

public YamlFormatter(YamlFormatterOptions options)

Parameters

options YamlFormatterOptions

The configured YamlFormatterOptions.

YamlFormatter(Action<YamlFormatterOptions>)

Initializes a new instance of the YamlFormatter class.

public YamlFormatter(Action<YamlFormatterOptions> setup)

Parameters

setup Action<YamlFormatterOptions>

The YamlFormatterOptions which need to be configured.

Methods

Deserialize(Stream, Action<IDeserializer, Parser>)

Deserializes the specified value using delegate deserializerFactory.

public void Deserialize(Stream value, Action<IDeserializer, Parser> deserializerFactory)

Parameters

value Stream

The object from which to deserialize the object graph.

deserializerFactory Action<IDeserializer, Parser>

The delegate that performs the deserialization.

Exceptions

ArgumentNullException

value is null -or- deserializerFactory is null.

Deserialize(Stream, Type)

Deserializes the specified value into an object of objectType.

public override object Deserialize(Stream value, Type objectType)

Parameters

value Stream

The object from which to deserialize the object graph.

objectType Type

The type of the deserialized object.

Returns

object

An object of objectType.

Exceptions

ArgumentNullException

value is null -or- objectType is null.

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

Deserializes the specified value using delegate deserializerFactory.

public static void DeserializeObject(Stream value, Action<IDeserializer, Parser> deserializerFactory, YamlFormatterOptions options)

Parameters

value Stream

The string from which to deserialize the object graph.

deserializerFactory Action<IDeserializer, Parser>

The delegate that performs the deserialization.

options YamlFormatterOptions

The configured YamlFormatterOptions.

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

Deserializes the specified value using delegate deserializerFactory.

public static void DeserializeObject(Stream value, Action<IDeserializer, Parser> deserializerFactory, Action<YamlFormatterOptions> setup = null)

Parameters

value Stream

The string from which to deserialize the object graph.

deserializerFactory Action<IDeserializer, Parser>

The delegate that performs the deserialization.

setup Action<YamlFormatterOptions>

The YamlFormatterOptions which may be configured.

Serialize(object, Type)

Serializes the specified source to an object of Stream.

public override Stream Serialize(object source, Type objectType)

Parameters

source object

The object to serialize to YAML format.

objectType Type

The type of the object to serialize.

Returns

Stream

A stream of the serialized source.

Exceptions

ArgumentNullException

source is null -or- objectType is null.

See Also