Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

SharpYaml

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

SharpYaml

A high-performance .NET YAML library providing parsing and serialization of object graphs, NativeAOT ready.

Source
nugetNuGet
Version
3.7.1
Version published
Maintainers
1
Created
Source

SharpYaml ci NuGet

SharpYaml is a high-performance .NET YAML parser, emitter, and object serializer - NativeAOT ready.

Note: SharpYaml v3 is a major redesign with breaking changes from v2. It uses a System.Text.Json-style API with YamlSerializer, YamlSerializerOptions, and resolver-based metadata (IYamlTypeInfoResolver). See the migration guide for details.

✨ Features

  • System.Text.Json-style API: familiar surface with YamlSerializer, YamlSerializerOptions, YamlTypeInfo<T>
  • YAML 1.2 Core Schema: spec-compliant parsing with configurable schema (Failsafe, JSON, Core, Extended)
  • Source generation: NativeAOT / trimming friendly via YamlSerializerContext with [YamlSerializable] roots
  • System.Text.Json attribute interop: reuse [JsonPropertyName], [JsonIgnore], [JsonPropertyOrder], [JsonConstructor]
  • Flexible I/O: serialize/deserialize from string, ReadOnlySpan<char>, TextReader, TextWriter
  • Rich options: naming policies, indent control, null handling, duplicate key behavior, reference handling, polymorphism
  • Low-level access: full scanner, parser, emitter, and syntax tree APIs for advanced YAML processing
  • NativeAOT and trimming oriented (IsAotCompatible, IsTrimmable)

📐 Requirements

SharpYaml targets net8.0, net10.0, and netstandard2.0.

  • Consuming the NuGet package works on any runtime that supports netstandard2.0 (including .NET Framework) or modern .NET (net8.0+).
  • Building SharpYaml from source requires the .NET 10 SDK (C# 14).

📦 Install

dotnet add package SharpYaml

SharpYaml ships the source generator in-package (analyzers/dotnet/cs) - no extra package needed.

🚀 Quick Start

using SharpYaml;

// Serialize
var yaml = YamlSerializer.Serialize(new { Name = "Ada", Age = 37 });

// Deserialize
var person = YamlSerializer.Deserialize<Person>(yaml);

Options

var options = new YamlSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    WriteIndented = true,
    IndentSize = 4,
    DefaultIgnoreCondition = YamlIgnoreCondition.WhenWritingNull,
};

var yaml = YamlSerializer.Serialize(config, options);
var model = YamlSerializer.Deserialize<MyConfig>(yaml, options);

By default, PropertyNamingPolicy is null, meaning CLR member names are used as-is for YAML mapping keys (same default as System.Text.Json).

Source Generation

Declare a context with [YamlSerializable] roots:

using SharpYaml.Serialization;

[YamlSerializable(typeof(MyConfig))]
internal partial class MyYamlContext : YamlSerializerContext { }

Then consume generated metadata:

var context = MyYamlContext.Default;
var yaml = YamlSerializer.Serialize(config, context.MyConfig);
var roundTrip = YamlSerializer.Deserialize(yaml, context.MyConfig);

Cross-Project Polymorphism

When a base type lives in one assembly and the derived types live in another, you can register mappings where the composition root already references both sides.

Reflection-based serialization can register mappings at runtime:

var options = new YamlSerializerOptions
{
    PolymorphismOptions = new YamlPolymorphismOptions
    {
        DerivedTypeMappings =
        {
            [typeof(Animal)] =
            [
                new YamlDerivedType(typeof(Dog), "dog") { Tag = "!dog" },
                new YamlDerivedType(typeof(Cat), "cat") { Tag = "!cat" },
            ]
        }
    }
};

Source-generated contexts can register the same relationship at compile time:

[YamlSerializable(typeof(Zoo))]
[YamlDerivedTypeMapping(typeof(Animal), typeof(Dog), "dog", Tag = "!dog")]
[YamlDerivedTypeMapping(typeof(Animal), typeof(Cat), "cat", Tag = "!cat")]
internal partial class ZooYamlContext : YamlSerializerContext { }

Context-level mappings are additive to [YamlDerivedType] and JsonDerivedType attributes. Attribute-based entries win when the same derived type or discriminator is registered more than once.

Reflection Control

Reflection fallback can be disabled globally before first serializer use:

AppContext.SetSwitch("SharpYaml.YamlSerializer.IsReflectionEnabledByDefault", false);

When reflection is disabled, POCO/object mapping requires metadata (use generated YamlTypeInfo<T>, the overloads that accept a YamlSerializerContext, or pass MyYamlContext.Default.Options to an options-based overload). Primitive scalars and untyped containers (object, Dictionary<string, object>, List<object>, object[]) still work without reflection.

When publishing with NativeAOT (PublishAot=true), the SharpYaml NuGet package disables reflection-based serialization by default. You can override the default by setting the following MSBuild property in your app project:

<PropertyGroup>
  <SharpYamlIsReflectionEnabledByDefault>true</SharpYamlIsReflectionEnabledByDefault>
</PropertyGroup>

🚀 Benchmarks

In the included benchmarks, SharpYaml is typically ~2x to ~15x faster than YamlDotNet and uses ~2x to ~9x less memory allocations, depending on the scenario (POCO vs generic vs source-generated).

BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.7840/25H2/2025Update/HudsonValley2)
AMD Ryzen 9 7950X 4.50GHz, 1 CPU, 32 logical and 16 physical cores
.NET SDK 10.0.103
  [Host]     : .NET 10.0.3 (10.0.3, 10.0.326.7603), X64 RyuJIT x86-64-v4
  DefaultJob : .NET 10.0.3 (10.0.3, 10.0.326.7603), X64 RyuJIT x86-64-v4
TypeMethodCategoriesMeanErrorStdDevRatioRatioSDGen0Gen1Gen2AllocatedAlloc Ratio
PocoBenchmarksSharpYaml_Deserialize_PocoDeserialize_Poco1,565.4 us28.06 us26.25 us1.000.02109.375068.3594-1803.33 KB1.00
PocoBenchmarksYamlDotNet_Deserialize_PocoDeserialize_Poco3,130.9 us49.63 us46.42 us2.000.04250.0000175.7813-4135.78 KB2.29
GenericSerializationBenchmarksSharpYaml_Serialize_GenericDictionarySerialize_GenericDictionary225.2 us4.41 us4.13 us1.000.0383.252083.252083.2520277.02 KB1.00
GenericSerializationBenchmarksYamlDotNet_Serialize_GenericDictionarySerialize_GenericDictionary2,680.6 us4.37 us3.65 us11.910.21152.3438148.437574.21882579.25 KB9.31
PocoBenchmarksSharpYaml_Serialize_PocoSerialize_Poco252.0 us2.06 us1.72 us1.000.0183.007883.007883.0078292.92 KB1.00
PocoBenchmarksYamlDotNet_Serialize_PocoSerialize_Poco3,396.3 us46.01 us43.04 us13.480.19152.3438148.437574.21882529.12 KB8.63
SourceGeneratedBenchmarksSharpYaml_SourceGenerated_SerializeSerialize_SourceGenerated201.1 us3.27 us3.36 us1.000.0283.252083.252083.2520268.92 KB1.00
SourceGeneratedBenchmarksYamlDotNet_StaticGenerator_SerializeSerialize_SourceGenerated2,620.2 us20.60 us18.26 us13.030.23152.343874.218874.21882404.09 KB8.94
GenericSerializationBenchmarksSharpYaml_Serialize_StringListSerialize_StringList217.9 us2.46 us2.30 us1.000.0199.853599.853599.8535329.25 KB1.00
GenericSerializationBenchmarksYamlDotNet_Serialize_StringListSerialize_StringList3,269.2 us13.52 us12.64 us15.010.16218.7500214.8438109.37503019.75 KB9.17

📖 Documentation

Full documentation is available at https://xoofx.github.io/SharpYaml.

🪪 License

This software is released under the MIT license.

🤗 Author

Alexandre Mutel aka xoofx.

Keywords

yaml

FAQs

Package last updated on 26 Apr 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts