2014-06-01 05:26:12 +04:00
|
|
|
|
---
|
|
|
|
|
language: yaml
|
|
|
|
|
contributors:
|
|
|
|
|
- ["Adam Brenecki", "https://github.com/adambrenecki"]
|
|
|
|
|
translators:
|
|
|
|
|
- ["Zach Zhang", "https://github.com/checkcheckzz"]
|
|
|
|
|
filename: learnyaml-cn.yaml
|
|
|
|
|
lang: zh-cn
|
|
|
|
|
---
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
YAML是一个数据序列化语言,被设计成人类直接可写可读的。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
它是JSON的严格超集,增加了语法显著换行符和缩进,就像Python。但和Python不一样,
|
|
|
|
|
YAML根本不容许文字制表符。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```yaml
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# YAML中的注解看起来像这样。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
|
|
|
|
|
################
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 标量类型 #
|
2014-06-01 05:26:12 +04:00
|
|
|
|
################
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 我们的根对象 (它们在整个文件里延续) 将会是一个地图,
|
|
|
|
|
# 它等价于在别的语言里的一个字典,哈西表或对象。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
key: value
|
|
|
|
|
another_key: Another value goes here.
|
|
|
|
|
a_number_value: 100
|
|
|
|
|
scientific_notation: 1e+12
|
|
|
|
|
boolean: true
|
|
|
|
|
null_value: null
|
|
|
|
|
key with spaces: value
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 注意到字符串不需要被引用。但是,它们可以被引用。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
"Keys can be quoted too.": "Useful if you want to put a ':' in your key."
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 多行字符串既可以写成像一个'文字块'(使用 |),
|
|
|
|
|
# 或像一个'折叠块'(使用 '>')。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
literal_block: |
|
|
|
|
|
This entire block of text will be the value of the 'literal_block' key,
|
|
|
|
|
with line breaks being preserved.
|
|
|
|
|
|
|
|
|
|
The literal continues until de-dented, and the leading indentation is
|
|
|
|
|
stripped.
|
|
|
|
|
|
|
|
|
|
Any lines that are 'more-indented' keep the rest of their indentation -
|
|
|
|
|
these lines will be indented by 4 spaces.
|
|
|
|
|
folded_style: >
|
|
|
|
|
This entire block of text will be the value of 'folded_style', but this
|
|
|
|
|
time, all newlines will be replaced with a single space.
|
|
|
|
|
|
|
|
|
|
Blank lines, like above, are converted to a newline character.
|
|
|
|
|
|
|
|
|
|
'More-indented' lines keep their newlines, too -
|
|
|
|
|
this text will appear over two lines.
|
|
|
|
|
|
|
|
|
|
####################
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 集合类型 #
|
2014-06-01 05:26:12 +04:00
|
|
|
|
####################
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 嵌套是通过缩进完成的。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
a_nested_map:
|
|
|
|
|
key: value
|
|
|
|
|
another_key: Another Value
|
|
|
|
|
another_nested_map:
|
|
|
|
|
hello: hello
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 地图不用有字符串键值。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
0.25: a float key
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 键值也可以是多行对象,用?表明键值的开始。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
? |
|
|
|
|
|
This is a key
|
|
|
|
|
that has multiple lines
|
|
|
|
|
: and this is its value
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# YAML也容许键值是集合类型,但是很多语言将会抱怨。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 序列 (等价于表或数组) 看起来像这样:
|
2014-06-01 05:26:12 +04:00
|
|
|
|
a_sequence:
|
|
|
|
|
- Item 1
|
|
|
|
|
- Item 2
|
|
|
|
|
- 0.5 # sequences can contain disparate types
|
|
|
|
|
- Item 4
|
|
|
|
|
- key: value
|
|
|
|
|
another_key: another_value
|
|
|
|
|
-
|
|
|
|
|
- This is a sequence
|
|
|
|
|
- inside another sequence
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 因为YAML是JSON的超集,你也可以写JSON风格的地图和序列:
|
2014-06-01 05:26:12 +04:00
|
|
|
|
json_map: {"key": "value"}
|
|
|
|
|
json_seq: [3, 2, 1, "takeoff"]
|
|
|
|
|
|
|
|
|
|
#######################
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 其余的YAML特点 #
|
2014-06-01 05:26:12 +04:00
|
|
|
|
#######################
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# YAML还有一个方便的特点叫'锚',它让你简单地在整个文件里重复内容。
|
|
|
|
|
# 两个键值将会有相同的值:
|
2014-06-01 05:26:12 +04:00
|
|
|
|
anchored_content: &anchor_name This string will appear as the value of two keys.
|
|
|
|
|
other_anchor: *anchor_name
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# YAML还有标签,你可以用它显示地声明类型。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
explicit_string: !!str 0.5
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 一些解析器实现特定语言的标签,就像这个为了Python的复数类型。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
python_complex_number: !!python/complex 1+2j
|
|
|
|
|
|
|
|
|
|
####################
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 其余的YAML类型 #
|
2014-06-01 05:26:12 +04:00
|
|
|
|
####################
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 字符串和数字不是仅有的YAML可以理解的标量。
|
|
|
|
|
# ISO 格式的日期和日期时间文字也是可以被解析的。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
datetime: 2001-12-15T02:59:43.1Z
|
|
|
|
|
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
|
|
|
|
|
date: 2002-12-14
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 这个!!binary标签表明一个字符串实际上是一个二进制blob的base64编码表示。
|
2014-06-01 05:26:12 +04:00
|
|
|
|
gif_file: !!binary |
|
|
|
|
|
R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
|
|
|
|
|
OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
|
|
|
|
|
+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
|
|
|
|
|
AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# YAML还有一个集合类型,它看起来像这样:
|
2014-06-01 05:26:12 +04:00
|
|
|
|
set:
|
|
|
|
|
? item1
|
|
|
|
|
? item2
|
|
|
|
|
? item3
|
|
|
|
|
|
2014-06-01 05:34:05 +04:00
|
|
|
|
# 像Python一样,集合仅是有null数值的地图;上面的集合等价于:
|
2014-06-01 05:26:12 +04:00
|
|
|
|
set2:
|
|
|
|
|
item1: null
|
|
|
|
|
item2: null
|
|
|
|
|
item3: null
|
|
|
|
|
```
|