1. 简介
YAML: YAML Ain’t Markup Language
(/ˈjæməl/ )设计目标是方便人类读写。它实质上是一种通用的数据串行化格式,远比 JSON 格式方便。
1.1 它的基本语法规则如下。
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
#
表示注释,从这个字符一直到行尾,都会被解析器忽略。
1.2 YAML 支持的数据结构有三种。
- 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
- 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
- 纯量(scalars):单个的、不可再分的值
2. 对象
复制代码
1
2
3
4
5
6
7## 使用冒号结构表示: animal: pets # { animal: 'pets' } ## 将所有键值对写成一个行内对象: hash: { name: Steve, foo: bar } # { hash: { name: 'Steve', foo: 'bar' } }
3. 数组
3.1 一组连词线开头的行,构成一个数组:
复制代码
1
2
3
4
5- Cat - Dog - Goldfish # [ 'Cat', 'Dog', 'Goldfish' ]
3.2 二维数组:
复制代码
1
2
3
4
5
6- - Cat - Dog - Goldfish # [ [ 'Cat', 'Dog', 'Goldfish' ] ]
3.3 行内表示法
复制代码
1
2
3animal: [Cat, Dog] # { animal: [ 'Cat', 'Dog' ] }
4. 复合结构
对象和数组可以结合使用,形成复合结构。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26languages: - Ruby - Perl - Python websites: YAML: yaml.org Ruby: ruby-lang.org Python: python.org Perl: use.perl.org # { # languages: [ 'Ruby', 'Perl', 'Python' ], # websites: { # YAML: 'yaml.org', # Ruby: 'ruby-lang.org', # Python: 'python.org', # Perl: 'use.perl.org' # } # }
5. 纯量
纯量是最基本的、不可再分的值。以下数据类型都属于 JavaScript 的纯量。
- 字符串
- 布尔值
- 整数
- 浮点数
- Null
- 时间
- 日期
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20## 数值直接以字面量的形式表示 number: 12.30 # { number: 12.30 } ## 布尔值用true和false表示 isSet: true # { isSet: true } ## null用~表示 parent: ~ # { parent: null } ## 时间采用 ISO8601 格式 date: 2001-12-14t21:59:43.10-05:00 #{ date: new Date('2001-12-14t21:59:43.10-05:00') } ## 日期采用复合 iso8601 格式的年、月、日表示 date: 1976-07-31 # { date: new Date('1976-07-31') } ## 使用两个感叹号,强制转换数据类型 e: !!str 123 f: !!str true # { e: '123', f: 'true' }
6. 字符串
6.1 单行
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14## 字符串默认不使用引号表示 str: 这是一行字符串 # { str: '这是一行字符串' } ## 如果字符串之中包含空格或特殊字符,需要放在引号之中。 str: '内容: 字符串' # { str: '内容: 字符串' } ## 单引号和双引号都可以使用,双引号不会对特殊字符转义。 s1: '内容n字符串' s2: "内容n字符串" # { s1: '内容\n字符串', s2: '内容n字符串' } ## 单引号之中如果还有单引号,必须连续使用两个单引号转义。 str: 'labor''s day' # { str: 'labor's day' }
6.2 多行
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29## 字符串可以写成多行,从第二行开始,必须有一个单空格缩进。换行符会被转为空格。 str: 这是一段 多行 字符串 # { str: '这是一段 多行 字符串' } ## 多行字符串可以使用|保留换行符,也可以使用>折叠换行。 this: | Foo Bar that: > Foo Bar # { this: 'FoonBarn', that: 'Foo Barn' } ## +表示保留文字块末尾的换行,-表示删除字符串末尾的换行。 s1: | Foo s2: |+ Foo s3: |- Foo # { s1: 'Foon', s2: 'Foonnn', s3: 'Foo' } ## 字符串之中可以插入 HTML 标记。 message: | <p style="color: red"> 段落 </p> # { message: 'n<p style="color: red">n 段落n</p>n' }
7. 引用
锚点&
和别名*
,可以用来引用。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12defaults: &defaults adapter: postgres host: localhost development: database: myapp_development <<: *defaults test: database: myapp_test <<: *defaults
等同于下面的代码。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18defaults: adapter: postgres host: localhost development: database: myapp_development adapter: postgres host: localhost test: database: myapp_test adapter: postgres host: localhost
&
用来建立锚点(defaults),<<
表示合并到当前数据,*
用来引用锚点。
下面是另一个例子。
复制代码
1
2
3
4
5
6
7- &showell Steve - Clark - Brian - Oren - *showell # [ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]
参考
阮一峰 YAML 语言教程
在线 Demo
最后
以上就是勤劳冬瓜最近收集整理的关于YAML用法详解的全部内容,更多相关YAML用法详解内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复