YAML入門教程

前言

YAML 是 "YAML Ain't a Markup Language"(YAML 不是一種標記語言)的遞歸縮寫。在開發(fā)的這種語言時,YAML 的意思其實是:"Yet Another Markup Language"(仍是一種標記語言)。

YAML 的語法和其他高級語言類似,并且可以簡單表達清單、散列表,標量等數(shù)據(jù)形態(tài)。它使用空白符號縮進和大量依賴外觀的特色,特別適合用來表達或編輯數(shù)據(jù)結(jié)構(gòu)、各種配置文件、傾印調(diào)試內(nèi)容、文件大綱(例如:許多電子郵件標題格式和YAML非常接近)。

YAML 的配置文件后綴為 .yml,如:ansible-playbook.yml,Hexo的配置文件也是_config.yml

更新歷史

2020年01月09日 - 初稿

閱讀原文 - https://wsgzao.github.io/post/yaml/

擴展閱讀

YAML


YAML簡介

YAML: YAML Ain't Markup Language

What It Is: YAML is a human friendly data serialization standard for all programming languages.

YAML 語言(發(fā)音 /?j?m?l/ )的設(shè)計目標,就是方便人類讀寫。它實質(zhì)上是一種通用的數(shù)據(jù)串行化格式。

它的基本語法規(guī)則如下。

  • 大小寫敏感
  • 使用縮進表示層級關(guān)系
  • 縮進時不允許使用Tab鍵,只允許使用空格。
  • 縮進的空格數(shù)目不重要,只要相同層級的元素左側(cè)對齊即可
# 表示注釋,從這個字符一直到行尾,都會被解析器忽略。

YAML 支持的數(shù)據(jù)結(jié)構(gòu)有三種。

  • 對象:鍵值對的集合,又稱為映射(mapping)/ 哈希(hashes) / 字典(dictionary)
  • 數(shù)組:一組按次序排列的值,又稱為序列(sequence) / 列表(list)
  • 純量(scalars):單個的、不可再分的值

X分鐘速成Y

其中 Y=yaml

源代碼下載: learnyaml-cn.yaml

YAML 是一個數(shù)據(jù)序列化語言,被設(shè)計成人類直接可寫可讀的。

它是 JSON 的嚴格超集,增加了語法顯著換行符和縮進,就像 Python。但和 Python 不一樣, YAML 根本不容許文字制表符。

# YAML 中的注解看起來像這樣。

################
# 標量類型     #
################

# 我們的根對象 (它們在整個文件里延續(xù)) 將會是一個映射,
# 它等價于在別的語言里的一個字典,哈希表或?qū)ο蟆?key: value
another_key: Another value goes here.
a_number_value: 100
# 數(shù)字 1 會被解釋為數(shù)值,而不是一個布爾值。
# 如果你想要的是一個布爾值,使用 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 字符需要被轉(zhuǎn)義(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 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

# 鍵也可以是復(fù)合型的,比如多行對象
# 我們用 ? 后跟一個空格來表示一個復(fù)合鍵的開始。
? |
  This is a key
  that has multiple lines
: and this is its value

# YAML 也允許使用復(fù)雜鍵語法表示序列間的映射關(guān)系。
# 但有些語言的解析器可能會不支持。
# 一個例子:
? - Manchester United
  - Real Madrid
: [ 2001-01-01, 2002-02-02 ]

# 序列 (等價于列表或數(shù)組) 看起來像這樣:
# 注意 '-' 算作縮進
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 風(fēng)格的映射和序列:
json_map: {"key": "value"}
json_seq: [3, 2, 1, "takeoff"]
and quotes are optional: {key: [3, 2, 1, takeoff]}

#######################
# 其余的 YAML 特性    #
#######################

# YAML 還有一個方便的特性叫 '錨',它能讓你很容易在文檔中進行文本復(fù)用。
# 如下兩個鍵會有相同的值:
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name

# 錨也可被用來復(fù)制/繼承屬性
base: &base
  name: Everyone has same name

# The regexp << is called Merge Key Language-Independent Type.
# 它表明指定映射的所有鍵值會插入到當(dāng)前的映射中。

foo: &foo
  <<: *base
  age: 10

bar: &bar
  <<: *base
  age: 20

# foo 和 bar 將都含有 name: Everyone has same name

# YAML 還有標簽,你可以用它顯示地聲明類型。
explicit_string: !!str 0.5
# 一些解析器實現(xiàn)特定語言的標簽,就像這個針對 Python 的復(fù)數(shù)類型。
python_complex_number: !!python/complex 1+2j

# 我們也可以在 YAML 的復(fù)合鍵中使用特定語言的標簽
? !!python/tuple [5, 7]
: Fifty Seven
# 將會是 Python 中的  {(5, 7): 'Fifty Seven'}

####################
# 其余的 YAML 類型 #
####################

# 除了字符串和數(shù)字,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:
  ? item1
  ? item2
  ? item3
or: {item1, item2, item3}

# 集合只是值為 null 的映射;上面的集合等價于:
set2:
  item1: null
  item2: null
  item3: null

...  # document end

參考文章

YAML official website

Online YAML Validator

X分鐘速成Y

YAML 入門教程

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容