Recursive AI

Control YAML property order in C#

OneYaml is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

With YamlPropertyOrder, lower numbers come first. Properties with no order attribute default to 0.

Terminal
dotnet add package OneYaml

The library targets .NET 8 and later; the example project uses .NET 10.

C#
using OneYaml;
 
var job = new Job("daily", 3, "15 2 * * *");
Console.Write(YamlSerializer.Serialize(job));
 
record Job(
    [property: YamlPropertyOrder(-1)]
    string Name,
    int Retries,
    [property: YamlPropertyOrder(-2)]
    string Schedule);

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

The snippet prints schedule first even though Schedule is declared last:

YAML
schedule: 15 2 * * *
name: daily
retries: 3

Variations

Put a field at the end. Replace the Job record with this version. Schedule has order 1; the other two properties default to 0, so schedule comes after both of them.

C#
record Job(string Name, int Retries,
    [property: YamlPropertyOrder(1)]
    string Schedule);
YAML
name: daily
retries: 3
schedule: 15 2 * * *

The short answer

Use [YamlPropertyOrder(n)] on the members you want to move, then call YamlSerializer.Serialize. A member with no order attribute has order 0, unless it has a JsonPropertyOrder attribute. In the example, Schedule (-2) comes before Name (-1) and both come before Retries (0). The numbers are sort keys, not output line numbers, so there’s no need to number every property or to use consecutive values.

How do I change the order of properties when serializing YAML in C#?

For a class property, put the attribute immediately above its declaration. On a positional record, use property: as shown so the attribute applies to the generated property rather than the constructor parameter. Choose negative values for a short header you always want near the top and positive values for fields you want below the ordinary settings. In this example, ties keep declaration order: give both Name and Schedule order -1 and name is written first.

The attribute changes the order in which the serializer writes siblings in the same YAML mapping. It doesn’t rename them: the example still emits camel-case keys. If the YAML key itself must change, that’s a separate decision made with YamlPropertyName or a naming policy. This is also a writing option, not a constraint on the order of keys in input. The YAML specification treats mapping order as a serialization detail, so use it for readable output rather than to express which setting takes precedence.

Can I use JsonPropertyOrder to set the YAML property order too?

Yes. OneYaml honors System.Text.Json.Serialization.JsonPropertyOrder when the property has no YamlPropertyOrder. If the model is written to both formats and the keys should appear in the same order, one JSON attribute is enough. If the YAML file needs a different order, use both attributes on that member: OneYaml uses the YAML attribute for its output. See the serializer’s attribute table for the other settings that work this way.

All oneyaml C# recipes