OneYaml for C#, a YAML parser and serializer for .NET, is free and open source from Recursive AI, under MIT or Apache-2.0. NuGet · GitHub
dotnet add package OneYaml.Compat.SharpYaml --version 1.2.1
using System.Text.Json; using OneYaml.Compat.SharpYaml; var options = new YamlSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var job = YamlSerializer.Deserialize<Job>("name: backup\nretries: 2\n", options)!; Console.Write(YamlSerializer.Serialize(job with { Retries = 3 }, options)); record Job(string Name, int Retries);
name: backup retries: 3
Swap the package and imports
Use a separate .NET 10 console project for each example. To switch an existing project, remove its direct SharpYaml reference with dotnet remove package SharpYaml, then run the adapter install command above. If another library exposes SharpYaml types, it still needs the original package: the adapter’s types are not interchangeable with them.
The example was tested against SharpYaml 3.14.0, the baseline in the OneYaml compatibility notes. This is not a migration from SharpYaml 2’s API; see SharpYaml’s v2-to-v3 guide if that’s where you’re starting.
| SharpYaml 3.14.0 | OneYaml adapter 1.2.1 |
|---|---|
using SharpYaml; | using OneYaml.Compat.SharpYaml; |
using SharpYaml.Serialization; | using OneYaml.Compat.SharpYaml.Serialization; |
YamlSerializer.Serialize, Deserialize, TryDeserialize | Same names for supported serializer calls. The example keeps the record, generic call, and camel-case naming policy. |
Attributes such as YamlPropertyName, YamlIgnore, and YamlRequired | Same names in the adapter’s Serialization namespace. Use the adapter types on your models. |
YamlException, SyntaxErrorException, SemanticErrorException | Types exist, but the adapter throws SyntaxErrorException for syntax errors and YamlException for other YAML errors. Don’t rely on a SemanticErrorException catch alone. |
Check what compiles and what runs
The adapter has no custom converters, IYamlOn* hooks, serializer polymorphism or extension data, or Model and Syntax APIs. A context declaration can compile without generating Ctx.Default or typed metadata properties. Check the full list of omissions before swapping packages.
Other settings compile but throw NotSupportedException at first use. Those include a non-Core schema, DictionaryKeyPolicy, and DuplicateKeyHandling set to LastWins or FirstWins. For example:
using OneYaml.Compat.SharpYaml; using OneYaml.Compat.SharpYaml.Serialization; var options = new YamlSerializerOptions { DuplicateKeyHandling = YamlDuplicateKeyHandling.LastWins }; try { YamlSerializer.Deserialize<object>("retries: 2\nretries: 3\n", options); } catch (NotSupportedException error) { Console.WriteLine(error.Message); }
DuplicateKeyHandling is not supported by OneYaml.Compat.SharpYaml. Leave it at its default value. See docs/compat/sharpyaml.md for what is and what is not covered.Run your own fixtures for duplicate keys, numeric overflow, and any byte-for-byte YAML snapshots. The adapter writes whole floats with a fraction and can quote YAML 1.1 look-alikes such as on. For input you don’t control, review OneYaml’s read limits, including the adapter-only Limits option.
Move to the native OneYaml API when the adapter doesn’t fit
For new code, start with the native serializer. Port converters and generated contexts rather than carrying their SharpYaml types over. If you’re editing an existing file rather than serializing a model, use YamlDocument to keep its comments. Here the inline comment stays in place when the retry count changes:
dotnet add package OneYaml --version 1.2.1
using OneYaml; using var doc = YamlDocument.Parse("name: backup\nretries: 2 # retry budget\n"); doc.Root["retries"] = 3; Console.Write(doc);
name: backup retries: 3 # retry budget
See the OneYaml migration guide for native serializer mappings and the editing guide for document operations.