Set the number of spaces OneYaml adds at each level when it writes YAML.
dotnet add package OneYamlThe library targets .NET 8 and later; the example project uses .NET 10.
using OneYaml; var config = new { Web = new { Port = 8080 } }; var options = new YamlSerializerOptions { IndentSize = 4 }; Console.Write(YamlSerializer.Serialize( config, options));
Unzip the example, then dotnet run.
Prints port indented by four spaces under web:
web: port: 8080
Web and Port become web and port because OneYaml writes property names in camelCase by default (see PropertyNamingPolicy).
The full program
The download writes the same configuration twice, with a list nested under routes so you can see the indentation at two levels.
using OneYaml; var config = new { Web = new { Port = 8080, Routes = new[] { "/", "/health" } } }; Console.WriteLine("Default (2 spaces):"); Console.Write(YamlSerializer.Serialize(config)); var options = new YamlSerializerOptions { IndentSize = 4 }; Console.WriteLine("Custom (4 spaces):"); Console.Write(YamlSerializer.Serialize(config, options));
Default (2 spaces): web: port: 8080 routes: - / - /health Custom (4 spaces): web: port: 8080 routes: - / - /health
A value outside 1–8 throws ArgumentOutOfRangeException when you create the options, before serialization starts.
Variations
Keep the default. Replace the options setup and write call in the snippet at the top with:
Console.Write(YamlSerializer.Serialize(config));web: port: 8080
Related recipes
The short answer
Set YamlSerializerOptions.IndentSize to the number of spaces to add at each level, then pass the options to YamlSerializer.Serialize. OneYaml’s serializer options allow 1 through 8; the default is 2. This is an output setting. You don’t need to match it when you read the result.
How should indentation be used in YAML files?
Indentation groups keys and lists. In the example, port and routes start in the same column because both belong directly to web. The list markers start farther in because they belong to routes. If you move port all the way left, it becomes a top-level key alongside web: that’s a change to the data, not just its appearance.
The YAML 1.2.2 indentation rules require siblings in a block to share an indentation level and let deeper indentation mark children. Use spaces, not tabs, for indentation. That restriction is on the whitespace that marks structure, not on every tab character that might appear inside a string value.
How many spaces should you indent YAML: two or four?
Both work for the configuration above. Match the convention of the files you already generate. With two spaces per level, the list markers in the example are four spaces in from the left; with four per level they’re eight spaces in. Setting IndentSize = 4 does not add four spaces to every line: top-level keys still start at the left margin, and each level adds four more spaces.
The option affects new output from the serializer. If your input is an existing YAML file, serializing the data again doesn’t preserve the original whitespace or comments. For a targeted change that should leave the surrounding text alone, use OneYaml’s document editing API rather than treating reserialization as a formatting-only edit.