源代碼下載: learnyaml-cn.yaml
YAML 是一種數據序列化語言,旨在讓人類直接可寫可讀。
它是 JSON 的嚴格超集,增加了在語法上有意義的(syntactically significant)換行符和縮進,就像 Python 一樣。但和 Python 的不同之處在於,YAML 不允許使用文字製表符(literal tab characters)來表示縮進。
--- # 文檔開頭
# YAML 中的註釋看起來像這樣。
################
# 標量類型 #
################
# 我們的根對象 (貫穿整個文檔的始終) 是一個映射(map),
# 它等價於其它語言中的一個字典(dictionary),哈希表(hash)或對象(object)。
key: value
another_key: Another value goes here.
a_number_value: 100
# 數字 1 會被解釋為數值,而不是一個布爾值。
# 如果你想要的是一個布爾值,使用 true。
scientific_notation: 1e+12
boolean: true
null_value: null
key with spaces: value
# 注意,字符串可以不括在引號裏。當然,也可以括在引號裏。
however: 'A string, enclosed in quotes.'
'Keys can be quoted too.': "Useful if you want to put a ':' in your key."
single quotes: 'have ''one'' escape pattern'
double quotes: "have many: \", \0, \t, \u263A, \x0d\x0a == \r\n, and more."
# UTF-8/16/32字符需要指明編碼(通過\u)。
Superscript two: \u00B2
# 多行字符串既可以寫成一個'字面量塊'(使用 '|'),
# 也可以寫成一個'摺疊塊'(使用 '>')。
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.
####################
# 集合類型 #
####################
# 嵌套是通過縮進完成的。推薦使用 2 個空格的縮進(但非必須)。
a_nested_map:
key: value
another_key: Another Value
another_nested_map:
hello: hello
# 映射的鍵不必是字符串。
0.25: a float key
# 鍵也可以是複合(complex)的,比如多行對象
# 我們用 '?' 後跟一個空格來表示一個複合鍵的開始。
? |
This is a key
that has multiple lines
: and this is its value
# YAML 也允許使用複雜鍵語法表示序列間的映射關係。
# 但有些解析器可能會不支持。
# 一個例子:
? - Manchester United
- Real Madrid
: [ 2001-01-01, 2002-02-02 ]
# 序列 (sequences,等價於列表 list 或數組 array ) 看起來像這樣:
# 注意 '-' 也算縮進:
a_sequence:
- Item 1
- Item 2
- 0.5 # 序列可以包含不同類型。
- Item 4
- key: value
another_key: another_value
-
- This is a sequence
- inside another sequence
- - - Nested sequence indicators
- can be collapsed
# 因為 YAML 是 JSON 的超集,你也可以寫 JSON 風格的映射和序列:
json_map: {"key": "value"}
json_seq: [3, 2, 1, "takeoff"]
and quotes are optional: {key: [3, 2, 1, takeoff]}
#######################
# 其餘的 YAML 特性 #
#######################
# YAML 還有一個方便的特性叫“錨”(anchors)。你可以使用它在文檔中輕鬆地完成文本複用。
# 如下兩個鍵會有相同的值:
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name
# 錨也可被用來複制/繼承屬性
base: &base
name: Everyone has same name
# '<<'稱為語言無關的合併鍵類型(Merge Key Language-Independent Type).
# 它表明一個或多個指定映射中的所有鍵值會插入到當前的映射中。
foo: &foo
<<: *base
age: 10
bar: &bar
<<: *base
age: 20
# foo 和 bar 將都含有 name: Everyone has same name
# YAML 還有標籤(tags),你可以用它顯式地聲明類型。
explicit_string: !!str 0.5
# 一些解析器實現了特定語言的標籤,就像這個針對Python的複數類型的標籤。
python_complex_number: !!python/complex 1+2j
# 我們也可以在 YAML 的複合鍵中使用特定語言的標籤:
? !!python/tuple [5, 7]
: Fifty Seven
# 將會是 Python 中的 {(5, 7): 'Fifty Seven'}
####################
# 其餘的 YAML 類型 #
####################
# 除了字符串和數字,YAML 還支持其它標量。
# ISO 格式的日期和時間字面量也可以被解析。
datetime: 2001-12-15T02:59:43.1Z
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14
# 這個 !!binary 標籤表明這個字符串實際上
# 是一個用 base64 編碼表示的二進制 blob。
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=
# YAML 還有一個集合(set)類型,它看起來像這樣:
set:
? item1
? item2
? item3
or: {item1, item2, item3}
# 集合只是值均為 null 的映射;上面的集合等價於:
set2:
item1: null
item2: null
item3: null
... # 文檔結束
更多資源
- YAML official website
- Online YAML Converter
- Online YAML Validator
有建議?或者發現什麼錯誤?在Github上開一個 issue ,或者發起 pull request !
原著 Leigh Brenecki ,並由0個好心人修改。
© 2022 Leigh Brenecki
Translated by: Zach Zhang Jiang Haiyun Wen Sun
本作品採用 CC BY-SA 3.0 協議進行許可。