# yaml 文件妙用

返回:java

YAML 是一种常用于数据序列化的文件格式

数据格式:List、Map、Object、基本数据类型

  • 键后跟英文冒号(:)、空格与值
  • 字符串可不打引号、可打单引号或者双引号
  • List、Map、Object
# [{},{},{}…]
lists: [{name: "ht"}, {name: "limin"}, {name: "chenyi"}]
listString: [ht, limin, chenyi]

# -换行后键值
lists:
  -
    name: ht
  -
    name: limin
  -
    name: chenyi
    
# -{}
lists:
  - {name: "ht"}
  - {name: "limin"}
  - {name: "chenyi"}
  
listString:
  - ht
  - limin
  - chenyi
  
maps: {key1: {name: "ht"}, key2: {name: "limin"}, key3: {name: "chneyi"}}
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

# 大多数程序员都不知道的 6 个 YAML 功能

# 等效符号

list_by_dash:
  - foo
  - bar
list_by_square_bracets: [foo, bar]
map_by_indentation:
  foo: bar
  bar: baz
map_by_curly_braces: { foo: bar, bar: baz }
string_no_quotes: Monty Python
string_double_quotes: "Monty Python"
string_single_quotes: "Monty Python"
bool_english: yes
bool_english_no: no
bool_python: True
bool_json: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

language: no # ISO 639-1 code for the Norwegian language

此否被解释为 false。您需要输入“ no”或“ no”。

  • 013映射到11,因为前导零触发八进制表示法
  • 4:30映射到270。Max Werner Kaul-Gothe 和 Niklas Baumstark 告诉我,这被自动转换为分钟(或秒?),因为它被解释为持续时间:4 * 60 + 30 = 270。有趣的是,这种模式仍然可以在 1:1:1:1:1:1:1:1:4:30 的情况下“工作”

# 长字符串

disclaimer: >
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  In nec urna pellentesque, imperdiet urna vitae, hendrerit
  odio. Donec porta aliquet laoreet. Sed viverra tempus fringilla.
1
2
3
4

等效于

{
  "disclaimer": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In nec urna pellentesque, imperdiet urna vitae, hendrerit odio. Donec porta aliquet laoreet. Sed viverra tempus fringilla."
}
1
2
3

# 多行字符串

mail_signature: |
  Martin Thoma
  Tel. +49 123 4567
1
2
3
{ "mail_signature": "Martin Thoma\nTel. +49 123 4567" }
1

请注意如何忽略前导空格。第一行(“ Martin Thoma”)确定忽略的前导空白的数量

#

email: &emailAddress "info@example.de"
id: *emailAddress
1
2
{ "email": "info@example.de", "id": "info@example.de" }
1

定义了一个变量 emailAddress,其值为“ info@example.de。然后,*表示紧随其后的是变量名。

foo: &default_settings
  db:
    host: localhost
    name: main_db
    port: 1337
  email:
    admin: admin@example.com
prod: *default_settings
dev: *default_settings
1
2
3
4
5
6
7
8
9
{
  "dev": {
    "db": { "host": "localhost", "name": "main_db", "port": 1337 },
    "email": { "admin": "admin@example.com" }
  },
  "foo": {
    "db": { "host": "localhost", "name": "main_db", "port": 1337 },
    "email": { "admin": "admin@example.com" }
  },
  "prod": {
    "db": { "host": "localhost", "name": "main_db", "port": 1337 },
    "email": { "admin": "admin@example.com" }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

您可能想在开发和生产设置中插入密码。您可以使用合并键<<来做到这一点:

foo: &default_settings
  db:
    host: localhost
    name: main_db
    port: 1337
  email:
    admin: admin@example.com
prod:
  <<: *default_settings
  app:
    port: 80
dev: *default_settings
1
2
3
4
5
6
7
8
9
10
11
12
{
  "foo": {
    "db": { "host": "localhost", "name": "main_db", "port": 1337 },
    "email": { "admin": "admin@example.com" }
  },
  "prod": {
    "app": { "port": 80 },
    "db": { "host": "localhost", "name": "main_db", "port": 1337 },
    "email": { "admin": "admin@example.com" }
  },
  "dev": {
    "db": { "host": "localhost", "name": "main_db", "port": 1337 },
    "email": { "admin": "admin@example.com" }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 类型转化

双重!在 YAML 中有特殊含义。

price: !!float 42
id: !!str 42
1
2

或更复杂的内容,例如映射到直接在 YAML 中未指定的默认 Python 类型:

tuple_example: !!python/tuple
  - 1337
  - 42
set_example: !!set { 1337, 42 }
date_example: !!timestamp 2020-12-31
1
2
3
4
5

# 一个 YAML 中的多个文档

YAML 中的三个破折号分别表示文档:

foo: bar
---
fizz: buzz
1
2
3

会得到:

[{ "foo": "bar" }, { "fizz": "buzz" }]
1