Click here to Skip to main content
6,822,613 members and growing! (19,545 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Parsers License: The Code Project Open License (CPOL)

YAML Parser in C#

By Liu Junfeng

An almost feature complete YAML Parser
C#, .NET
Revision:4 (See All)
Posted:20 Aug 2008
Updated:10 Feb 2009
Views:19,770
Bookmarked:35 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 5.72 Rating: 4.86 out of 5

1

2

3
2 votes, 13.3%
4
13 votes, 86.7%
5

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 Official YAML Web Site for more information.

YAML Basic

One YAML file may contain zero or more YAML documents, separated by document markers.

One 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:

  • Block style item can be nested to block style item but not flow style item.
  • Flow style item can be nested to either block style or 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 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:

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:

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

License

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

About the Author

Liu Junfeng


Member

Occupation: Software Developer
Location: China China

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
GeneralWell done. PinmemberWuJunyin20:50 30 Jul '09  
GeneralDoesn't work with non-indent list Pinmembersgwong18:38 15 Jul '09  
GeneralRe: Doesn't work with non-indent list PinmemberLiu Junfeng22:59 19 Jul '09  
GeneralRe: Doesn't work with non-indent list Pinmembersgwong18:25 21 Jul '09  
GeneralThe mapping for YAML should be a dictionary instead of a List Pinmembersgwong2:19 29 Jun '09  
GeneralRe: The mapping for YAML should be a dictionary instead of a List PinmemberLiu Junfeng19:13 2 Jul '09  
GeneralRe: The mapping for YAML should be a dictionary instead of a List Pinmembersgwong18:41 15 Jul '09  
GeneralAccessing Data Items Pinmemberanandamd21:21 3 Jun '09  
GeneralRe: Accessing Data Items PinmemberLiu Junfeng20:27 7 Jun '09  
QuestionVery Nice PinmemberDuddy18:45 9 Mar '09  
GeneralYaml for .Net Pinmemberbriviere210:50 5 Nov '08  
GeneralGreat Job Pinmemberdaaharper23:34 20 Sep '08  
RantI hate recursive acronyms Pinmemberlogan133713:52 25 Aug '08  
GeneralGreat work.. Pinmemberbreakthrough613:23 22 Aug '08  
GeneralRe: Great work.. PinmemberLiu Junfeng21:27 24 Aug '08  
GeneralVery nice PinmemberJonathan C Dickinson2:29 21 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 10 Feb 2009
Editor: Deeksha Shenoy
Copyright 2008 by Liu Junfeng
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project