5,693,062 members and growing! (20,295 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# (C# 1.0, C# 2.0, C# 3.0, C#), .NET

Posted: 20 Aug 2008
Updated: 20 Aug 2008
Views: 6,248
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 5.00 Rating: 4.64 out of 5
1 vote, 8.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 8.3%
4
10 votes, 83.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 Bisic

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 humun 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 indentation 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 " #" can not 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 last entry of Sequence or Maping 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 shortcomming of this parser is that error messages are not intuitive. 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



    Occupation: Software Developer
    Location: China China

    Other popular Algorithms & Recipes articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
     Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
    GeneralYaml for .Netmemberbriviere210:50 5 Nov '08  
    GeneralGreat Jobmemberdaaharper23:34 20 Sep '08  
    RantI hate recursive acronymsmemberlogan133713:52 25 Aug '08  
    GeneralGreat work..memberbreakthrough613:23 22 Aug '08  
    GeneralRe: Great work..memberLiu Junfeng21:27 24 Aug '08  
    GeneralVery nicememberJonathan C Dickinson2:29 21 Aug '08  

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

    PermaLink | Privacy | Terms of Use
    Last Updated: 20 Aug 2008
    Editor:
    Copyright 2008 by Liu Junfeng
    Everything else Copyright © CodeProject, 1999-2008
    Web18 | Advertise on the Code Project