YAML to C# Class Generator
CV-87Paste a YAML document and press Convert to get classes with properties and JSON attributes. The YAML is read into JSON first, so the types come from its values, and it runs in your browser.
Runs in your browser. Nothing is uploaded.
YAML
C#
How to use it
- Paste one YAML document, or open a .yaml or .yml file. Use a sample that shows every section you expect.
- Pick System.Text.Json or Newtonsoft.Json for the attributes, and turn nullable reference types on or off.
- Press Convert, then copy the result or download it as a .cs file.
- Deserialize with JsonSerializer.Deserialize<Root>(json) for System.Text.Json or JsonConvert.DeserializeObject<Root>(json) for Newtonsoft.Json. The names follow the JSON form of your YAML, so add the tags your YAML library needs if you load YAML directly.
How the classes are built
Each object becomes a public class with { get; set; } properties written in PascalCase. Arrays become List<T>, with the item type worked out from every item rather than only the first. A key that is null or missing in some items gets a nullable type such as int?, and string? when nullable reference types are on; mixed types and empty arrays become object. When a property name is not an exact copy of the key, an attribute carries the original key: [JsonPropertyName] for System.Text.Json or [JsonProperty] for Newtonsoft.Json.
How the YAML becomes classes
The YAML is parsed with the core schema, converted to JSON, and the code is built from that JSON. true/false, null, integers and floats get their usual types, while dates stay strings. Anchors, aliases and << merge keys are resolved, so the values they point to appear in the generated types. Comments are dropped, only one document is read, and keys that are not text become text. Keys that are not valid C# names, such as first-name, are renamed, and the original key is kept in the [JsonPropertyName] or [JsonProperty] attribute.
What a sample cannot tell it
The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay string; change them to DateTime or DateTimeOffset by hand. A YAML file with optional sections should be pasted with every section present, or the missing sections will not appear in the output.
Questions people ask
How do I convert YAML to a C# class?
Paste the YAML into the input box, choose your options and press Convert to get the classes. Deserialize with JsonSerializer.Deserialize<Root>(json) for System.Text.Json or JsonConvert.DeserializeObject<Root>(json) for Newtonsoft.Json. The classes follow the JSON form of the YAML. The conversion happens in your browser.
Why is a large number written as double in C#?
A whole number beyond the range of long has no exact simple C# integer type, so it becomes double, which can round it, and the line gets a comment. If you need every digit, change the property to decimal or string by hand. This applies to the JSON form of your YAML.
Does it turn date text into DateTime in C#?
No. Strings stay string, even when they look like an ISO date or a GUID. Change those properties to DateTime, DateTimeOffset or Guid yourself if you want typed values; the rest of the class stays as generated. This applies to the JSON form of your YAML.
Should I choose System.Text.Json or Newtonsoft.Json for the C# output?
Choose the library your project already uses. The only difference in the output is the attribute that carries the original key: [JsonPropertyName("key")] for System.Text.Json and [JsonProperty("key")] for Newtonsoft.Json. The class and property shapes are the same. This applies to the JSON form of your YAML.
Does it keep YAML comments, anchors and merge keys?
Comments are dropped because they are not data. Anchors, aliases and << merge keys are resolved, so the values they point to appear in the types. A document that contains an alias to itself is rejected with an error. The C# classes are written from that converted data.
Why did a YAML value like 1.0 come out as an integer type?
The YAML is converted to JSON before types are chosen, and 1.0 is the same number as 1 there. A field that always holds whole-looking decimals in your sample can come out as an integer type. Add a non-whole decimal to the sample, or change the type by hand. The C# classes are written from that converted data.
About this tool
The YAML is read with the core schema (true/false, null, integers and floats), converted to JSON, and the code is generated from that. Dates stay text, comments are dropped, anchors, aliases and << merge keys are resolved, keys that are not text become text, and only one document is read. A value such as 1.0 is read as the number 1, so a field that holds whole-looking decimals can come out as an integer type; an integer beyond 9007199254740991 is rounded by the YAML reader before any code is written; .nan and .inf become null. Every object becomes a class with { get; set; } properties in PascalCase, and an attribute keeps the original key whenever the property name differs ([JsonPropertyName] for System.Text.Json or [JsonProperty] for Newtonsoft.Json). Whole numbers become int, or long beyond 32 bits; decimals become double. A whole number beyond long also becomes double and can round, and a comment marks the line. Arrays become List<T>. A key that is null or missing in some items gets a nullable type, and mixed types, always-null keys and empty arrays become object. It does not add validation attributes or date types. Everything runs in your browser; nothing is uploaded.