Skip to content

WickedFlame/YamlMap

Repository files navigation

YamlMap

Build status Build status

NuGet Version NuGet Version

Codacy Badge

Quality Gate Status Coverage

A .NET Yaml Parser.
Map Yaml to .NET objects and vice versa.

Usage

Deserialize a yaml string to a object

// Read a file using a YamlReader
var reader = new YamlReader();
var item = reader.Read<Item>(filepath);

// or

// Read a string using the static Serializer
var item = Serializer.Deserialize<Item>(yml);

Serialize a object to a yaml string

// Write a file using a YamlWriter
var writer = new YamlWriter();
writer.Write(filepath, item);

// serialize an object to string
var yml = Serializer.Serialize(item);

Implemented serialization features

  • Reading YAML strings to Tokens
  • Deserializing YAML string to a POCO
  • Writing Tokens to YAML
  • Serializing POCO to YAML

Implemented YAML features

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'

Not yet implemented YAML Features

Blocks

--- 

Inline Objects

User: {name: John Smith, age: 33}

Block statements

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

Best practice

Deserializng yaml to a Object

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.