Table of Contents

Class YamlFormatterOptions

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

Configuration options for YamlFormatter.

public class YamlFormatterOptions : EncodingOptions, IEncodingOptions, IExceptionDescriptorOptions, IContentNegotiation, IValidatableParameterObject, IParameterObject
Inheritance
YamlFormatterOptions
Implements
Inherited Members
Extension Methods

Examples

The following example demonstrates how to configure YamlFormatterOptions to customize YAML serialization behavior, including converters, encoding, and fault sensitivity.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Codebelt.Extensions.YamlDotNet.Converters;
using Codebelt.Extensions.YamlDotNet.Formatters;
using Cuemon.Diagnostics;

namespace ExampleNamespace;

class Program
{
    static void Main()
    {
        // Create options and configure defaults
        var options = new YamlFormatterOptions
        {
            // Set encoding for output
            Encoding = Encoding.UTF8,
            
            // Configure which exception details to include
            SensitivityDetails = FaultSensitivityDetails.FailureWithStackTrace | FaultSensitivityDetails.FailureWithData
        };
        
        // Add custom converters to the settings
        options.Settings.Converters.AddFailureConverter();
        
        // Use options to create a formatter
        var formatter = new YamlFormatter(options);
        
        // Now use the formatter for serialization
        try
        {
            throw new InvalidOperationException("Example error");
        }
        catch (Exception ex)
        {
            var failure = new Failure(ex, FaultSensitivityDetails.FailureWithStackTrace | FaultSensitivityDetails.FailureWithData);
            var yaml = formatter.Serialize(failure, typeof(Failure));
            using (var reader = new StreamReader(yaml))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
        }
    }
}

Constructors

YamlFormatterOptions()

Initializes a new instance of the YamlFormatterOptions class.

public YamlFormatterOptions()

Remarks

 The following table shows the initial property values for an instance of <xref href="Codebelt.Extensions.YamlDotNet.Formatters.YamlFormatterOptions" data-throw-if-not-resolved="false"></xref>.

 <table><thead><tr><th class="term">Property</th><th class="description">Initial Value</th></tr></thead><tbody><tr><td class="term"><xref href="Codebelt.Extensions.YamlDotNet.Formatters.YamlFormatterOptions.Settings" data-throw-if-not-resolved="false"></xref></td><td class="description"><code>new YamlSerializerOptions();</code></td></tr><tr><td class="term"><xref href="Codebelt.Extensions.YamlDotNet.Formatters.YamlFormatterOptions.SensitivityDetails" data-throw-if-not-resolved="false"></xref></td><td class="description"><xref href="Cuemon.Diagnostics.FaultSensitivityDetails.None" data-throw-if-not-resolved="false"></xref></td></tr><tr><td class="term"><xref href="Codebelt.Extensions.YamlDotNet.Formatters.YamlFormatterOptions.SupportedMediaTypes" data-throw-if-not-resolved="false"></xref></td><td class="description"><pre><code class="lang-csharp">new List<MediaTypeHeaderValue>()

{ DefaultMediaType, new("text/plain"), new("application/yaml"), new("text/yaml"), new("/") };

Properties

DefaultConverters

Gets or sets a delegate that is invoked when YamlFormatterOptions is initialized and propagates registered YamlConverter implementations.

public static Action<IList<YamlConverter>> DefaultConverters { get; set; }

Property Value

Action<IList<YamlConverter>>

The delegate which propagates registered YamlConverter implementations when YamlFormatterOptions is initialized.

DefaultMediaType

Provides the default/fallback media type that the associated formatter should use when content negotiation either fails or is absent.

public static MediaTypeHeaderValue DefaultMediaType { get; }

Property Value

MediaTypeHeaderValue

The media type that the associated formatter should use when content negotiation either fails or is absent.

SensitivityDetails

Gets or sets a bitwise combination of the enumeration values that specify which sensitive details to include in the serialized result.

public FaultSensitivityDetails SensitivityDetails { get; set; }

Property Value

FaultSensitivityDetails

The enumeration values that specify which sensitive details to include in the serialized result.

Settings

Gets or sets the settings to support the YamlConverter.

public YamlSerializerOptions Settings { get; set; }

Property Value

YamlSerializerOptions

A YamlSerializerOptions instance that specifies a set of features to support the YamlConverter object.

SupportedMediaTypes

Gets or sets the collection of MediaTypeHeaderValue elements supported by the YamlFormatter.

public IReadOnlyCollection<MediaTypeHeaderValue> SupportedMediaTypes { get; set; }

Property Value

IReadOnlyCollection<MediaTypeHeaderValue>

A collection of MediaTypeHeaderValue elements supported by the YamlFormatter.

Methods

ValidateOptions()

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

public void ValidateOptions()

Remarks

This method is expected to throw exceptions when one or more conditions fails to be in a valid state.

Exceptions

InvalidOperationException

Settings cannot be null.