✨ 100% vibe engineered ✨
A high-performance, AOT-compatible YAML 1.2 serializer for .NET.
- 100% AOT-compatible - No reflection at runtime, works with Native AOT, iOS, and WebAssembly
- Source-generated serialization - Type-safe serialization code generated at compile time
- Zero-allocation parsing - Uses
ref structandSpan<T>for allocation-free operation - YAML 1.2 compliant - Full support for the latest YAML specification
- Familiar API - Similar patterns to
System.Text.Json
Full YAML 1.2 support. Tested against the official yaml-test-suite (v2022-01-17).
dotnet add package Yamlifyusing Yamlify.Serialization;
// Your data types
public class Person
{
public string Name { get; set; } = "";
public int Age { get; set; }
public List<string>? Tags { get; set; }
}
// Register types for source generation
[YamlSerializable(typeof(Person))]
public partial class MySerializerContext : YamlSerializerContext { }var person = new Person
{
Name = "John Doe",
Age = 30,
Tags = ["developer", "yaml"]
};
// Option A: Pass context explicitly
string yaml = YamlSerializer.Serialize(person, MySerializerContext.Default.Person);
// Option B: Set default resolver once, use simple API everywhere
YamlSerializerOptions.Default.TypeInfoResolver = MySerializerContext.Default;
string yaml = YamlSerializer.Serialize(person); // No context needed!
// Output:
// name: John Doe
// age: 30
// tags:
// - developer
// - yamlvar yaml = """
name: Jane Smith
age: 25
tags:
- designer
""";
// With explicit context
var person = YamlSerializer.Deserialize<Person>(yaml, MySerializerContext.Default.Person);
// Or with default resolver configured
var person = YamlSerializer.Deserialize<Person>(yaml);var options = new YamlSerializerOptions
{
// Property naming
PropertyNamingPolicy = YamlNamingPolicy.CamelCase, // or SnakeCase, KebabCase (default)
// Null handling
IgnoreNullValues = true, // Omit properties with null values
IgnoreEmptyObjects = true, // Omit nested objects where all nullable props are null
// Circular references
ReferenceHandler = ReferenceHandler.IgnoreCycles,
// Formatting
WriteIndented = true,
IndentSize = 2
};
var yaml = YamlSerializer.Serialize(person, MySerializerContext.Default.Person, options);See the Serialization Guide for more details.
public class Product
{
[YamlPropertyName("product-id")]
public string Id { get; set; } = "";
[YamlIgnore]
public string InternalCode { get; set; } = "";
[YamlPropertyOrder(1)]
public string Name { get; set; } = "";
[YamlPropertyOrder(2)]
public decimal Price { get; set; }
}Yamlify supports polymorphic type hierarchies with type discriminators:
[YamlPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[YamlDerivedType(typeof(Dog), "dog")]
[YamlDerivedType(typeof(Cat), "cat")]
public abstract class Animal
{
public string Name { get; set; } = "";
}
public class Dog : Animal { public string Breed { get; set; } = ""; }
public class Cat : Animal { public bool IsIndoor { get; set; } }$type: dog
name: Buddy
breed: Golden RetrieverFor configurations where the type is determined by a sibling property (e.g., an enum):
public class ConfigItem
{
public string Name { get; set; } = "";
public ValueType Type { get; set; }
[YamlSiblingDiscriminator(nameof(Type))]
[YamlDiscriminatorMapping(nameof(ValueType.String), typeof(StringValue))]
[YamlDiscriminatorMapping(nameof(ValueType.Integer), typeof(IntegerValue))]
public ConfigValue? Value { get; set; }
}See the Serialization Guide for complete polymorphism documentation.
For advanced scenarios, use the low-level reader directly:
using Yamlify.Core;
var yaml = """
name: John
age: 30
"""u8;
var reader = new Utf8YamlReader(yaml);
while (reader.Read())
{
Console.WriteLine($"{reader.TokenType}: {reader.GetString()}");
}- Serialization Guide - Polymorphism, sibling discrimination, options, and attributes
- Architecture & Internals - Deep dive into parser design
# Clone with submodules (recommended)
git clone --recurse-submodules https://github.com/SwissLife-OSS/yamlify.git
# Or if already cloned, initialize submodules
git submodule update --init --recursivedotnet testThe test suite includes the official yaml-test-suite as a git submodule for YAML compliance testing.
MIT License - see LICENSE for details.