YAML to C#

Generate C# classes or records from a YAML sample.

YAML to C# – Typed classes for .NET configuration

The YAML to C# converter turns a YAML document into ready-to-compile C# classes or records. Paste an appsettings.yaml, a Kubernetes manifest, a CI pipeline or any API sample and get a model you can deserialize with YamlDotNet, or with System.Text.Json after converting the YAML to JSON.

How YAML maps to C# types

The input is parsed with the YAML 1.2 core schema. Mappings become classes, sequences become List<T> (or arrays if you prefer), whole numbers become int or long, decimals become double, and true/false become bool. Anchors and aliases are resolved first and comments are dropped. When a list holds several mappings, their keys are merged into one class, and keys that are missing in some items become nullable properties.

Options

Set the root class name and the namespace, pick the attribute flavour – [JsonPropertyName] for System.Text.Json, [JsonProperty] for Newtonsoft.Json or XmlSerializer attributes – and switch to positional records for immutable settings objects. Enable DateTime for timestamps to type ISO 8601 strings as DateTime instead of string.

Using the output with YamlDotNet

Create a DeserializerBuilder, choose the naming convention that matches your keys (for example CamelCaseNamingConvention.Instance) and call Deserialize<Order>(yaml). Because property names are generated in PascalCase from the original keys, no extra mapping is required for camelCase or snake_case files.

Typical uses

Typed configuration for worker services and console tools, reading Docker Compose or GitHub Actions files in build tooling, Kubernetes operators written in .NET, and replacing Dictionary<string, object> lookups with IntelliSense-friendly properties.

Tips

Include several list items when keys vary between entries, so optional members are detected. Durations such as 30s and version strings stay string; convert them yourself if you need TimeSpan or Version.

Check your input with the YAML Validator, convert it with YAML to JSON, or start from JSON with JSON to C#.

Frequently Asked Questions

Yes. YamlDotNet maps properties by name, so the generated PascalCase properties work with its CamelCaseNamingConvention or UnderscoredNamingConvention. The JSON attributes are ignored by YamlDotNet and do no harm.

A key that is null in the sample, or that is missing from some items of a list, may be absent at runtime. Those properties get a nullable type such as bool? or string? so the compiler reminds you to check them. A key that is only ever null becomes object? because its real type is unknown.

Records are concise and immutable, which suits configuration that is loaded once. Classes with get/set properties are easier to bind with the Options pattern in ASP.NET Core, which needs settable properties.

No. Parsing and code generation happen entirely in your browser, so secrets in a configuration file never leave your machine.