Click here to Skip to main content
15,890,512 members
Articles / Programming Languages / C#

YAML Parser in C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (26 votes)
17 Feb 2011CPOL2 min read 330K   4.5K   70   39
An almost feature complete YAML parser.

Image 1

Image 2

Introduction

YAML is a human-friendly, cross language, Unicode based data serialization language designed around the common native data types of agile programming languages. It is broadly useful for programming needs ranging from configuration files to Internet messaging to object persistence to data auditing.

Visit the official YAML website for more information.

YAML Basics

A YAML file may contain zero or more YAML documents, separated by document markers. A YAML document contains one root DataItem. There are three types of DataItems: Scalar, Sequence, and Mapping. DataItems may be nested to form structured data.

Each DataItem type has several formatting styles for good human readability.

yamlDataItem.png

Some rules:

  • A block style item can be nested to a block style item but not a flow style item.
  • A flow style item can be nested to either a block style or a flow style item.
  • Block structure is denoted by indentation.
  • All indentations only use space char, tab is not allowed.

Here are some examples:

Block Scalar

Literal Text
|
The text using
literal style.
|-
The text using
literal style.
"The text using\nliteral style.\n""The text using\nliteral style."
Folded Text
>
The text using
folded style.
>-
The text using
folded style.
"The text using folded style.\n""The text using folded style."
Flow Scalar
Plain Text
  • Can not start with ,[]{}#&*!|>'\"%@`
  • Can start with -?: followed by non space char
  • ": " and " #" cannot appear in between
'Single Quoted Text'
  • Line breaks are folded
  • "'" is escaped with "''"
"Double Quoted Text"
  • Line breaks are folded
  • Escape sequences can be used
Sequence
Block Sequence
- Item one
- Item two
- Item three
Flow Sequence
[Item one, Item two,
Item three]
Mapping
Block Mapping
Key1: Item one
Key2: Item two
? Key3
: Item three 
Flow Mapping
{Key1: Item one, Key2: Item two, 
Key3: Item three} 

Other

Anchor and Alias
Key1: &items
  A: Item A
  B: Item B
Key2: *items
Key1:
  A: Item A
  B: Item B
Key2:
  A: Item A
  B: Item B
Comment
# whole line comment
Data Item # inline comment

Background

There is already a Yaml Library for .NET project, but the features supported are limited.

Using the Code

The parser code is generated using a homemade tool based on grammar specified in the YAML.PEG.txt file. This grammar is not completely equal to the official YAML specification. Here are some differences:

A separator “,” is not allowed following the last entry of Sequence or Mapping in this parser. The 32-bit Unicode escape sequence “U” (ns-hex-digit × 8) is not supported.

The parser can be used like this:

C#
YamlParser parser = new YamlParser();
TextInput input = new TextInput(File.ReadAllText(yamlFilePath));
bool success;
YamlStream yamlStream = parser.ParseYamlStream(input, out success);
if (success)
{
    foreach (YamlDocument doc in yamlStream.Documents)
    {
        // access DataItem by doc.Root
    }
}
else
{
    MessageBox.Show(parser.GetEorrorMessages());
}

Or:

C#
YamlStream yamlStream = YamlParser.Load(yamlFilePath);

Points of Interest

The main shortcoming of this parser is that error messages are not intuitive. You are welcome to give suggestions.

History

  • 2008-08-21: Article submitted.
  • 2011-02-16: Redesigned the UI, fixed some parser bugs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: The mapping for YAML should be a dictionary instead of a List Pin
sgwong15-Jul-09 17:41
sgwong15-Jul-09 17:41 
GeneralAccessing Data Items Pin
anandamd3-Jun-09 20:21
anandamd3-Jun-09 20:21 
GeneralRe: Accessing Data Items Pin
Liu Junfeng7-Jun-09 19:27
Liu Junfeng7-Jun-09 19:27 
QuestionVery Nice Pin
Duddy9-Mar-09 17:45
Duddy9-Mar-09 17:45 
GeneralYaml for .Net Pin
briviere25-Nov-08 9:50
briviere25-Nov-08 9:50 
GeneralGreat Job Pin
daaharper20-Sep-08 22:34
daaharper20-Sep-08 22:34 
RantI hate recursive acronyms Pin
chaiguy133725-Aug-08 12:52
chaiguy133725-Aug-08 12:52 
GeneralGreat work.. Pin
breakthrough622-Aug-08 12:23
breakthrough622-Aug-08 12:23 
This is really needed, and I can't believe you posted this yesterday, talk about timing.

What would really be a nice addition is to tie this up into a separate assembly (just the parser), and extend it so it can create a traversable object in .NET, similar to how YAML bindings tied into the object layer of Ruby for instance.

Seems it would be possible to iterate through the YAML document, and generate a list of "document" objects, with accessors to drill into the associated lists (collections), maps, etc.

Guess I'm wondering if it's then possible to do something like this..

YamlDocuments docs = new YamlParser.loadYaml(file);

// test for any documents in the YAML file
if (docs.count())
{
// get the first document
YamlDocument doc = docs[0];

// access any mappings defined at this layer
if (doc.hasMapping)
foreach(string key in doc.keys)
{
// grab the object (which is the value)
YamlObject result = doc[key];
// cast it to the data type desired, or perform some automatic casting
string value = result.value.toString();
}
}

In theory, if you knew it loaded up perfectly, you could access the 3rd item in a mapping keyed by "name" in the first document like this..

string item = docs[0]["name"][2];

even if the syntax wasn't quite so great...

string item = docs[0].mapping["name"].sequence(2).toString();

... similarly, it would allow you to iterate into collections, just as easily as maps. Essentially, it would be pre-loaded in the object layer.

Any thoughts?
GeneralRe: Great work.. Pin
Liu Junfeng24-Aug-08 20:27
Liu Junfeng24-Aug-08 20:27 
GeneralVery nice Pin
Jonathan C Dickinson21-Aug-08 1:29
Jonathan C Dickinson21-Aug-08 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.