yaml開始使用yaml

備註

YAML是“ Y AML A in't M arkup L anguage”的遞歸縮寫。它是適用於所有編程語言的人類可讀數據序列化標準。

版本

發布日期
1.0 2004-01-29
1.1 2005-01-18
1.2 2009-10-01

基本的Yaml語法

YAML是一種基於文本的格式,允許在層次結構中存儲結構化數據。 YAML旨在以人為和機器可讀,並且開銷最小。 YAML規範可以在yaml.org找到。還有一張參考卡

註釋以#開頭,直到換行,註釋必須通過空格與其他標記分開。空格不是空閒的,縮進必須是空格,而不是製表符。 YAML會考慮在其中包含前綴比父鍵更多空格的行。此外,所有行必須以相同數量的空格作為前綴,以屬於同一個地圖。

YAML具有序列和映射作為集合類型,兩者都可以用流和塊樣式表示。

YAML中的一系列標量字符串如下所示:

[ one, two, three ]   # flow style

# or block style

- one
- two
- three

映射由鍵/值對組成:

index: 4  # block style
name: nali

# or 

{ index: 4, name: nali }   # flow style

# or nested (equivalent of { level: { one: { two: fun } } }):

level:

  one:

    two: fun

基本的YAML類型

integer: 25
string: "25"
float: 25.0
boolean: true
null type: null

YAML順序數據

相同的列表級別:

- Cat
- Dog
- Goldfish

嵌套列表:

-
 - Cat
 - Dog
 - Goldfish

評論

# This comment occupies a whole line
- some item # This comment succeeds content of a line
- http://example.com/#nocomment
- "This # does not introduce a comment."
- |
    This is a block scalar.
    A # inside it does not introduce a comment.
   # unless it is less indented than the first line (this is one)

請注意,對於#引入註釋,它必須要么

  • 發生在一行的開頭,或
  • 之前是空格。

#必須始終後跟空格。 #裡面引用標量不會開始評論。 #可能會在塊標量的末尾引入註釋,但因此,它必須比塊標量的基本縮進(通常由其第一個非空行的縮進確定)縮進。

塊樣式映射

使用隱式鍵:

key: value
another key:
  - some
  - more
  - values
[1, 2, 3]: last value, which has a flow style key

使用隱式和顯式鍵:

? key
: value
another key:
  - some
  - more
  - values
? [1, 2, 3]
: last value, which has a flow style key

keyanother key[1, 2, 3]是相同映射的鍵,儘管它們使用不同的鍵樣式。


嵌套映射:

first level:
  second level:
    ? third level
    :
      forth level: value of implicit key
    ? third level, second key
    : value of explicit key
  ?
    mapping as: key of
    another: mapping
  : scalar value of mapping key
first level, second key:
  last value

在多行上拆分文本字符串

- Without quotes:
   You can just
   split a long piece of text like this.
- With quotes:
    "[But be careful:
     if you \"need\" punctuation, put double quotes around it. You can ev\
     en split without spaces by using backslashes."
- Or single quotes:
    'This works
     but isn''t as flexible'
- If you want to keep those new line characters: | 
    Then do
    it this way with 
    a pipe (|) character. (This string has three \n characters)
- Or you can have just the one final new line: >
    This string has
    just one \n character, at the very end.
- Block indicators:
    Look up >-, >+, |- and |+ for fine tuning.

逃脫角色

YAML支持三種轉義表示法:

  1. 實體逃脫

    一個。空間:“ ”

    灣冒號:“:”

    C。 &符號:“&”

  2. Unicode Escapes

    一個。空間:“\ u0020”

    灣單引號:“\ u0027”

    C。雙引號:“\ u0022”

  3. 引用逃脫

    一個。單引號中的雙引號:'是'我總是撒謊“一個真實的陳述?”

    灣嵌套雙引號:“她說,”我退出了“”

    C。嵌套的單引號:'他無言以對:'''