YAML学习笔记

这基本是官方文档的部分翻译和自我理解,主要为了能有个地方放一下,最近也打算迁移Blog 到 Github了。

正在搞Jekyll,iteye现在的人气实在不行咯

 

YAML Ain't Markup Language(YAML)

语法

Structure 空格 Sequence - Map 键值对对 : 分割

Collections

Example 2.1 Sequence of scalars

- Mark McGwire
- Sammy Sosa
- Ken Griffey

Example 2.2 Mapping of scalars to scalars

hr: 65
avg: 0.278
rbi: 147

Example 2.3 Mapping of scalars to sequences

american:
    - Boston Red Sox
    - Detroit Tigers
    - New York Yankees
national:
    - New York Mets
    - Chicago Cubs
    - Atlanta Braves

Example 2.4 Sequence of mappings

-
 name: Mark McGwire
 hr: 65
 avg: 0.278
-
 name: Sammy Sosa
 hr: 63
 avg: 0.288

Example 2.5 Sequence of sequences

- [name, hr, avg]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa, 63, 0.299]

Example 2.6 Mapping of mappings

Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
    hr: 63,
    avg: 0.288
}

Structure

在一个档案中,可同时包含多个文件,并用"——"分隔。选择性的符号"..."可以用来表示档案结尾(在利用串流的通讯中,这非常有用,可以在不关闭串流的情况下,发送结束讯号)。

Example 2.7 Tow Documents in a Stream(each with a leading comment)

# Ranking of 1998 home runs
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey

# Team ranking
---
- Chicago Cubs
- St Louis Cardinals

Example 2.8 Play by Play Feed

from a Game

---
time: 20:03:20
player: Sammy Sosa
action: strike (miss)
...
---
time: 20:03:47
player: Sammy Sosa
action: grand slam
...

重复的node可以用锚点&定义,下一次使用*引用

Example 2.9 Single Document with Two Comments

---
hr: # 1998 hr ranking
  - Mark McGwire
  - Sammy Sosa
rbi:
  # 1998 rbi ranking
  - Sammy Sosa
  - Ken Griffey

Example 2.10 Node for "Sammy Sosa"

appears twice in this document

---
hr:
  - Mark McGwire
  # Following node labeled SS
  - &SS Sammy Sosa
rbi:
  - *SS # Subsequent occurrence
  - Ken Griffey

? 加上一个空格后面可以带上一个复杂的Mapping Key

Example 2.11. Mapping between Sequences

? - Detroit Tigers
  - Chicago cubs
:
  - 2001-07-23

? [ New York Yankees,
    Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
    2001-08-14 ]

Example 2.12. Compact Nested Mapping

---
# Products purchased
- item    : Super Hoop
  quantity: 1
- item    : Basketball
  quantity: 4
- item    : Big Shoes
  quantity: 1

Scalars 标量

区块字符可以以| 或 > 打头开始,用|打头会保留换行符,用>会以折叠的方式保存,换行符会被保存为空格

Example 2.13 In literals, newlines are preserved

# ASCII Art
--- |
  \//||\/||
  // ||  ||__

Example 2.14. In the folded scalars, newlines become spaces

--- >
  Mark McGwire's
  year was crippled
  by a knee injury.

Example 2.15. Folded newlines are preserved for "more indented" and blank lines

>
 Sammy Sosa completed another
 fine season with great stats.

   63 Home Runs
   0.288 Batting Average

 What a year!

Example 2.16. Indentation determines scope

name: Mark McGwire
accomplishment: >
  Mark set a major league
  home run record in 1998.
stats: |
  65 Home Runs
  0.278 Batting Average

YAML的标量包含了加引号的文本和不加引号的文本,目前使用的都是不加引号的文本。双引号文本提供了转义,单引号不提供转义。所有的文本标量都能跨多行;换行符会自动的折叠

Example 2.17. Quoted Scalars

unicode: "Sosadidfine.\u263A"
control: "\b1998\t1999\t2000\n"
hex esc: "\x0d\x0ais\r\n"

single: '"Howdy!"hecried.'
quoted: '#Nota''comment''.'
tie-fighter: '|\-*-/|'

Example 2.18. Multi-line Flow Scalars

plain:
  This unquoted scalar
  spans many lines.

quoted: "Sodoesthis
  quotedscalar.\n"

Tags

YAML中,默认的数据类型根据使用的场景耳钉。通常的类型包含了seq,map和str。还有一些包含int, float, null。另外还有binary, omap, set等等 如果要在YAML中指定数据类型,在数据类型标示前加!。强制类型转换用!!, 另外还能使用自定义类型!

Example 2.23. Various Explicit Tags

---
not-date: !!str 2002-04-28

picture: !!binary |
 R0lGODlhDAAMAIQAAP//9/X
 17unp5WZmZgAAAOfn515eXv
 Pz7Y6OjuDg4J+fn5OTk6enp
 56enmleECcgggoBADs=

application specific tag: !something |
 The semantics of the tag
 above may be different for
 different documents.

Example 2.24. Global Tags

%TAG ! tag:clarkevans.com,2002:
--- !shape
  # Use the ! handle for presenting
  # tag:clarkevans.com,2002:circle
- !circle
  center: &ORIGIN {x: 73, y: 129}
  radius: 7
- !line
  start: *ORIGIN
  finish: { x: 89, y: 102 }
- !label
  start: *ORIGIN
  color: 0xFFEEBB
  text: Pretty vector drawing.

Example 2.25. Unordered Sets

# Sets are represented as a
# Mapping where each key is
# associated with a null value
--- !!set
? Mark McGwire
? Sammy Sosa
? Ken Griff

Example 2.26. Ordered Mappings

# Ordered maps are represented as
# A sequence of mappings, with
# each mapping having one key
--- !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58

完整的例子

Example 2.27. Invoice

--- !
invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
    address:
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
comments:
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.

你可能感兴趣的:(YAML)