GuideIntroduction
Getting started
Install Styml, write an object as YAML, read it back, and edit a YAML document.
Styml is a YAML 1.2 library for .NET: a reader, a writer, a document model and a serializer with reflection and source
generation. It is System.Text.Json's code and API carried over to YAML, so if you know JsonSerializer, you know
YamlSerializer.
Install the packageLink to this heading
Styml is published on NuGet as Styml:
dotnet add package Styml
The package targets .NET 11, .NET 10 and .NET Standard 2.0, which covers .NET Framework 4.6.2 and later. Its namespace
is System.Text.Yaml, next to System.Text.Json, without conflict:
using System.Text.Yaml; // YamlSerializer, YamlSerializerOptions, the reader, the writer, YamlDocument
using System.Text.Yaml.Nodes; // YamlNode, YamlObject, YamlArray, YamlValue
using System.Text.Yaml.Serialization; // attributes, converters, the source generator's types
Previews of the next version are published to a Feedz feed
after every push to main:
dotnet nuget add source https://f.feedz.io/sebastienros/styml/nuget/index.json --name styml-preview
dotnet add package Styml --prerelease
Write an object as YAMLLink to this heading
YamlSerializer.Serialize writes any .NET object, with the public properties of its type. The samples of this page use
this record:
public record Service(string Name, string Image, int[] Ports, Dictionary<string, string> Environment);
var service = new Service("web", "nginx:1.27", [80, 443], new() { ["TIER"] = "front", ["REPLICAS"] = "3" });
string yaml = YamlSerializer.Serialize(service);
Name: web
Image: nginx:1.27
Ports:
- 80
- 443
Environment:
TIER: front
REPLICAS: "3"
The writer quotes "3" because a plain 3 would read back as a number. It quotes exactly what YAML would otherwise
resolve to another type, and nothing else: see Scalar styles and quoting.
Read YAML into an objectLink to this heading
YamlSerializer.Deserialize reads YAML into a type. Members are matched by name, case-sensitively by default, and a
type with a constructor is created through it:
string yaml = """
Name: api
Image: example/api:2.4
Ports: [8080]
Environment:
LOG_LEVEL: debug
""";
Service service = YamlSerializer.Deserialize<Service>(yaml)!;
Console.WriteLine($"{service.Name} on {service.Ports[0]}, {service.Environment.Count} variable");
api on 8080, 1 variable
The flow sequence [8080] reads like a block one: the style of the source doesn't matter to the serializer.
Edit a YAML documentLink to this heading
YamlNode is a mutable document, like JsonNode. It keeps the styles of the source, so an edited document is written
back the way it was written:
YamlNode config = YamlNode.Parse("""
server:
port: 8080
hosts: [a.example, b.example]
""")!;
config["server"]!["port"] = 9090;
config["server"]!["hosts"]!.AsArray().Add("c.example");
string yaml = config.ToYamlString();
server:
port: 9090
hosts: [a.example, b.example, c.example]
About the samplesLink to this heading
Every sample of this guide is compiled and run by Styml's tests (test/Styml.Yaml.Tests/Docs), and the output shown
under it is the one the tests check, so the samples can't drift from what the library does. Where a sample shows an
exception, it calls a small helper that returns the value, or the type and message of the exception:
static string Describe(Func<object?> read)
{
try
{
return read()?.ToString() ?? "null";
}
catch (Exception e)
{
return e.GetType().Name + ": " + e.Message.Split('\n')[0];
}
}
Next stepsLink to this heading
- Coming from System.Text.Json: what is the same, and what YAML changes.
- Serialize and deserialize: the serializer's methods, options and errors.
- Source generation and Native AOT: serialize without reflection.
- The DOM and the reader and the writer, to work with YAML without types.
- The API reference.