YAML用法詳解

1. 簡介

YAML 語言(發音 /ˈjæməl/ )的設計目標,就是方便人類讀寫。它實質上是一種通用的數據串行化格式,遠比 JSON 格式方便。
YAML: YAML Ain’t Markup Language

1.1 它的基本語法規則如下。

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

1.2 YAML 支持的數據結構有三種。

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

2. 對象

## 使用冒號結構表示:
animal: pets
# { animal: 'pets' }

## 將所有鍵值對寫成一個行內對象:
hash: { name: Steve, foo: bar } 
# { hash: { name: 'Steve', foo: 'bar' } }

3. 數組

3.1 一組連詞線開頭的行,構成一個數組:

- Cat
- Dog
- Goldfish

# [ 'Cat', 'Dog', 'Goldfish' ]

3.2 二維數組:

-
 - Cat
 - Dog
 - Goldfish
 
# [ [ 'Cat', 'Dog', 'Goldfish' ] ]

3.3 行內表示法

animal: [Cat, Dog]
 
# { animal: [ 'Cat', 'Dog' ] }

4. 複合結構

對象和數組可以結合使用,形成複合結構。

languages:
 - Ruby
 - Perl
 - Python 
websites:
 YAML: yaml.org 
 Ruby: ruby-lang.org 
 Python: python.org 
 Perl: use.perl.org 
 
# { 
#  languages: [ 'Ruby', 'Perl', 'Python' ],
#  websites: {
#     YAML: 'yaml.org',
#     Ruby: 'ruby-lang.org',
#     Python: 'python.org',
#     Perl: 'use.perl.org' 
#   } 
# }

5. 純量

純量是最基本的、不可再分的值。以下數據類型都屬於 JavaScript 的純量。

  • 字符串
  • 布爾值
  • 整數
  • 浮點數
  • Null
  • 時間
  • 日期
## 數值直接以字面量的形式表示
number: 12.30
# { number: 12.30 }

## 布爾值用true和false表示
isSet: true
# { isSet: true }

## null用~表示
parent: ~ 
# { parent: null }

## 時間採用 ISO8601 格式
date: 2001-12-14t21:59:43.10-05:00 
#{ date: new Date('2001-12-14t21:59:43.10-05:00') }

## 日期採用複合 iso8601 格式的年、月、日表示
date: 1976-07-31
# { date: new Date('1976-07-31') }

## 使用兩個感嘆號,強制轉換數據類型
e: !!str 123
f: !!str true
# { e: '123', f: 'true' }

6. 字符串

6.1 單行

## 字符串默認不使用引號表示
str: 這是一行字符串
# { str: '這是一行字符串' }

## 如果字符串之中包含空格或特殊字符,需要放在引號之中。
str: '內容: 字符串'
# { str: '內容: 字符串' }

## 單引號和雙引號都可以使用,雙引號不會對特殊字符轉義。
s1: '內容\n字符串'
s2: "內容\n字符串"
# { s1: '內容\\n字符串', s2: '內容\n字符串' }

## 單引號之中如果還有單引號,必須連續使用兩個單引號轉義。
str: 'labor''s day' 
# { str: 'labor\'s day' }

6.2 多行

## 字符串可以寫成多行,從第二行開始,必須有一個單空格縮進。換行符會被轉爲空格。
str: 這是一段
  多行
  字符串
# { str: '這是一段 多行 字符串' }

## 多行字符串可以使用|保留換行符,也可以使用>摺疊換行。
this: |
  Foo
  Bar
that: >
  Foo
  Bar
# { this: 'Foo\nBar\n', that: 'Foo Bar\n' }

## +表示保留文字塊末尾的換行,-表示刪除字符串末尾的換行。
s1: |
  Foo

s2: |+
  Foo


s3: |-
  Foo
# { s1: 'Foo\n', s2: 'Foo\n\n\n', s3: 'Foo' }

## 字符串之中可以插入 HTML 標記。
message: |

  <p style="color: red">
    段落
  </p>
# { message: '\n<p style="color: red">\n  段落\n</p>\n' }

7. 引用

錨點&和別名*,可以用來引用。

defaults: &defaults
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  <<: *defaults

test:
  database: myapp_test
  <<: *defaults

等同於下面的代碼。

defaults:
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  adapter:  postgres
  host:     localhost

test:
  database: myapp_test
  adapter:  postgres
  host:     localhost

&用來建立錨點(defaults),<<表示合併到當前數據,*用來引用錨點。

下面是另一個例子。

- &showell Steve 
- Clark 
- Brian 
- Oren 
- *showell 
# [ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]

參考

阮一峯 YAML 語言教程
在線 Demo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章