Table of Contents

Class EmitterExtensions

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

Extension methods for the YamlDotNet.Core.IEmitter interface.

public static class EmitterExtensions
Inheritance
EmitterExtensions

Examples

This example demonstrates how to use the EmitterExtensions methods to manually construct YAML output with fine-grained control over structure and formatting. The workflow includes: initializing a StringWriter and Emitter instance, writing object properties using WriteString and WritePropertyName, creating nested arrays with WriteStartArray/WriteEndArray, and serializing complex objects with WriteObject. The observable outcome is a properly formatted YAML string that combines primitive values, arrays, and nested objects, demonstrating how each extension method contributes to building the complete YAML document structure.

using System;
using System.IO;
using Codebelt.Extensions.YamlDotNet;
using Codebelt.Extensions.YamlDotNet.Formatters;
using YamlDotNet.Core;
using YamlDotNet.Serialization;

namespace ExampleNamespace;

public class AppConfig
{
    public string Name { get; set; }
    public string Version { get; set; }
    public string[] Features { get; set; }
}

class Program
{
    static void Main()
    {
        var options = new YamlFormatterOptions();
        var stringWriter = new StringWriter();
        var emitter = new Emitter(stringWriter);
        
        // Use WriteStartObject and WriteString for simple key-value pairs
        emitter.WriteStartObject();
        emitter.WriteString("applicationName", "MyApp");
        emitter.WriteString("version", "1.0.0");
        
        // Use WriteStartArray and WriteEndArray for list values
        emitter.WritePropertyName("features");
        emitter.WriteStartArray();
        emitter.WriteValue("logging");
        emitter.WriteValue("metrics");
        emitter.WriteValue("debugging");
        emitter.WriteEndArray();
        
        // Use WriteObject to serialize a complex object
        emitter.WritePropertyName("configuration");
        var config = new AppConfig { Name = "Primary", Version = "2.0", Features = new[] { "auth", "caching" } };
        emitter.WriteObject(config, options);
        
        emitter.WriteEndObject();
        
        Console.WriteLine(stringWriter.ToString());
    }
}

Methods

WriteEndArray(IEmitter)

Denotes the end of a YAML array.

public static void WriteEndArray(this IEmitter writer)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

WriteEndObject(IEmitter)

Denotes the end of a YAML object.

public static void WriteEndObject(this IEmitter writer)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

WriteObject(IEmitter, object, YamlFormatterOptions)

Serializes the specified value into a YAML format.

public static void WriteObject(this IEmitter writer, object value, YamlFormatterOptions options)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

value object

The Object to serialize.

options YamlFormatterOptions

Options to control the conversion behavior.

WriteObject(IEmitter, object, Type, YamlFormatterOptions)

Serializes the specified value into a YAML format.

public static void WriteObject(this IEmitter writer, object value, Type valueType, YamlFormatterOptions options)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

value object

The Object to serialize.

valueType Type

The type of the value to convert.

options YamlFormatterOptions

Options to control the conversion behavior.

WritePropertyName(IEmitter, string)

Writes the propertyName of an YAML object.

public static void WritePropertyName(this IEmitter writer, string propertyName)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

propertyName string

The name of the YAML object.

WriteStartArray(IEmitter)

Writes the beginning of a YAML array.

public static void WriteStartArray(this IEmitter writer)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

WriteStartObject(IEmitter)

Writes the beginning of a YAML object.

public static void WriteStartObject(this IEmitter writer)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

WriteString(IEmitter, string, string, Action<ScalarOptions>)

Writes the propertyName and value as part of a name/value pair of an YAML object.

public static void WriteString(this IEmitter writer, string propertyName, string value, Action<ScalarOptions> setup = null)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

propertyName string

The name of the YAML object.

value string

The value to be written as part of the name/value pair of an YAML object.

setup Action<ScalarOptions>

The ScalarOptions which may be configured.

WriteValue(IEmitter, string, Action<ScalarOptions>)

Writes the value of an YAML object.

public static void WriteValue(this IEmitter writer, string value, Action<ScalarOptions> setup = null)

Parameters

writer IEmitter

The YamlDotNet.Core.IEmitter to extend.

value string

The value to be written as part of the name/value pair of an YAML object.

setup Action<ScalarOptions>

The ScalarOptions which may be configured.