Recursive AI

Serialize YAML keys in kebab-case in C#

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

KebabCaseLower turns PoolName into pool-name.

Terminal
dotnet add package OneYaml

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

C#
using OneYaml;
using System.Text.Json;
 
var options = new YamlSerializerOptions
{
    PropertyNamingPolicy =
        JsonNamingPolicy.KebabCaseLower
};
var pool = new Pool("web", 100);
var yaml = YamlSerializer.Serialize(
    pool, options);
Console.Write(yaml);
var read = YamlSerializer.Deserialize<Pool>(
    yaml, options);
Console.WriteLine(read == pool);
 
record Pool(string PoolName,
    int MaxConnections);

Unzip the example, then dotnet run.

Text
pool-name: web
max-connections: 100
True

True means the record read back matches the original. Use the same options when reading and writing. With the default naming policy, OneYaml expects maxConnections instead. An unmatched key is skipped by default; set UnmappedMemberHandling to Disallow if a spelling mistake should raise a YamlException.

Variations

To write max_connections instead, replace the PropertyNamingPolicy assignment with:

C#
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
Text
pool_name: web
max_connections: 100
True

The short answer

Set YamlSerializerOptions.PropertyNamingPolicy to JsonNamingPolicy.KebabCaseLower and pass the options to YamlSerializer.Serialize and YamlSerializer.Deserialize. This maps the C# names PoolName and MaxConnections to pool-name and max-connections in YAML. The C# record doesn’t change.

How do you change YAML property names for a whole C# model?

Use a naming policy when the same convention applies to the model instead of repeating an attribute on every property. OneYaml uses the policy for both the keys it writes and the keys it looks up when reading. JsonNamingPolicy comes from .NET’s System.Text.Json namespace; OneYaml uses it to convert names, not to convert the file to JSON. See Microsoft’s table of built-in naming policies and OneYaml’s serializer options.

The default is camel case: PoolName becomes poolName. If the YAML contract uses the original C# names, set the policy to null. If it uses hyphens, use the kebab-case policy rather than case-insensitive matching: ignoring capitalization doesn’t turn poolName into pool-name.

How do you give one YAML property a different name?

Use [YamlPropertyName] when one key has to keep a name that doesn’t follow the rule. For example, if an existing config calls the limit capacity, replace the Pool declaration in the snippet with this one. PoolName still follows the policy, but MaxConnections writes and reads capacity:

C#
record Pool(string PoolName,
    [property: YamlPropertyName("capacity")]
    int MaxConnections);

The property: target puts the attribute on the property generated by the positional record. For an ordinary class you can put the attribute directly above the property. See OneYaml’s attribute reference.

All oneyaml C# recipes