YAML 是一种面向人类可读可写的数据序列化语言
.yaml or .yml extensionn1: 1 # integer
n2: 1.234 # float
s1: 'abc' # string
s2: "abc" # string
s3: abc # string
b: false # boolean type
d: 2015-04-05 # date type
{
"n1": 1,
"n2": 1.234,
"s1": "abc",
"s2": "abc",
"s3": "abc",
"b": false,
"d": "2015-04-05"
}
Use spaces to indent. There must be space between the element parts.
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
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"
]
}
}
---
document: this is doc 1
---
document: this is doc 2
YAML uses --- to separate directives from document content.
document: this is doc 2
YAML 使用 `---` 符号分隔同一个文件内的多个文档内容。
## YAML 集合类型 (YAML Collections)
### 列表/序列 (Sequence)
```yaml
- Mark McGwire
- Sammy Sosa
- Ken Griffey
↓ 等价的 JSON 输出
[
"Mark McGwire",
"Sammy Sosa",
"Ken Griffey"
]
hr: 65 # 本垒打
avg: 0.278 # 打击率
rbi: 147 # 打点数
↓ 等价的 JSON 输出
{
"hr": 65,
"avg": 0.278,
"rbi": 147
}
attributes:
- a1
- a2
methods: [getter, setter]
↓ 等价的 JSON 输出
{
"attributes": ["a1", "a2"],
"methods": ["getter", "setter"]
}
children:
- name: Jimmy Smith
age: 15
- name: Jimmy Smith
age: 15
-
name: Sammy Sosa
age: 12
↓ 等价的 JSON 输出
{
"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
↓ 等价的 JSON 输出
{
"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 }
↓ 等价的 JSON 输出
{
"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' }
↓ 等价的 JSON 输出
{
"Jack": {
"id": 1,
"name": "Franc",
"salary": 25000,
"hobby": ["a", "b"],
"location": {
"country": "A",
"city": "A-A"
}
}
}
set1: !!set
? one
? two
set2: !!set { 'one', 'two' }
↓ 等价的 JSON 输出
{
"set1": { "one": null, "two": null },
"set2": { "one": null, "two": null }
}
无序集合 (Set) 被表示为键关联了 null 值的映射字典。
ordered: !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58
↓ 等价的 JSON 输出
{
"ordered": [
{ "Mark McGwire": 65 },
{ "Sammy Sosa": 63 },
{ "Ken Griffy": 58 }
]
}
参考自 YAML.org 官方参考卡片。
| 符号 | 描述说明 |
|---|---|
% |
指令标记符 (Directive indicator) |
--- |
文档起始头标记 (Document header) |
... |
文档结束标记 (Document terminator) |
| 符号 | 描述说明 |
|---|---|
? |
键标记符 (Key indicator) |
: |
值标记符 (Value indicator) |
- |
嵌套序列元素标记符 |
, |
内联分支元素分隔符 |
[] |
包裹内联序列分支 |
{} |
包裹内联键值字典分支 |
| 符号 | 描述说明 |
|---|---|
& |
锚点定义属性 (Anchor property) |
* |
别名引用标记 (Alias indicator) |
| 符号 | 描述说明 |
|---|---|
= |
默认 "value" 映射键 |
<< |
从另一个映射合并所有键值 (Merge) |
| 符号 | 描述说明 |
|---|---|
'' |
单引号包裹内联未转义标量 |
" |
双引号包裹内联转义标量 |
| |
保留换行的块级标量标记符 |
> |
折叠换行的标量标记符 |
- |
裁切换行修饰符 (|- 或 >-) |
+ |
保留末尾换行修饰符 (|+ 或 >+) |
1-9 |
显式缩进层级修饰符 (|1 或 >2),修饰符可组合 (|2-, >+1) |
| 标签语法 | 描述说明 |
|---|---|
none |
未指定标签(由应用程序自动推导) |
! |
非特定标签(默认按 !!map/!!seq/!!str 解析) |
!foo |
主标签(按约自定义的本地 !foo 标签) |
!!foo |
官方规范二级标签(即 tag:yaml.org,2002:foo) |
!h!foo |
需提前定义 %TAG !h! <prefix>(解析为 <prefix>foo) |
!<foo> |
逐字标签(严格解析为 foo) |
| 符号 | 描述说明 |
|---|---|
# |
注释标记符 |
`@ |
保留供未来版本扩展使用 |
| 核心标签 | 数据结构对应 |
|---|---|
!!map |
{哈希表、字典、映射} |
!!seq |
{列表、数组、元组、向量、序列} |
!!str |
Unicode 文本字符串 |
数值编码转义
\x12 (8 位 hex)\u1234 (16 位 hex)\U00102030 (32 位 hex)特殊字符转义
\\ (反斜杠 \)\" (双引号 ")\ (空格)\<TAB> (制表符 TAB)C 语言传统转义
\0 (空字符 NUL)\a (响铃 BEL)\b (退格 BS)\f (换页 FF)\n (换行 LF)\r (回车 CR)\t (制表符 TAB)\v (垂直制表符 VTAB)补充转义字符
\e (Escape ESC)\_ (不换行空格 NBSP)\N (下一行 NEL)\L (行分隔符 LS)\P (段落分隔符 PS)| 扩展标签 | 示例对应 |
|---|---|
!!set |
{cherries, plums, apples} |
!!omap |
[one: 1, two: 2] |
| 表达式类型 | 对应具体含义 |
|---|---|
{~, null} |
Null 空值 (无值) |
[1234, 0x4D2, 02333] |
[十进制整数, 十六进制整数, 八进制整数] |
[1_230.15, 12.3015e+02] |
[定点浮点数, 科学计数法浮点数] |
[.inf, -.Inf, .NAN] |
[正无穷大, 负无穷大, 非数字 NaN] |
{Y, true, Yes, ON} |
布尔真 (Boolean true) |
{n, FALSE, No, off} |
布尔假 (Boolean false) |