YAML何许物也?在XML泛滥的情况下,YAML的出现的确让人眼前一亮,在初步学习了YAML以后,粗略的总结了一下,拿出来和大家分享。
[MindMap]
[参考文档]
YAML Specification
YAML 数据类型说明
[摘要]
YAML的设计目的
YAML描述: [注释, 文档, 数据结构, 数据类型, 其他]
YAML的设计目的
1、容易人类阅读
2、适合表示程序语言的数据结构
3、可用于不同程序间交换数据
4、支持泛型工具
5、支持串行处理?
6、丰富的表达能力和可扩展性
7、易于使用
粗看了specification以后感觉最好的是“容易人类阅读”,对比一下下面的xml和yaml的代码块:
# xml代码块:








# YAML代码块










或:




YAML利用缩进或者是explicit indicatior(如上面的{})来表示属性的嵌套,更为直观和simple。
YAML描述
先来看看YAML里的主要标记:
(1)注释:
举个例子:
# Comment Example
# Profile Of Mary
Mary:
- name: Mary
- age : 19 # age property
(2)文档(document):
现在还不明白文档是什么意思,既然YAML定义是data serialization,暂时把一个doucment视为一个object序列化后得到的yaml配置信息

---



(3)数据结构:
YAML的设计者认为在配置文件中所要表达的数据内容有三种类型:标量(Scalar,如字符串和整数等)、序列(Sequence,如数组)和Mapping(类似hash的key/value pair)。
sequence型主要是用来表示数组类型的数据。下图描述了YAML中Sequence型数据的表示法:
mapping数据类型主要用来表示key: value对类型的数据。YAML描述方式见下图:
最后,我们用YAML来描述一本书《单元测试知道-c#版》















YAML推荐使用空格作为缩进,避免了在不同编辑器中对tab的表示形式不同而可能产生误解。