A .NET Yaml Parser.
Map Yaml to .NET objects and vice versa.
// Read a file using a YamlFileReader
var reader = new YamlFileReader();
var item = reader.Read<Item>(filepath);
// or
// Read a string using the YamlReader
var reader = new YamlReader();
var item = reader.Read<Item>(yaml);
// or
// Read a string using the static Serializer
var item = Serializer.Deserialize<Item>(yaml);
// Write a file using a YamFilelWriter
var writer = new YamlFileWriter();
writer.Write(filepath, item);
// or
// Serialize an object to a string using a YamlWriter
var writer = new YamlWriter();
writer.Write(item);
// or
// serialize an object to string using the static Serializer
var yml = Serializer.Serialize(item);
- Reading YAML strings to Tokens
- Deserializing YAML string to a POCO
- Writing Tokens to YAML
- Serializing POCO to YAML
Comments
# This is a comment
Properties
Property: Value
Objects
User:
name: John Smith
age: 33
Lists
Movies:
- Casablanca
- Spellbound
- Notorious
Inline Lists
Movies: [Casablanca, Spellbound, Notorious]
Multiline Lists
Movies: [Casablanca,
Spellbound, Notorious]
Quotations
Movies: "Casablanca, Spellbound, Notorious"
Drive: 'c: is a drive'
Block statements (multiline strings)
Text: |
There was a young fellow of Warwick
Who had reason for feeling euphoric
For he could, by election
Have triune erection
Ionic, Corinthian, and Doric
WrappedText: >
Wrapped text
will be folded
into a single
paragraph
Blank lines denote
paragraph breaks
Pitfalls with Plain flow scalars:
- Colons (:) can be contained in a string, but are not allowed before a space or newline
Blocks
---
Inline Objects
User: {name: John Smith, age: 33}
Block statements with strip or keep newlines at end
Text: |+
There was a young fellow of Warwick
Text: |-
There was a young fellow of Warwick
WrappedText: >+
Wrapped text
WrappedText: >-
Wrapped text
To deserialize a Yaml to a object, it is best to have a parameterless constructor.
If a object needs to have a constructor with parameters, the parameter names have to be the same as the properties that they are mapped to.
Else YamlMap will not be able to map the correct values to the parameters.