Click here to Skip to main content
11,643,493 members (73,740 online)
Click here to Skip to main content

YAML Parser in C#

, 17 Feb 2011 CPOL 102K 2.4K 62
Rate this:
Please Sign up or sign in to vote.
An almost feature complete YAML parser.

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:

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.
  • 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)

Share

About the Author

Liu Junfeng
Software Developer (Senior) Beyondsoft SH
China China
No Biography provided

You may also be interested in...

Comments and Discussions

 
BugPossible NullReferenceException in places Pin
Austin Stephens20-Nov-13 13:58
memberAustin Stephens20-Nov-13 13:58 
GeneralMy vote of 5 Pin
CaptainClueless6-Apr-13 12:07
memberCaptainClueless6-Apr-13 12:07 
QuestionComplement to the YAML Parser Pin
CaptainClueless6-Apr-13 11:18
memberCaptainClueless6-Apr-13 11:18 
AnswerRe: Complement to the YAML Parser Pin
CaptainClueless6-Apr-13 12:15
memberCaptainClueless6-Apr-13 12:15 
QuestionHow do I get the data out of the tree? Pin
CaptainClueless6-Apr-13 10:54
memberCaptainClueless6-Apr-13 10:54 
QuestionNew to YAML Pin
Hanibani27-Sep-12 6:03
memberHanibani27-Sep-12 6:03 
Question谢谢你! Pin
sebgod28-Dec-11 21:37
membersebgod28-Dec-11 21:37 
你做的一个非常棒的软件!!!你好厉害啊!
Smile | :)
SuggestionVery good Library Pin
Madhur Ahuja30-Aug-11 0:50
memberMadhur Ahuja30-Aug-11 0:50 
GeneralCould you release this under GPL, LGPL, or BSD licence please? Pin
aj_codeproject26-May-11 3:43
memberaj_codeproject26-May-11 3:43 
GeneralRe: Could you release this under GPL, LGPL, or BSD licence please? Pin
Member 393683426-May-11 5:26
memberMember 393683426-May-11 5:26 
GeneralRe: Could you release this under GPL, LGPL, or BSD licence please? Pin
Liu Junfeng4-Jun-11 2:02
memberLiu Junfeng4-Jun-11 2:02 
GeneralRe: Could you release this under GPL, LGPL, or BSD licence please? Pin
aj_codeproject22-Jun-11 15:17
memberaj_codeproject22-Jun-11 15:17 
GeneralNearly Perfect [modified] Pin
Mercury Phlegmatic20-Mar-11 9:57
memberMercury Phlegmatic20-Mar-11 9:57 
GeneralThis parser seems good... Pin
aamironline18-Feb-11 1:39
memberaamironline18-Feb-11 1:39 
QuestionProblem with folded text scalar Pin
Anders Jørgensen20-Jan-11 2:40
memberAnders Jørgensen20-Jan-11 2:40 
AnswerRe: Problem with folded text scalar Pin
Anders Jørgensen20-Jan-11 3:24
memberAnders Jørgensen20-Jan-11 3:24 
AnswerRe: Problem with folded text scalar Pin
Liu Junfeng17-Feb-11 14:48
memberLiu Junfeng17-Feb-11 14:48 
GeneralRe: Problem with folded text scalar Pin
Anders Jørgensen17-Feb-11 20:43
memberAnders Jørgensen17-Feb-11 20:43 
GeneralNew to YAML Pin
RKO10020-Aug-10 2:42
memberRKO10020-Aug-10 2:42 
GeneralNested sequences not working Pin
Member 293651324-Mar-10 12:35
memberMember 293651324-Mar-10 12:35 
GeneralRe: Nested sequences not working Pin
Liu Junfeng20-Jul-10 0:44
memberLiu Junfeng20-Jul-10 0:44 
GeneralWell done. Pin
WuJunyin30-Jul-09 19:50
memberWuJunyin30-Jul-09 19:50 
GeneralDoesn't work with non-indent list Pin
sgwong15-Jul-09 17:38
membersgwong15-Jul-09 17:38 
GeneralRe: Doesn't work with non-indent list Pin
Liu Junfeng19-Jul-09 21:59
memberLiu Junfeng19-Jul-09 21:59 
GeneralRe: Doesn't work with non-indent list Pin
sgwong21-Jul-09 17:25
membersgwong21-Jul-09 17:25 
GeneralRe: Doesn't work with non-indent list Pin
iamcharliemouse23-May-11 5:55
memberiamcharliemouse23-May-11 5:55 
GeneralThe mapping for YAML should be a dictionary instead of a List Pin
sgwong29-Jun-09 1:19
membersgwong29-Jun-09 1:19 
GeneralRe: The mapping for YAML should be a dictionary instead of a List Pin
Liu Junfeng2-Jul-09 18:13
memberLiu Junfeng2-Jul-09 18:13 
GeneralRe: The mapping for YAML should be a dictionary instead of a List Pin
sgwong15-Jul-09 17:41
membersgwong15-Jul-09 17:41 
GeneralAccessing Data Items Pin
anandamd3-Jun-09 20:21
memberanandamd3-Jun-09 20:21 
GeneralRe: Accessing Data Items Pin
Liu Junfeng7-Jun-09 19:27
memberLiu Junfeng7-Jun-09 19:27 
QuestionVery Nice Pin
Duddy9-Mar-09 17:45
memberDuddy9-Mar-09 17:45 
GeneralYaml for .Net Pin
briviere25-Nov-08 9:50
memberbriviere25-Nov-08 9:50 
GeneralGreat Job Pin
daaharper20-Sep-08 22:34
memberdaaharper20-Sep-08 22:34 
RantI hate recursive acronyms Pin
logan133725-Aug-08 12:52
memberlogan133725-Aug-08 12:52 
GeneralGreat work.. Pin
breakthrough622-Aug-08 12:23
memberbreakthrough622-Aug-08 12:23 
GeneralRe: Great work.. Pin
Liu Junfeng24-Aug-08 20:27
memberLiu Junfeng24-Aug-08 20:27 
GeneralVery nice Pin
Jonathan C Dickinson21-Aug-08 1:29
memberJonathan C Dickinson21-Aug-08 1:29 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 17 Feb 2011
Article Copyright 2008 by Liu Junfeng
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid