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
optionsYamlFormatterOptionsThe configured YamlFormatterOptions.
YamlFormatter(Action<YamlFormatterOptions>)
Initializes a new instance of the YamlFormatter class.
public YamlFormatter(Action<YamlFormatterOptions> setup)
Parameters
setupAction<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
valueStreamThe object from which to deserialize the object graph.
deserializerFactoryAction<IDeserializer, Parser>The delegate that performs the deserialization.
Exceptions
- ArgumentNullException
valueis null -or-deserializerFactoryis null.
Deserialize(Stream, Type)
Deserializes the specified value into an object of objectType.
public override object Deserialize(Stream value, Type objectType)
Parameters
valueStreamThe object from which to deserialize the object graph.
objectTypeTypeThe type of the deserialized object.
Returns
- object
An object of
objectType.
Exceptions
- ArgumentNullException
valueis null -or-objectTypeis 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
valueStreamThe string from which to deserialize the object graph.
deserializerFactoryAction<IDeserializer, Parser>The delegate that performs the deserialization.
optionsYamlFormatterOptionsThe 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
valueStreamThe string from which to deserialize the object graph.
deserializerFactoryAction<IDeserializer, Parser>The delegate that performs the deserialization.
setupAction<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
sourceobjectThe object to serialize to YAML format.
objectTypeTypeThe type of the object to serialize.
Returns
- Stream
A stream of the serialized
source.
Exceptions
- ArgumentNullException
sourceis null -or-objectTypeis null.