Open source, MIT licensed
YAML for .NET. The API you already know. The performance you expect.
Styml is System.Text.Json's code and API, carried over to YAML 1.2: YamlSerializer, Utf8YamlReader, YamlDocument, YamlNode and a source generator. Code written against System.Text.Json reads the same with Yaml in place of Json.
dotnet add package Styml
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" # quoted: a plain 3 is a number
Features
System.Text.Json's design, and everything YAML adds
- 01
The same API
Options, attributes, converters, naming policies, the contract model and the source generator, renamed from
Coming from System.Text.JsonJsontoYaml. - 02
YAML 1.2, all of it
Every valid case of the yaml-test-suite is read with the expected events, every error case is rejected.
Reading YAML - 03
Anchors and aliases
Aliases are read everywhere,
ReferencesReferenceHandler.Preservewrites anchors, and alias expansion is bounded. - 04
Tags, safely
Tag-based polymorphism and tag mappings are explicit allow-lists: a tag never loads a .NET type.
Polymorphism and tags - 05
Styles you choose
Block or flow collections, plain, quoted, literal or folded scalars, comments, and quotes only where YAML needs them.
Scalar styles - 06
A DOM that round-trips
The DOMYamlDocumentandYamlNodekeep styles, anchors and tags, so an edited document keeps its look. - 07
Trimming and Native AOT
A source-generated
Source generationYamlSerializerContextserializes without reflection, with the analyzers on. - 08
Fast, few allocations
A UTF-8 reader and writer over spans, pooled buffers, and a document that allocates 72 bytes.
Performance
Source generation
Declare the types once, serialize without reflection
A YamlSerializerContext is generated at build time with the contracts of your types and the options you set on it, the YAML ones included. It works trimmed and with Native AOT.
string yaml = YamlSerializer.Serialize(service, AppYamlContext.Default.Service);
Service back = YamlSerializer.Deserialize(yaml, AppYamlContext.Default.Service)!;
[YamlSerializable(typeof(Service))]
[YamlSourceGenerationOptions(
PropertyNamingPolicy = YamlKnownNamingPolicy.CamelCase,
IndentSequences = true)]
public partial class AppYamlContext : YamlSerializerContext;
Paste some YAML now.
The playground runs Styml in your browser with WebAssembly. Format YAML, convert it to JSON, look at its document tree and its tokens, and change every reading and writing option.
Open the playground