Table of Contents

Class YamlConverterFactory

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

Provides a factory based way to create and wrap an YamlConverter implementation.

public static class YamlConverterFactory
Inheritance
YamlConverterFactory

Examples

This example demonstrates how to create a custom YAML converter using YamlConverterFactory for a specific type and integrate it with YamlFormatter. It shows the workflow: defining a custom writer delegate that controls how a Person object is serialized (object start/end, properties), instantiating the factory to create the converter, adding the converter to the formatter's settings, and then serializing a Person instance to observe the custom YAML output. The observable outcome is that the Person object is serialized according to the custom writer logic rather than default YAML conventions.

using System;
using System.Collections.Generic;
using System.IO;
using Codebelt.Extensions.YamlDotNet;
using Codebelt.Extensions.YamlDotNet.Formatters;
using YamlDotNet.Core;

namespace ExampleNamespace;

class Person
{
    public required string Name { get; set; }
    public required int Age { get; set; }
}

class Program
{
    static void Main()
    {
        // Create a custom converter for the Person type
        var personConverter = YamlConverterFactory.Create<Person>(
            writer: (emitter, person, formatter) =>
            {
                emitter.WriteStartObject();
                emitter.WriteString("name", person.Name);
                emitter.WriteString("age", person.Age.ToString());
                emitter.WriteEndObject();
            }
        );
        
        // Add the custom converter to the formatter options
        var options = new YamlFormatterOptions { Settings = new YamlSerializerOptions() };
        options.Settings.Converters.Add(personConverter);
        
        // Use the formatter with the custom converter
        var formatter = new YamlFormatter(options);
        var person = new Person { Name = "John Doe", Age = 30 };
        var yaml = formatter.Serialize(person, typeof(Person));
        
        using (var reader = new StreamReader(yaml))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}

Methods

Create(Func<Type, bool>, Action<IEmitter, object, YamlFormatter>, Func<IParser, Type, YamlFormatter, object>)

Creates a dynamic instance of an YamlConverter implementation wrapping Codebelt.Extensions.YamlDotNet.Converters.YamlConverter.WriteYamlCore(YamlDotNet.Core.IEmitter, object, YamlDotNet.Serialization.ObjectSerializer) through writer and ReadYamlCore(IParser, Type, ObjectDeserializer) through reader.

public static YamlConverter Create(Func<Type, bool> predicate, Action<IEmitter, object, YamlFormatter> writer = null, Func<IParser, Type, YamlFormatter, object> reader = null)

Parameters

predicate Func<Type, bool>

The function delegate that validates if the given Type can be converted to or from YAML.

writer Action<IEmitter, object, YamlFormatter>

The delegate that converts an object to its YAML representation.

reader Func<IParser, Type, YamlFormatter, object>

The delegate that generates an object from its YAML representation.

Returns

YamlConverter

An YamlConverter implementation of an object.

Create(Type, Action<IEmitter, object, YamlFormatter>, Func<IParser, Type, YamlFormatter, object>)

Creates a dynamic instance of an YamlConverter implementation wrapping Codebelt.Extensions.YamlDotNet.Converters.YamlConverter.WriteYamlCore(YamlDotNet.Core.IEmitter, object, YamlDotNet.Serialization.ObjectSerializer) through writer and ReadYamlCore(IParser, Type, ObjectDeserializer) through reader.

public static YamlConverter Create(Type typeToConvert, Action<IEmitter, object, YamlFormatter> writer = null, Func<IParser, Type, YamlFormatter, object> reader = null)

Parameters

typeToConvert Type

The type to convert.

writer Action<IEmitter, object, YamlFormatter>

The delegate that converts an object to its YAML representation.

reader Func<IParser, Type, YamlFormatter, object>

The delegate that generates an object from its YAML representation.

Returns

YamlConverter

An YamlConverter implementation of an object.

Create<T>(Action<IEmitter, T, YamlFormatter>, Func<IParser, Type, YamlFormatter, T>)

Creates a dynamic instance of an YamlConverter<T> implementation wrapping WriteYaml(IEmitter, T) through writer and ReadYaml(IParser, Type) through reader.

public static YamlConverter Create<T>(Action<IEmitter, T, YamlFormatter> writer = null, Func<IParser, Type, YamlFormatter, T> reader = null)

Parameters

writer Action<IEmitter, T, YamlFormatter>

The delegate that converts T to its YAML representation.

reader Func<IParser, Type, YamlFormatter, T>

The delegate that generates T from its YAML representation.

Returns

YamlConverter

An YamlConverter implementation of T.

Type Parameters

T

The type to implement an YamlConverter for.

Create<T>(Func<Type, bool>, Action<IEmitter, T, YamlFormatter>, Func<IParser, Type, YamlFormatter, T>)

Creates a dynamic instance of an YamlConverter<T> implementation wrapping WriteYaml(IEmitter, T) through writer and ReadYaml(IParser, Type) through reader.

public static YamlConverter Create<T>(Func<Type, bool> predicate, Action<IEmitter, T, YamlFormatter> writer = null, Func<IParser, Type, YamlFormatter, T> reader = null)

Parameters

predicate Func<Type, bool>

The function delegate that validates if the given Type can be converted to or from YAML.

writer Action<IEmitter, T, YamlFormatter>

The delegate that converts T to its YAML representation.

reader Func<IParser, Type, YamlFormatter, T>

The delegate that generates T from its YAML representation.

Returns

YamlConverter

An YamlConverter implementation of T.

Type Parameters

T

The type to implement an YamlConverter for.