Format YAML in C#
The code for the options above. It gives the same output as the page:
The Parser reads the YAML into events (mapping start, scalar, comment and so on) and the Emitter writes them back out. Nothing is converted to .NET objects on the way, so values keep their exact text: 0755 stays 0755 and 1.10 stays 1.10.
What the formatter changes
- Indentation becomes the width you pick, at every level.
- List items start at the same column as their key (
ports:then- 80directly below), the waykubectlwrites YAML. Tick the option to indent them instead. - Spacing is normalized: one space after
:and-, and[a, b]and{a: 1}with one space after each comma. - Flow collections (
[ ]and{ }) stay flow collections on one line. With a line width, long ones wrap. - Quotes stay as written, and unquoted values stay unquoted.
- Block scalars (
|and>) are kept. Their indentation and chomping indicators (|-,|+,|2) are written from the value. - Anchors, aliases and tags are kept. Tags are written in short form:
tag:yaml.org,2002:strbecomes!!str. - Documents:
---,...and%YAMLdirectives are written when the input has them.
Comments
By default YamlDotNet's parser skips comments. With skipComments: false they become events that the Emitter writes back, one space after #. Two things to know:
- Comments inside
[ ]and{ }are dropped. - With comments turned on, YamlDotNet's parser rejects some valid YAML. The common case is a comment on the same line as a value with an anchor, such as
port: &port 8080 # default. The page tells you when that happens, and unticking "Keep comments" formats the file without them.
The page also reads its output back. In rare cases the Emitter writes a block scalar that reads back differently, and the page warns you instead of hiding it.
FAQ
Does formatting change my data?
It should not, and the page checks: it parses the output again and compares every value, anchor and tag with the input. If they differ, you get a warning.
Can I sort the keys?
No. The Emitter writes events in the order the parser reads them. To sort keys, deserialize to a dictionary, sort it and serialize it with YamlDotNet's Serializer. That drops comments.
Why use this instead of a Prettier-style formatter?
If your .NET code writes YAML with YamlDotNet, this shows exactly what that code produces, so formatted files and generated files look the same.
How do I format YAML in C#?
Read the YAML with YamlDotNet's Parser and pass every event to an Emitter that writes to a StringWriter. EmitterSettings sets the indentation, the line width and whether list items are indented under their key.
Can I keep the comments when I format YAML with YamlDotNet?
Yes. Create the parser with new Scanner(reader, skipComments: false). The comments become Comment events, and the Emitter writes them back, except comments inside flow collections ([ ] and { }).
Why are my list items not indented under their key?
YamlDotNet's Emitter writes "- " at the same indentation as the parent key by default, like kubectl does. Pass indentSequences: true in EmitterSettings (or WithIndentedSequences()) to indent them.
Is my YAML uploaded?
No. The formatter runs in your browser.