yaml的序列使用缩进表明作用域,每个条目一行,一个杠和空格表明一个序列条目:'- '
一个冒号和空格表明映射:': '
哈希符号表明注释:'#'
e.g: - Mark McGwire
- Sammy Sosa
- Ken Griffey
映射标量到序列
american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves
映射的序列:
-
name: Mark McGwire
hr: 65
avg: 0.278
-
name: Sammy Sosa
hr: 63
avg: 0.288
yaml流风格的序列(flaw style),来使用标识符表明作用域而不是缩进,在括号内以逗号分隔
序列的序列:
- [name , hr, avg ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa , 63, 0.288]
映射的映射:
Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
hr: 63,
avg: 0.288
}
三个杠'---'分隔指令(directives)和文档内容,表明文档的开始,三个点'...'表明文档的结束
e.g: # Ranking of 1998 home runs
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey
# Team ranking
---
- Chicago Cubs
- St Louis Cardinals
重复的节点,实体?(nodes,objects)首先被用锚标记,通过'&',然后在后面通过'*'引用
hr: # 1998 hr ranking
- Mark McGwire
- Sammy Sosa
rbi:
# 1998 rbi ranking
- Sammy Sosa
- Ken Griffey
hr:
- Mark McGwire
# Following node labeled SS
- &SS Sammy Sosa
rbi:
- *SS # Subsequent occurrence
- Ken Griffey
一个问号加上一个空格表明一个复杂的映射,Within a block collection, key: value pairs can start immediately following the dash, colon, or question mark.
e.g:序列间的映射:
e.g: ? - Detroit Tigers
- Chicago cubs
:
- 2001-07-23
? [ New York Yankees,
Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
2001-08-14 ]
标量内容可以用块符号,using a literal style(indicated by '|'),换行符有意义,或者使用折叠样式(denoted by '>'),所有的换行符被翻译成空白,以一个空行或多缩进(more-indented)行结束
e.g: 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!
缩进表明作用域:
name: Mark McGwire
accomplishment: >
Mark set a major league
home run record in 1998.
stats: |
65 Home Runs
0.278 Batting Average
YAML’s flow scalars include the plain style (most examples thus far) and two quoted styles. The double-quoted style provides escape sequences. The single-quoted style is useful when escaping is not needed. All flow scalars can span multiple lines; line breaks are always folded
Example 2.17. Quoted Scalars
unicode: "Sosa did fine.\u263A"
control: "\b1998\t1999\t2000\n"
hex esc: "\x0d\x0a is \r\n"
single: '"Howdy!" he cried.'
quoted: ' # Not a ''comment''.'
tie-fighter: '|\-*-/|'
Example 2.18. Multi-line Flow Scalars
plain:
This unquoted scalar
spans many lines.
quoted: "So does this
quoted scalar.\n"
tags :标签,数据类型?没有打标签的节点的类型依赖于应用程序,这里使用seq,map和str类型,也可以使用json的int,float, null,也可以使用额外的binary,omap,set
整形: canonical: 12345
decimal: +12345
octal: 0o14
hexadecimal: 0xC
浮点型:
canonical: 1.23015e+3
exponential: 12.3015e+02
fixed: 1230.15
negative infinity: -.inf
not a num: .NaN
其他:
null:
booleans: [ true, false ]
string: '012345'
时间戳:
canonical: 2001-12-15T02:59:43.1Z
iso8601: 2001-12-14t21:59:43.10-05:00
spaced: 2001-12-14 21:59:43.10-5
date: 2002-12-14
显示类型通过'!!'和标签声明:
e.g: 各种显示标签:
---
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.
全局标签是URI,也and may be specified in a tag shorthand notation using a handle,应用程序特定的本地标签也可以使用
e.g: %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.
ordered map
# 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
YAML指令表明yaml的版本信息:
e.g:
%YAML 1.3 # Attempt parsing
# with a warning
---
"foo"
'TAG'指令:tag指令通过为指定的节点标签建立速记标签,每一个tag指令关联一个handle通过一个前缀(prefix)
ns-tag-directive c-tag-handle ns-tag-prefix
不能为相同的handle指定相同的TAG在同一个文档中,尽管给出了相同的前缀
%TAG ! !foo
%TAG ! !foo
bar
ERROR:
The TAG directive must only
be given at most once per
handle in the same document.
c-tag-handle::= c-named-tag-handle| c-secondary-tag-handle | c-primary-tag-handle
primary handle :primary handle是单个'!',默认前缀是'!',因此这种速记标签是本地标签,可通过指明tag和前缀改变
e.g: # Private
!foo "bar"
...
# Global
%TAG ! tag:example.com,2000:app/
---
!foo "bar"
secondary handle: secondary tag handle是'!!',默认前缀是"tag:yaml.org,2002:", 这个前缀是yaml官方标签库
e.g: %TAG !! tag:example.com,2000:app/
---
!!int 1 - 3 # Interval, not integer
%YAML 1.2
---
!<tag:example.com,2000:app/int> "1 - 3"
named handle: 用'!'包围一个非空名称 A handle name must not be used in a tag shorthand unless an explicit “TAG” directive has associated some prefix with it.
%TAG !e! tag:example.com,2000:app/
---
!e!foo "bar"
%YAML 1.2
---
!<tag:example.com,2000:app/foo> "bar"
ns-tag-prefix ::= c-ns-local-tag-prefix | ns-global-tag-prefix
tag前缀以!开头表明是本地tag
%TAG !m! !my-
--- # Bulb here
!m!light fluorescent
...
%TAG !m! !my-
--- # Color here
!m!light green
%YAML 1.2
---
!<!my-light> "fluorescent"
...
%YAML 1.2
---
!<!my-light> "green"
不以'!'开头的前缀是全局tag,必须是一个有效的uri前缀,并且至少包含scheme和authority, 关联的速记标签必须是全局唯一的
%TAG !e! tag:example.com,2000:app/
---
- !e!foo "bar"
%YAML 1.2
---
!<tag:example.com,2000:app/foo> "bar"
每个节点都有额外的属性,anchor和tag
c-ns-properties(n,c) ::= ( c-ns-tag-property
( s-separate(n,c) c-ns-anchor-property )? )
| ( c-ns-anchor-property
( s-separate(n,c) c-ns-tag-property )? )
!!str &a1 "foo":
!!str bar
&a2 baz : *a1
Verbatim Tags
A verbatim tag must either begin with a “!” (a local tag) or be a valid URI (a global tag)
c-verbatim-tag ::= “!” “<” ns-uri-char+ “>”
!<tag:yaml.org,2002:str> foo :
!<!bar> baz
区块的字符:字符不需要包在引号内,,,,区块字符可以以| 或 > 打头开始,用|打头会保留换行符,用>会以折叠的方式保存,换行符会被保存为空格
保存新行:
--- | #譯者注:這是一首著名的五行民謠(limerick)
There once was a man from Darjeeling #這裡曾有一個人來自大吉嶺
Who got on a bus bound for Ealing #他搭上一班往伊靈的公車
It said on the door #門上這麼說的
"Please don't spit on the floor" #"請勿在地上吐痰"
So he carefully spat on the ceiling #所以他小心翼翼的吐在天花板上
根据设定,前方的引领空白符号(leading white space)必须排成条状,以便和其他资料或是行为(如范例中的缩排)明显区分。
折叠新行:
--- >
Wrapped text #摺疊的文字
will be folded #將會被收
into a single #進單一一個
paragraph #段落
Blank lines denote #空白的行代表
paragraph breaks #段落之間的區隔
10.2.1.4. Floating Point
URI:
tag:yaml.org,2002:float
10.2.1.3. Integer
URI:
tag:yaml.org,2002:int
YAML的标量包含了加引号的文本和不加引号的文本,目前使用的都是不加引号的文本。双引号文本提供了转义,单引号不提供转义。所有的文本标量都能跨多行;换行符会自动的折叠
浙公网安备 33010602011771号