这是理解和编写 YAML 格式配置文件的快速参考备忘单。
YAML 是一种数据序列化语言,旨在供人类直接读写
.yaml
.yml
n1: 1 # 整数 n2: 1.234 # 浮点 s1: 'abc' # 字符串 s2: "abc" # 字符串 s3: abc # 字符串 b: false # 布尔类型 d: 2015-04-05 # 日期类型
{ "n1": 1, "n2": 1.234, "s1": "abc", "s2": "abc", "s3": "abc", "b": false, "d": "2015-04-05" }
使用空格缩进。 元素部分之间必须有空间。
some_thing: &VAR_NAME foobar other_thing: *VAR_NAME
{ "some_thing": "foobar", "other_thing": "foobar" }
# A single line comment example # block level comment example # comment line 1 # comment line 2 # comment line 3
description: | hello world
{"description": "hello\nworld\n"}
parent: &defaults a: 2 b: 3 child: <<: *defaults b: 4
{ "parent": { "a": 2, "b": 3 }, "child": { "a": 2, "b": 4 } }
values: &ref - Will be - reused below other_values: i_am_ref: *ref
{ "values": [ "Will be", "reused below" ], "other_values": { "i_am_ref": [ "Will be", "reused below" ] } }
description: > hello world
{"description": "hello world\n"}
--- document: this is doc 1 --- document: this is doc 2
YAML使用---将指令与文档内容分开。
---
- Mark McGwire - Sammy Sosa - Ken Griffey
[ "Mark McGwire", "Sammy Sosa", "Ken Griffey" ]
hr: 65 # Home runs avg: 0.278 # Batting average rbi: 147 # Runs Batted In
{ "hr": 65, "avg": 0.278, "rbi": 147 }
attributes: - a1 - a2 methods: [getter, setter]
{ "attributes": ["a1", "a2"], "methods": ["getter", "setter"] }
children: - name: Jimmy Smith age: 15 - name: Jimmy Smith age: 15 - name: Sammy Sosa age: 12
{ "children": [ {"name": "Jimmy Smith", "age": 15}, {"name": "Jimmy Smith", "age": 15}, {"name": "Sammy Sosa", "age": 12} ] }
my_sequences: - [1, 2, 3] - [4, 5, 6] - - 7 - 8 - 9 - 0
{ "my_sequences": [ [1, 2, 3], [4, 5, 6], [7, 8, 9, 0] ] }
Mark McGwire: {hr: 65, avg: 0.278} Sammy Sosa: { hr: 63, avg: 0.288 }
{ "Mark McGwire": { "hr": 65, "avg": 0.278 }, "Sammy Sosa": { "hr": 63, "avg": 0.288 } }
Jack: id: 1 name: Franc salary: 25000 hobby: - a - b location: {country: "A", city: "A-A"}
{ "Jack": { "id": 1, "name": "Franc", "salary": 25000, "hobby": ["a", "b"], "location": { "country": "A", "city": "A-A" } } }
set1: !!set ? one ? two set2: !!set {'one', "two"}
{ "set1": {"one": null, "two": null}, "set2": {"one": null, "two": null} }
集合表示为一个映射,其中每个键都与一个空值相关联
ordered: !!omap - Mark McGwire: 65 - Sammy Sosa: 63 - Ken Griffy: 58
{ "ordered": [ {"Mark McGwire": 65}, {"Sammy Sosa": 63}, {"Ken Griffy": 58} ] }
基于 YAML.org refcard。
%
...
?
:
-
,
[]
{}
&
*
=
<<
''
"
>
|-
>-
+
|+
>+
1-9
|1
>2
|2-
>+1
none
!
!!map
!!seq
!!str
!foo
!!foo
tag:yaml.org,2002:foo
!h!foo
%TAG !h! <prefix>
<prefix>foo
!<foo>
#
`@
{Hash table, dictionary, mapping}
{List, array, tuple, vector, sequence}
\x12
\u1234
\U00102030
\\
\"
\
\<TAB>
\0
\a
\b
\f
\n
\r
\t
\v
\e
\_
\N
\L
\P
!!set
{cherries, plums, apples}
!!omap
[one: 1, two: 2]
{~, null}
[1234, 0x4D2, 02333]
[1_230.15, 12.3015e+02]
[.inf, -.Inf, .NAN]
{Y, true, Yes, ON}
{n, FALSE, No, off}