![]() |
General Programming »
Algorithms & Recipes »
Parsers
License: The Code Project Open License (CPOL)
YAML Parser in C#By Liu JunfengAn almost feature complete YAML Parser |
C#, .NET
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
Some rules:
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
|
|
'Single Quoted Text'
|
|
"Double Quoted Text"
|
|
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
|
There is already a Yaml Library for .NET project, but the features supported are limited.
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);
The main shortcoming of this parser is that error messages are not intuitive. You are welcome to give suggestions.
General
News
Question
Answer
Joke
Rant
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 |