---
title: "Serialize YAML keys in kebab-case in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/serialize-yaml-keys-in-kebab-case/
description: "Use OneYaml to write C# properties as lowercase, hyphenated YAML keys and read them back with the same naming policy. No per-property attributes needed."
version: "1.2.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/OneYaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-csharp) · [Docs](https://recursiveai.org/oneyaml/csharp/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/csharp/cookbook/serialize-yaml-keys-in-kebab-case/serialize-yaml-keys-in-kebab-case.zip)

`KebabCaseLower` turns `PoolName` into `pool-name`.

```shell
dotnet add package OneYaml
```

The library targets .NET 8 and later; the example project uses [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0).

```csharp
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](https://recursiveai.org/oneyaml/csharp/cookbook/serialize-yaml-keys-in-kebab-case/serialize-yaml-keys-in-kebab-case.zip), 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:

```csharp
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
```

```text
pool_name: web
max_connections: 100
True
```

## Related recipes

- [Rename one YAML property with an attribute](https://recursiveai.org/oneyaml/csharp/cookbook/rename-yaml-property-with-attribute/)
- [Reject unknown YAML properties](https://recursiveai.org/oneyaml/csharp/cookbook/reject-unknown-yaml-properties/)
- [Serialize YAML enum values as strings](https://recursiveai.org/oneyaml/csharp/cookbook/serialize-yaml-enums-as-strings/)

## 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](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties#use-a-built-in-naming-policy) and [OneYaml’s serializer options](https://recursiveai.org/oneyaml/csharp/docs/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`:

```csharp
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](https://recursiveai.org/oneyaml/csharp/docs/serializer/#attributes).
