在
yaml.org(英文)
可以找到轻巧而好用的
小抄(亦是用YAML表示)
及
格式说明
。下面的内容,是关于基本组件的摘要。
YAML使用可打印的Unicode字符,可使用UTF-8或UTF-16。
使用空白字符为文件缩进来表示结构;不过不能使用跳格字符(TAB)。
注解由井字号( # )开始,可以出现在一行中的任何位置,而且范围只有一行(也就是一般所谓的单行注解)
每个清单成员以单行表示,并用短杠+空白( - )起始。或使用方括号(
),并用逗号+空白( , )分开成员。
每个散列表的成员用冒号+空白( : )分开键值和内容。或使用大括号( { } ),并用逗号+空白( , )分开。
散列表的键值可以用问号 ( ? )起始,用来明确的表示多个词汇组成的键值。
字符串平常并不使用引号,但必要的时候可以用双引号 ( " )或单引号 ( ’ )框住。
使用双引号表示字符串时,可用倒斜线( \ )开始的转义字符(这跟C语言类似)表示特殊字符。
区块的字符串用缩进和修饰符(非必要)来和其他数据分隔,有新行保留(preserve)(使用符号 | )或新行折叠(flod)(使用符号 > )两种方式。
在单一文件中,可用连续三个连字号(—)区分多个文件。
另外,还有选择性的连续三个点号( … )用来表示文件结尾。
重复的内容可使从参考标记星号 ( * )复制到锚点标记( & )。
指定格式可以使用两个惊叹号 ( !! ),后面接上名称。
文件中的单一文件可以使用指导指令,使用方法是百分比符号( % )。有两个指导指令在YAML1.1版中被定义:
%YAML 指导指令,用来识别文件的YAML版本。
%TAG 指导指令,被用在URI的前缀标记。这个方法在标记节点的类型时相当有用。
YAML在使用逗号及冒号时,后面都必须接一个空白字符,所以可以在字符串或数值中自由加入分隔符号( 例如:5,280 或
http://www.wikipedia.org
) 而不需要使用引号。
另外还有两个特殊符号在YAML中被保留,有可能在未来的版本被使用–( @ )和( ` )。
§参考小抄
%YAML 1.1 # Reference card
Collection indicators:
'? ' : Key indicator.
': ' : Value indicator.
'- ' : Nested series entry indicator.
', ' : Separate in-line branch entries.
'[]' : Surround in-line series branch.
'{}' : Surround in-line keyed branch.
Scalar indicators:
'''' : Surround in-line unescaped scalar ('' escaped ').
'"' : Surround in-line escaped scalar (see escape codes below).
'|' : Block scalar indicator.
'>' : Folded scalar indicator.
'-' : Strip chomp modifier ('|-' or '>-').
'+' : Keep chomp modifier ('|+' or '>+').
1-9 : Explicit indentation modifier ('|1' or '>2').
# Modifiers can be combined ('|2-', '>+1').
Alias indicators:
'&' : Anchor property.
'*' : Alias indicator.
Tag property: # Usually unspecified.
none : Unspecified tag (automatically resolved by application).
'!' : Non-specific tag (by default, "!!map"/"!!seq"/"!!str").
'!foo' : Primary (by convention, means a local "!foo" tag).
'!!foo' : Secondary (by convention, means "tag:yaml.org,2002:foo").
'!h!foo': Requires "%TAG !h! <prefix>" (and then means "<prefix>foo").
'!<foo>': Verbatim tag (always means "foo").
Document indicators:
'%' : Directive indicator.
'---': Document header.
'...': Document terminator.
Misc indicators:
' #' : Throwaway comment indicator.
'`@' : Both reserved for future use.
Special keys:
'=' : Default "value" mapping key.
'<<' : Merge keys from another mapping.
Core types: # Default automatic tags.
'!!map' : { Hash table, dictionary, mapping }
'!!seq' : { List, array, tuple, vector, sequence }
'!!str' : Unicode string
More types:
'!!set' : { cherries, plums, apples }
'!!omap': [ one: 1, two: 2 ]
Language Independent Scalar types:
{ ~, null } : Null (no value).
[ 1234, 0x4D2, 02333 ] : [ Decimal int, Hexadecimal int, Octal int ]
[ 1_230.15, 12.3015e+02 ]: [ Fixed float, Exponential float ]
[ .inf, -.Inf, .NAN ] : [ Infinity (float), Negative, Not a number ]
{ Y, true, Yes, ON } : Boolean true
{ n, FALSE, No, off } : Boolean false
? !!binary >
R0lG...BADS=
Base 64 binary value.
Escape codes:
Numeric : { "\x12": 8-bit, "\u1234": 16-bit, "\U00102030": 32-bit }
Protective: { "\\": '\', "\"": '"', "\ ": ' ', "\<TAB>": TAB }
C : { "\0": NUL, "\a": BEL, "\b": BS, "\f": FF, "\n": LF, "\r": CR,
"\t": TAB, "\v": VTAB }
Additional: { "\e": ESC, "\_": NBSP, "\N": NEL, "\L": LS, "\P": PS }
(备注: YAML参考链接在 这里 )