---# 文檔開頭# YAML 中的注解看起來像這樣。################# 標量類型 ################## 我們的根對象 (它們在整個文件里延續) 將會是一個映射,# 它等價于在別的語言里的一個字典,哈希表或對象。key: value
another_key: Another value goes here.
a_number_value:100# 數字 1 會被解釋為數值,而不是一個布爾值。# 如果你想要的是一個布爾值,使用 true。scientific_notation:1e+12boolean:truenull_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 字符需要被轉義(encoded)
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 isstripped.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 thistime, 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: valueanother_key: Another Valueanother_nested_map:hello: hello# 映射的鍵不必是字符串。0.25: a float key# 鍵也可以是復合型的,比如多行對象# 我們用 ? 后跟一個空格來表示一個復合鍵的開始。?|This is a keythat has multiple lines
: and this is its value# YAML 也允許使用復雜鍵語法表示序列間的映射關系。# 但有些語言的解析器可能會不支持。# 一個例子:?- Manchester United- Real Madrid
:[2001-01-01,2002-02-02]# 序列 (等價于列表或數組) 看起來像這樣:# 注意 '-' 算作縮進a_sequence:- Item 1- Item 2-0.5 # 序列可以包含不同類型。- Item 4-key: valueanother_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 還有一個方便的特性叫 '錨',它能讓你很容易在文檔中進行文本復用。# 如下兩個鍵會有相同的值:anchored_content:&anchor_name This string will appear as the value of two keys.
other_anchor:*anchor_name# 錨也可被用來復制/繼承屬性base:&basename: Everyone has same name# The regexp << is called Merge Key Language-Independent Type.# 它表明指定映射的所有鍵值會插入到當前的映射中。foo:&foo<<:*baseage:10bar:&bar<<:*baseage:20# foo 和 bar 將都含有 name: Everyone has same name# YAML 還有標簽,你可以用它顯式地聲明類型。explicit_string:!!str0.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.1Zdatetime_with_spaces:2001-12-14 21:59:43.10 -5date:2002-12-14# 這個 !!binary 標簽表明這個字符串實際上# 是一個用 base64 編碼表示的二進制 blob。gif_file:!!binary|R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=# YAML 還有一個集合類型,它看起來像這樣:set:? item1? item2? item3
or:{item1, item2, item3}# 集合只是值為 null 的映射;上面的集合等價于:set2:item1:nullitem2:nullitem3:null...# 文檔結束