让我们一起YAML吧
[ 2007-08-18 12:43:03 | 作者: progame ]
YAML Ain't Markup Language, 一般会认为Yet Another Markup Lanuage, 结果它表明自己不是标记语言, 按wikipedia的解释: A markup language combines text and extra information about the text. The extra information, for example about the text's structure or presentation, is expressed using markup, which is intermingled with the primary text. 既然YAML说自己非标记性语言, 那么它很清楚的在告诉大家: 在这里, 只有数据!
YAML(tm) is an international collaboration to make a data serialization language which is both human readable and computationally powerful. 所以它的出发点就是对人来说是易读的, 对计算机来说是容易处理的, 主要用来序列化数据, 当然我认为它还包括了反序列化
慢慢地, 面对越来越多的诱惑和需求, YAML开始变得复杂, 有兴趣的可以看它的Specification: http://yaml.org/spec/current.html 估计大多数人要晕了, YAML的初衷是希望它的Parser是易于实现而且只需要单向处理(即不需回溯), 这样可以保证Prase的速度, 但现在, 如果完全实现它的Specification.... 起码C#的YAML Parser: http://yaml-net-parser.sourceforge.net/ 已经歇菜了, 两位作者说实在无法去应对所有的特性
因为我需要很多描述性的数据要存储读取, 包括在程序文件夹和数据库中, 在外部的还得经常编辑, 自然地但想着用YAML来处理, 结果试用了一下Yaml.Net, 很多我需要的功能没有, 所以最后我还是自己写了一个Yaml Parser, 因为它是通过TAB缩进描述的, 所以一般的Parser Generator就派不上用场了, 幸好里面的规则不多
当然我也不会去完全实现所有特性, 取舍之后, 有了很多修改, 到后来原来的YAML语法就不管了, 怎么易读易写就怎么来了
改动之后, TAG功能完全去掉了, 毕竟.Net是可以反射的, 没必要增加编辑文件的痛苦
一段Yaml可以包含多个对象定义
对象定义可以嵌套
字符串如果没有换行,空格和特殊字符 \t \" \' = : & , | >, 可以直接写入
否则可以以\' 和 \" 两种方式标明
换行的有两种表式 | 表示的是多行文件, > 表示的是单行文件写做多行
string:abc
string:"abc"
string:'abc'
string| # value is "ab\r\nc"
ab
c
string> # value is "abc"
ab
c
注释以#开始
对象引用以&开始
value:&object
对象定义后 : 表示继承
inheritedtype:parenttype
属性和值
prop:value
泛型的Dictionary
简单类型 key1:value1, key2:value2
复杂类型
key=prop1:value1 prop2:value2
或
key
prop1:value1
prop2:value2
泛型的List
简单类型
value1, value2,...
或
value1
value2
...
复杂类型
-
prop1:value11
prop2:value21
-
prop1:value12
prop2:value22
或
- prop1:value11 prop2:value21
- prop1:value21 prop2:value22
或
-
prop1:value11 prop2:value21
-
prop1:value21 prop2:value22
测试用例:
public class Person
{
public string name;
public int age;
public float salary;
public bool married;
public DateTime birthday;
public string remark;
public Dictionary<string, Person> Children;
public List<Person> Friends;
public List<string> Titles;
public Dictionary<string, string> Address;
public Person ReportTo;
public Person Parent;
public Company Company;
public Color haircolor;
public Person()
{
Children = new Dictionary<string, Person>();
Friends = new List<Person>();
Titles = new List<string>();
Address = new Dictionary<string, string>();
}
}
public struct Company
{
public string name;
public string address;
}
YAML文件:
#collection:list,dictionary
#property
#simple type:string,int,float,double,datetime,bool
#custom type
simpletype
name:progame
age:100
salary:10.23
birthday:1979-09-20
married:false # if married
simpletype2
name:progame age: 100 remark>
good
name
struct
name:progame
company
name:tastysoft
address:shanghai
list
children
A = name:A
B = name:B
friends
-
name:A
age:10
-
name:B
age:200
friends
-
name:BB
simplelist
titles
A
B
C
simplelist2
titles
A,B,C
simplelist3
friends
- name:A age:10
- name:B age:200
# can't be name:A agen:10, name:B age:200
simplelist4
friends
-
name:A age:10
-
name:B age:200
simplelist5
titles
- A
- B
simplelist6
titles
-
A
-
B
simplemap
address
home:shanghai,office:beijing
simplemap2
address
home:shanghai
office:beijing
simplemap3
children
A
B
simplemap4
address
home,office
qutoedstring
name:'pro 我gam\\e\''
remark:"pro 我gam\\e\""
multext
remark|
a
b
foldedtext
remark>
a
b
refer
reportto:&simpletype
inherit:simpletype
name:inherit
inherit2:list
children
A = age:100
B = name:BB
C = name:C
friends
-
name:C
age:200
selfref
parent:&selfref
dump:simpletype
remark|
good
name
children
A = name:A
B = name:B
friends
-
name:A
age:10
-
name:B
age:200
address
home:"shang:hai",office:beijing
titles
A,B,C
reportto:&simpletype
评论Feed: /feed.asp?q=comment&id=999
YAML(tm) is an international collaboration to make a data serialization language which is both human readable and computationally powerful. 所以它的出发点就是对人来说是易读的, 对计算机来说是容易处理的, 主要用来序列化数据, 当然我认为它还包括了反序列化
慢慢地, 面对越来越多的诱惑和需求, YAML开始变得复杂, 有兴趣的可以看它的Specification: http://yaml.org/spec/current.html 估计大多数人要晕了, YAML的初衷是希望它的Parser是易于实现而且只需要单向处理(即不需回溯), 这样可以保证Prase的速度, 但现在, 如果完全实现它的Specification.... 起码C#的YAML Parser: http://yaml-net-parser.sourceforge.net/ 已经歇菜了, 两位作者说实在无法去应对所有的特性
因为我需要很多描述性的数据要存储读取, 包括在程序文件夹和数据库中, 在外部的还得经常编辑, 自然地但想着用YAML来处理, 结果试用了一下Yaml.Net, 很多我需要的功能没有, 所以最后我还是自己写了一个Yaml Parser, 因为它是通过TAB缩进描述的, 所以一般的Parser Generator就派不上用场了, 幸好里面的规则不多
当然我也不会去完全实现所有特性, 取舍之后, 有了很多修改, 到后来原来的YAML语法就不管了, 怎么易读易写就怎么来了
改动之后, TAG功能完全去掉了, 毕竟.Net是可以反射的, 没必要增加编辑文件的痛苦
一段Yaml可以包含多个对象定义
对象定义可以嵌套
字符串如果没有换行,空格和特殊字符 \t \" \' = : & , | >, 可以直接写入
否则可以以\' 和 \" 两种方式标明
换行的有两种表式 | 表示的是多行文件, > 表示的是单行文件写做多行
string:abc
string:"abc"
string:'abc'
string| # value is "ab\r\nc"
ab
c
string> # value is "abc"
ab
c
注释以#开始
对象引用以&开始
value:&object
对象定义后 : 表示继承
inheritedtype:parenttype
属性和值
prop:value
泛型的Dictionary
简单类型 key1:value1, key2:value2
复杂类型
key=prop1:value1 prop2:value2
或
key
prop1:value1
prop2:value2
泛型的List
简单类型
value1, value2,...
或
value1
value2
...
复杂类型
-
prop1:value11
prop2:value21
-
prop1:value12
prop2:value22
或
- prop1:value11 prop2:value21
- prop1:value21 prop2:value22
或
-
prop1:value11 prop2:value21
-
prop1:value21 prop2:value22
测试用例:
public class Person
{
public string name;
public int age;
public float salary;
public bool married;
public DateTime birthday;
public string remark;
public Dictionary<string, Person> Children;
public List<Person> Friends;
public List<string> Titles;
public Dictionary<string, string> Address;
public Person ReportTo;
public Person Parent;
public Company Company;
public Color haircolor;
public Person()
{
Children = new Dictionary<string, Person>();
Friends = new List<Person>();
Titles = new List<string>();
Address = new Dictionary<string, string>();
}
}
public struct Company
{
public string name;
public string address;
}
YAML文件:
#collection:list,dictionary
#property
#simple type:string,int,float,double,datetime,bool
#custom type
simpletype
name:progame
age:100
salary:10.23
birthday:1979-09-20
married:false # if married
simpletype2
name:progame age: 100 remark>
good
name
struct
name:progame
company
name:tastysoft
address:shanghai
list
children
A = name:A
B = name:B
friends
-
name:A
age:10
-
name:B
age:200
friends
-
name:BB
simplelist
titles
A
B
C
simplelist2
titles
A,B,C
simplelist3
friends
- name:A age:10
- name:B age:200
# can't be name:A agen:10, name:B age:200
simplelist4
friends
-
name:A age:10
-
name:B age:200
simplelist5
titles
- A
- B
simplelist6
titles
-
A
-
B
simplemap
address
home:shanghai,office:beijing
simplemap2
address
home:shanghai
office:beijing
simplemap3
children
A
B
simplemap4
address
home,office
qutoedstring
name:'pro 我gam\\e\''
remark:"pro 我gam\\e\""
multext
remark|
a
b
foldedtext
remark>
a
b
refer
reportto:&simpletype
inherit:simpletype
name:inherit
inherit2:list
children
A = age:100
B = name:BB
C = name:C
friends
-
name:C
age:200
selfref
parent:&selfref
dump:simpletype
remark|
good
name
children
A = name:A
B = name:B
friends
-
name:A
age:10
-
name:B
age:200
address
home:"shang:hai",office:beijing
titles
A,B,C
reportto:&simpletype
评论Feed: /feed.asp?q=comment&id=999
您可能感兴趣的文章:
Winform内存泄露是如此容易的一件事 (progame at 2008-08-17)
vb6升级到vb.net? 小心! (progame at 2008-10-06)
感受ADO.Net 2.0 (progame at 2006-09-08)
八月桂花香 NUnit来帮忙 (progame at 2006-09-20)
C#和Java语言级别的几点不同 (progame at 2007-04-06)
放弃XML做为配置文件,转投Yaml怀抱 (progame at 2007-04-22)
using用用还是蛮好的 (progame at 2008-01-16)
C#枚举所有sql server数据库实例 (progame at 2008-03-30)
这篇日志没有评论.

