YAML学习笔记

什么是 YAML?

yaml官网

YAML: YAML Ain’t Markup Language™
What It Is:
YAML is a human-friendly data serialization
language for all programming languages.
It uses Unicode printable characters, some of which provide structural information and the rest containing the data itself.
There are many kinds of data structures, but they can all be adequately represented with three basic primitives: mappings (hashes/dictionaries), sequences (arrays/lists) and scalars (strings/numbers).
YAML was specifically created to work well for common use cases such as: configuration files, log files, interprocess messaging, cross-language data sharing, object persistence and debugging of complex data structures.

YAML:YAML Ain’t Markup Language™
它是什么:
YAML是一种人性化的数据序列化语言,适用于所有编程语言。
它使用Unicode 可打印 的字符,其中一些提供结构信息,其余包含数据本身。
有许多种数据结构,但它们都可以用三个基本原语充分表示:映射(哈希/字典)、序列(数组/列表)和标量(字符串/数字)。
YAML是专门为常见用例而创建的,例如:配置文件、日志文件、进程间消息传递、跨语言数据共享、对象持久化和调试复杂数据结构。

YAML 文件使用 .yml 或 .yaml 扩展名,并遵循特定的语法规则。
YAML是一种简单、表达力强的、以数据为导向的语言,它不是一种标记语言 (YAML),作为配置格式时非常易读。
它具有直观的可视化结构,并且其逻辑非常简单:==缩进的项目继承父项目的属性==。
YAML 拥有 Perl、C、XML、HTML 和其他编程语言的特性。
YAML 也是 JSON 的超集,所以 ==JSON 文件在 YAML 中有效==。
没有通常的格式符号,如大括号、方括号、结束标记或引号。YAML 文件更容易阅读,因为其使用 Python 风格的缩进来确定结构并表示嵌套。为了保持跨系统的可移植性,设计时不允许使用制表符,因此改用空格(字面意义的空格字符)。
注释可以用井号或哈希符号(#)。使用注释始终是最佳实践,因为其可描述代码的意图。==YAML 不支持多行注释,每行都需要以井号字符为后缀==。
YAML 文件的结构是映射或列表,遵循层次结构,具体取决于缩进以及定义键值的方式。它以映射来关联键值对。每个键必须是唯一的,而且顺序并不重要。这跟 Python 字典或 Bash 脚本中的变量赋值相似。
YAML 中的映射必须经过解析后才能关闭,然后创建新的映射。新的映射可以通过增加缩进级别或解析之前的映射并新建一个相邻映射来创建。
列表包括以特定顺序列出的值,可以包含任何数量的所需项目。==一个列表序列以破折号(-)和空格开始,而缩进则将其与父级分开==。
我们可以把序列看作是一个 Python 列表或者 Bash 或 Perl 中的数组。一个列表可以嵌入到一个映射中。
YAML 还包含标量,它是以 Unicode 编码的任意数据,可以用作字符串、整数、日期、数字或布尔等值。
在创建 YAML 文件时,需要确保遵循这些语法规则,而且文件必须有效。

(摘自Red Hat红帽What is YAML?

YAML 有什么用途?

配置文件:YAML是一种常用的配置文件格式,它可以轻松地表示各种配置选项和参数。相比 JSON,因为 YAML 有更好的可读性,对用户更友好,所以通常建议用 YAML 来编写配置文件,尽管它们在大多数情况下可以互换使用。

数据传输:YAML可以用于在不同的应用程序或系统之间传输数据,因为它是一种跨平台的数据格式,易于解析和处理。

持久化存储:YAML可以用于将数据序列化为文本格式并进行持久化存储,例如将数据存储到文件或数据库中。

除了在 Ansible 中使用之外,YAML 还用于 Kubernetes 资源和部署
(Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能)
(网页Kubernetes 也称为 K8s,是用于自动部署、扩缩和管理容器化应用程序的开源系统)
使用 YAML 的一大好处是,YAML 文件可以添加到源代码控制中,比如 Github,这样就可以跟踪和审计变更。
(摘自自Red Hat红帽YAML for beginners)

YAML基本语法

  • ==大小写敏感==
  • 使用缩进表示层级关系
  • 缩进时不允许使用 Tab 键,只允许使用空格。
  • 缩进的空格数不重要,但同级元素必须左侧对齐。
  • ‘#’表示注释

集(Collections)

YAML’s block collections use indentation for scope and begin each entry on its own line. Block sequences indicate each entry with a dash and space (“- ”). Mappings use a colon and space (“: ”) to mark each key/value pairComments begin with an octothorpe (also called a “hash”, “sharp”, “pound” or “number sign” - “#”).
YAML also has flow styles, using explicit indicators rather than indentation to denote scope. The flow sequence is written as a comma separated list within square brackets. In a similar manner, the flow mapping uses curly braces.

YAML的block collections(块集)使用缩进表示作用域,并在每个条目的单独一行上开始。Block sequences(块序列)使用破折号和空格(“- “)表示每个条目。Mappings(映射)使用冒号和空格(“: “)分隔键/值对Comments(注释)以八角符号(也称为“hash”、“sharp”、“pound”或“number sign” - “#“)开头。
YAML还具有flow styles(流程样式),使用显式的指示符而不是缩进表示作用域。Flow sequence(流序列)以逗号分隔的列表形式写在方括号内。类似地,flow mapping(流映射)使用花括号表示。

结构(Structures)

YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present. Three dots ( “...”) indicate the end of a document without starting a new one, for use in communication channels.

YAML使用三个破折号(“—”)将指令与文档内容分开。如果没有指令,这也表示文档的开始。三个点(“…”)表示文档的结束,不会开始新的文档,适用于通信渠道。

YAML数据类型

YAML represents any native data structure using three node kindssequence - an ordered series of entries; mapping - an unordered association of unique keys to values; and scalar - any datum with opaque structure presentable as a series of Unicode characters.
Nodes
A YAML node represents a single native data structure. Such nodes have content of one of three kinds: scalar, sequence or mapping. In addition, each node has a tag which serves to restrict the set of possible values the content can have.

YAML使用三种节点类型来表示任何本地数据结构:序列(sequence)- 有序的条目序列;映射(mapping)- 无序的键值对;标量(scalar)- 任何具有不透明结构的数据,可以表示为一系列Unicode字符。
节点
一个YAML节点表示一个单一的本地数据结构。这样的节点有三种内容种类:标量(scalar)、序列(sequence)或映射(mapping)。此外,每个节点都有一个标记,用于限制内容可能具有的可能值集。

YAML 支持以下几种数据类型:

  • 标量(scalar):单个的、不可再分的值
  • 序列(sequence):一组按次序排列的值,又称为列表(list)
  • 映射(mapping):键值对的集合,又称为哈希(hashes) / 字典(dictionary)

标量(scalar)

Scalar content can be written in block notation, using a literal style (indicated by “|”) where all line breaks are significant. Alternatively, they can be written with the folded style (denoted by “>”) where each line break is folded to a space unless it ends an empty or a more-indented line.

标量内容可以使用块表示法编写,使用字面样式(由“|”表示),其中所有换行都是重要的。或者,它们可以使用折叠样式(由“>”表示)编写,其中每个换行符折叠成空格,除非它结束一个空行或更多缩进的行。

  • 字符串
  • 布尔值
  • 整数
  • 浮点数
  • Null
  • 时间
  • 日期
    纯量直接写在键值对的“value”中。
    使用key:[空格]value的形式表示一对键值对(空格不能省略)

字符串(String)

Generic String
Kind
Scalar
Definition
Represents a Unicode string, a sequence of zero or more Unicode characters. This type is usually bound to the native language’s string type or, for languages lacking one (such as C), to a character array.

通用字符串
类型
标量
定义
表示Unicode字符串,即零个或多个Unicode字符的序列。该类型通常绑定到本地语言的字符串类型,或者对于缺乏该类型的语言(如C语言)绑定到字符数组。

字符串是一系列字符序列,可以使用单引号或双引号括起来,也可以不使用引号。如果字符串中包含特殊字符,应使用引号括起来。

1
2
3
name: 'Alice'
age: 30
address: "123 Main St, New York"

单引号和双引号的使用情况

单引号:使用单引号表示一个纯量字符串,该字符串==不会被转义,也不会被解析为特殊字符或变量==。
单引号字符串中的任何特殊字符都会被视为普通字符。例如:

1
2
name: 'zhangsan \n lisi'
name: 'John Doe'

输出结果为:

1
2
zhangsan \n lisi
John Doe

双引号:使用双引号表示一个字符串,==该字符串可以包含转义字符和变量,也可以包含特殊字符序列,特殊字符会输出为其本身想表达的含义,例如可以包含换行符,==。
双引号字符串中的变量==可以使用${}语法进行插值==。例如:

1
2
name: zhangsan \n lisi
name: "My name is ${name}."

输出结果为:

1
2
3
4
5
zhangsan
lisi

My name is ${name}
#若name有传值,例如Anna,则为My name is ${Anna}

数字(Number)

数字可以是整数或浮点数。

Integer
Kind
Scalar.
Definition
Represents arbitrary sized finite mathematical integers. Scalars of this type should be bound to a native integer data type, if possible.
Some languages (such as Perl) provide only a “number” type that allows for both integer and floating-point values. A YAML processor may use such a type for integers as long as they round-trip properly.
In some languages (such as C), an integer may overflow the native type’s storage capability. A YAML processor may reject such a value as an error, truncate it with a warning or find some other manner to round-trip it. In general, integers representable using 32 binary digits should safely round-trip through most systems.

整数
类型
标量
定义
表示任意大小的有限数学整数。如果可能,此类型的标量应绑定到本地的整数数据类型。
一些语言(如Perl)仅提供允许整数和浮点数值的“数字”类型。只要它们正确地往返传输,YAML处理器可以将这种类型用于整数。
在一些语言中(如C语言)中,整数可能会超出本地类型的存储能力。YAML处理器可以将这样的值拒绝为错误,使用警告截断它或找到其他方法往返传输。一般来说,可以使用32位二进制数字表示的整数应该能够安全地通过大多数系统往返传输。

Floating Point
Kind
Scalar.
Definition
Represents an approximation to real numbers, including three special values (positive and negative infinity and “not a number”).
Some languages (such as Perl) provide only a “number” type that allows for both integer and floating-point values. A YAML processor may use such a type for floating-point numbers, as long as they round-trip properly.
Not all floating-point values can be stored exactly in any given native type. Hence a float value may change by “a small amount” when round-tripped. The supported range and accuracy depends on the implementation, though 32 bit IEEE floats should be safe. Since YAML does not specify a particular accuracy, using floating-point mapping keys requires great care and is not recommended.

浮点数
类型
标量
定义
表示实数的近似值,包括三个特殊值(正无穷、负无穷和“非数字”)。
一些语言(如Perl)仅提供允许整数和浮点值的“数字”类型。只要它们正确地往返传输,YAML处理器可以将这种类型用于浮点数。
并非所有浮点数值都可以在任何给定的本地类型中精确存储。因此,浮点值可能在往返传输时“略微变化”。支持的范围和精度取决于实现,但32位IEEE浮点数应该是安全的。由于YAML不指定特定的精度,因此使用浮点映射键需要格外小心,不建议使用。

1
2
number: 42
float: 3.14

布尔值(Boolean)

Boolean
Kind
Scalar.
Definition
Represents a true/false value. In languages without a native Boolean type (such as C), they are usually bound to a native integer type, using one for true and zero for false.

布尔值
类型
标量
定义
表示真或假的值。在没有本地布尔类型的语言(如C语言)中,它们通常绑定到本地的整数类型,使用1表示true,使用0表示false

布尔值可以是true或false。

1
2
is_student: true
is_adult: false

空值(Null)

Null
Kind
Scalar.
Definition
Represents the lack of a value. This is typically bound to a native null-like value (e.g., undef in Perl, None in Python). Note that a null is different from an empty string. Also, a mapping entry with some key and a null value is valid and different from not having that key in the mapping.

空值
类型
标量
定义
表示缺少值。这通常绑定到本地的类似null的值(如Perl中的undef,Python中的None)。请注意,null与空字符串不同。此外,具有某个键和null值的映射条目是有效的,并且与在映射中没有该键不同。

空值表示缺少一个值,可以使用null关键字表示,也可以用~表示。

1
value: null

日期和时间(Date and Time)

日期和时间时间采用 ISO8601 格式表示。

1
2
3
date: 2022-08-01
time: 20:30:00
datetime: 2022-08-01T20:30:00Z
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
# 数据类型示例
name: 'Alice'
age: 30
is_student: true
weight: 65.5
birthday: 1990-01-01

boolean:
- TRUE #true,True都可以
- FALSE #false,False都可以
float:
- 3.14
- 6.8523015e+5 #可以使用科学计数法
int:
- 123
- 0b1010_0111_0100_1010_1110 #二进制表示
null:
nodeName: 'node'
parent: ~ #使用~表示null
string:
- 哈哈
- 'Hello world' #可以使用双引号或者单引号包裹特殊字符
- newline
newline2 #字符串可以拆成多行,每一行会被转化成一个空格
date:
- 2018-02-17 #日期必须使用ISO 8601格式,即yyyy-MM-dd
datetime:
- 2018-02-17T15:02:31+08:00 #时间使用ISO 8601格式,时间和日期之间使用T连接,最后使用+代表时区

类型转换

YAML 允许使用两个感叹号,强制转换数据类型

1
2
e: !!str 123
f: !!str true

序列(Sequence)

Generic Mapping
Kind
Sequence.
Definition
Represents a collection indexed by sequential integers starting with zero. Example bindings to native types include Perl’s array, Python’s list or tuple and Java’s array or Vector.

通用序列
类型
序列
定义
表示一组以从零开始的顺序整数索引的集合。绑定到本地类型的示例包括Perl的数组、Python的列表或元组和Java的数组或向量。

序列(Sequence):序列是一种元素的有序集合,每个元素可以是任何类型的值,例如字符串、数字、布尔值、映射或序列。

使用短横线和空格表示一个新的元素。
使用-表示序列中的元素。
序列使用方括号来表示,元素之间使用逗号分隔。

1
2
3
4
5
# 序列示例
fruits:
- Apple
- Banana
- Orange
1
2
3
- A
- B
- C
1
pets: [dog,cat,pig]
1
key: [value1, value2, ...]

一个相对复杂的例子:

1
2
3
4
5
6
7
8
9
companies:
-
id: 1
name: company1
price: 200W
-
id: 2
name: company2
price: 500W

意思是 companies 属性是一个序列,每一个序列元素又是由 id、name、price 三个属性构成。

序列也可以使用流式(flow)的方式表示:

1
companies: [{id: 1,name: company1,price: 200W},{id: 2,name: company2,price: 500W}]

映射(Mapping)

Generic Mapping
Kind
Mapping
Definition
Represents an associative container, where each key is unique in the association and mapped to exactly one value. YAML places no restrictions on the type of keys; in particular, they are not restricted to being scalars. Example bindings to native types include Perl’s hash, Python’s dictionary and Java’s Hashtable.

通用映射
类型
映射
定义
表示一个关联容器,其中每个键在关联中是唯一的,并映射到一个值。YAML对键的类型没有限制;特别是,它们不限于标量。绑定到本地类型的示例包括Perl的哈希、Python的字典和Java的哈希表。

映射(Map):使用冒号来分隔键值对,使用短横线和空格来表示一个新的键值对。==key: value==
映射使用大括号来表示,键值对之间使用逗号分隔。

1
2
3
key: 
child-key: value
child-key2: value2
1
2
3
website: 
name: bianchengbang
url: www.biancheng.net
1
2
3
4
5
6
7
# 映射示例
person:
name: Alice
age: 30
address:
city: New York
street: 123 Main St
1
key:{key1: value1, key2: value2, ...}
1
website: {name: bianchengbang,url: www.biancheng.net}

复杂的映射Mapping

较为复杂的映射格式,可以使用问号加一个空格代表一个复杂的 key,配合一个冒号加一个空格代表一个 value:

1
2
3
4
5
6
?  
- complexkey1
- complexkey2
:
- complexvalue1
- complexvalue2

意思即映射的属性是一个序列 [complexkey1,complexkey2],对应的值也是一个序列 [complexvalue1,complexvalue2]

注释(Comment)

注释以井号(#)开头,可以出现在行末或行中间。
注释可以帮助我们记录和解释YAML文档中的内容。

1
2
3
4
5
6
7
# 注释示例
# 这是一个映射
person:
# 这是一个字符串键
name: Alice
# 这是一个整数键
age: 30

复合结构

以上三种数据结构可以任意组合使用,以实现不同的用户需求,例如:

1
2
3
4
5
6
7
8
9
10
11
12
person:
name: zhangsan
age: 30
pets:
-dog
-cat
-pig
car:
name: QQ
child:
name: zhangxiaosan
age: 2

例:

1
2
3
4
5
6
7
8
9
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org

转换为 json 为:

1
2
3
4
5
6
7
8
9
{ 
languages: [ 'Ruby', 'Perl', 'Python'],
websites: {
YAML: 'yaml.org',
Ruby: 'ruby-lang.org',
Python: 'python.org',
Perl: 'use.perl.org'
}
}

组织结构

缩进(Indentation)

缩进空格 Indentation Spaces

Indentation Spaces
In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line.
To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently. Note that most modern editors may be configured so that pressing the tab key results in the insertion of an appropriate number of spaces.
The amount of indentation is a presentation detail and must not be used to convey content information.
Each node must be indented further than its parent node. All sibling nodes must use the exact same indentation level. However the content of each sibling node may be further indented independently.

缩进空格
缩进是用来表示嵌套结构的方式。
每当一个新的映射或序列开始时,都必须缩进两个或四个空格,以表明其嵌套级别。
缩进的空格数量必须是相同的,不能混用。
另外,缩进也可以用来对齐相同级别的键值对或元素。
每个节点必须比其父节点缩进更多。所有兄弟节点必须使用完全相同的缩进级别。但是,每个兄弟节点的内容可以独立地进一步缩进。

分隔空格Separation Spaces

Separation Spaces
Outside indentation and scalar content, YAML uses white space characters for separation between tokens within a line. Note that such white space may safely include tab characters.
Separation spaces are a presentation detail and must not be used to convey content information.

分隔空格
在缩进和标量内容之外,YAML使用空白字符在行内标记之间进行分隔。请注意,这样的空格可以安全地包括制表符。
分隔空格是表示细节,不能用于传达内容信息。

1
2
3
4
5
6
7
8
# 缩进示例
person:
name: Alice
age: 30
address:
city: New York
street: 123 Main St

锚点、引用、合并

& 锚点(Anchor)

“&”符号用于创建一个锚点(Anchor)。
锚点允许在YAML文档中创建可重用的节点,从而避免重复定义相同的节点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
defaults: &defaults
#defaults映射
adapter: postgres
host: localhost

development:
#development映射
database: myapp_development
<<: *defaults

test:
#test映射
database: myapp_test
<<: *defaults

在这个例子中,锚点defaults定义了一个包含adapterhost键的映射。然后,在developmenttest映射中,使用<<合并键将锚点defaults与这些映射合并,从而==继承锚点中定义的键值对==。

具体来说,<<合并键表示==将当前映射与另一个映射合并,从而继承另一个映射中的键值对==。
*后面跟着锚点的名称,表示要使用该锚点中定义的键值对。在示例中,<<: *defaults表示将defaults锚点中定义的键值对合并到当前映射中。

因此,在给定的YAML示例中,”&”符号的作用是创建一个锚点,并使用”<<”合并键将锚点中定义的键值对合并到其他映射中,从而避免了重复定义相同的键值对的问题。

* 引用(Alias)

Alias Nodes
Subsequent occurrences of a previously serialized node are presented as alias nodes. The first occurrence of the node must be marked by an anchor to allow subsequent occurrences to be presented as alias nodes.
An alias node is denoted by the “*” indicator. The alias refers to the most recent preceding node having the same anchor. It is an error for an alias node to use an anchor that does not previously occur in the document. It is not an error to specify an anchor that is not used by any alias node.
Note that an alias node must not specify any properties or content, as these were already specified at the first occurrence of the node.

别名节点
先前序列化的节点的后续出现形式被表示为别名节点。必须通过锚点标记节点的第一次出现,以允许后续出现形式表示为别名节点。
别名节点由*指示符表示。该别名引用具有相同锚点的最近的先前节点。如果别名节点使用尚未在文档中出现的锚点,则会出现错误。指定一个别名节点未使用的锚点不是错误。
请注意,别名节点不得指定任何属性或内容,因为这些已在节点的第一次出现时指定。

*符号是一个引用标记(anchor reference),用于引用先前定义的锚点(anchor)。

& 锚点和 * 别名,可以用来引用。*符号后跟着锚点的名称,表示要引用该锚点中定义的节点。

引用标记*可以在YAML文档的任何地方使用,包括映射、序列和标量值。它允许在文档中创建可重用的节点,并在需要时引用它们。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
defaults: &defaults
#defaults映射
adapter: postgres
host: localhost

development:
#development映射
database: myapp_development
<<: *defaults

test:
#test映射
database: myapp_test
<<: *defaults

相当于:

1
2
3
4
5
6
7
8
9
10
11
12
13
defaults:
adapter: postgres
host: localhost

development:
database: myapp_development
adapter: postgres
host: localhost

test:
database: myapp_test
adapter: postgres
host: localhost

下面是另一个例子:

1
2
3
4
5
6
- &showell Steve 
- Clark
- Brian
- Oren
- *showell

转为 JavaScript 代码如下:

1
[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]

<< 合并(Merge)

“<<”符号是一个合并键(merge key),它用于将一个映射或者哈希表与另一个映射或哈希表合并。表示合并到当前数据,用来引用锚点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#示例
defaults: &defaults
#defaults映射
adapter: postgres
host: localhost

development:
#development映射
database: myapp_development
<<: *defaults

test:
#test映射
database: myapp_test
<<: *defaults

在给定的示例中,<<: *defaults表示将当前映射与名为default的映射合并。

*defaults是一个锚点(anchor),它是一个可重用的命名节点,可以在其他位置引用它,*defaults引用了名为default的锚点。

如果default锚点被定义为一个映射,那么<<: *defaults将会将当前映射与default映射进行合并。

合并的方式如下:

  1. 当前映射中没有”default”中的键时,将”default”中的所有键值对添加到当前映射中。

  2. 当前映射中有”default”中的键时,将”default”中的键值对覆盖当前映射中对应的键值对。

标签(Tags)

In YAML, untagged nodes are given a type depending on the application. The examples in this specification generally use the seqmap and str types from the fail safe schema. A few examples also use the intfloat and null types from the JSON schema.
Explicit typing is denoted with a tag using the exclamation point (“!”) symbol. Global tags are URIs and may be specified in a tag shorthand notation using a handleApplication-specific local tags may also be used.

在YAML中,未标记的节点根据应用程序赋予一种类型。本规范中的示例通常使用故障安全模式下的seq、map和str类型。一些示例还使用JSON模式下的int、float和null类型。
显式类型使用感叹号(“!”)符号标记。全局标记是URI,可以使用句柄的标记缩写表示。也可以使用特定于应用程序的本地标记。

标签用”!”表示,可以用来标识数据类型或格式。标签可以是内置标签或自定义标签。内置标签包括”!!str”(字符串)、”!!int”(整数)、”!!float”(浮点数)等,自定义标签则可以根据需要进行定义。

1
2
3
- !!str "123"
- !!int "456"
- !!float "3.14"

在上面的示例中,使用了内置标签!!str!!int!!float来标识不同的数据类型。

---文档分隔符

一个 YAML 文件可以由一个或多个文档组成,文档之间使用---作为分隔符,且个文档相互独立,互不干扰。
如果 YAML 文件只包含一个文档,则---分隔符可以省略。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
---
website:
name: bianchengbang
url: www.biancheng.net
---
website: {name: bianchengbang,url: www.biancheng.net}
pets:
-dog
-cat
-pig

---
pets: [dog,cat,pig]

name: "zhangsan \n lisi"

---
name: 'zhangsan \n lisi'

日志文件完整YAML示例

a sample log file

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
---
Time: 2001-11-23 15:01:42 -5
User: ed
Warning:
This is an error message
for the log file
---
Time: 2001-11-23 15:02:31 -5
User: ed
Warning:
A slightly different error
message.
---
Date: 2001-11-23 15:03:17 -5
User: ed
Fatal:
Unknown variable "bar"
Stack:
- file: TopClass.py
line: 23
code: |
x = MoreObject("345\n")
- file: MoreClass.py
line: 58
code: |-
foo = bar

Spring Boot 常用yml示例:

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server:
port: 8080
servlet:
context-path: /myapp
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: myuser
password: mypassword
jpa:
hibernate:
ddl-auto: update
show-sql: true
logging:
level:
root: INFO
com.example.mypackage: DEBUG
myapp:
greeting: Hello, World!
timeout: 5000

参考教程

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信