概述

2022.06.19

本文按照YAML官方文档1.2.2版本翻译总结而成

https://yaml.org/spec/1.2.2/#chapter-2-language-overview

This section provides a quick glimpse into the expressive power of YAML. It is not expected that the first-time reader grok all of the examples. Rather, these selections are used as motivation for the remainder of the specification.

本节简要介绍YAML的表达能力。第一次阅读的读者不可能熟读所有的例子。相反,这些选择被用作规范其余部分的动机。

2.1. 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 pair. Comments begin with an octothorpe (also called a “hash”, “sharp”, “pound” or “number sign” - “#”).

Example 2.1 Sequence of Scalars (ball players)

Example 2.2 Mapping Scalars to Scalars (player statistics)

Example 2.3 Mapping Scalars to Sequences (ball clubs in each league)

Example 2.4 Sequence of Mappings (players’ statistics)

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.

Example 2.5 Sequence of Sequences

Example 2.6 Mapping of Mappings

2.2. 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.

Example 2.7 Two Documents in a Stream (each with a leading comment)

Example 2.8 Play by Play Feed from a Game

Repeated nodes (objects) are first identified by an anchor (marked with the ampersand - “&”) and are then aliased (referenced with an asterisk - “*”) thereafter.

重复的节点(对象)首先由标识(用&号标记),然后别名(用星号引用- " ' * ' ")。

Example 2.9 Single Document with Two Comments

Example 2.10 Node for “Sammy Sosa” appears twice in this document

Sammy Sosa的节点在本文档中出现了两次

A question mark and space (“?”) indicate a complex mapping key. Within a block collection, key/value pairs can start immediately following the dash, colon or question mark.

一个问号和一个空格(" ' ?' ")表示复杂的映射 key。在块集合中,键/值对可以紧跟着破折号冒号问号

Example 2.11 Mapping between Sequences

Example 2.12 Compact Nested Mapping

2.3. Scalars

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.

Example 2.13 In literals, newlines are preserved

在字面值中,保留换行符

Example 2.14 In the folded scalars, newlines become spaces

在折叠的标量中,换行成为空格

Example 2.15 Folded newlines are preserved for “more indented” and blank lines

为“更缩进”和空白行保留折叠换行

下面是python-IDE中的案例

Example 2.16 Indentation determines scope

YAML’s flow scalars include the plain style (most examples thus far) and two quoted styles. The double-quoted style provides escape sequences. The single-quoted style is useful when escaping is not needed. All flow scalars can span multiple lines; line breaks are always folded.

YAML的流标量包括普通样式(到目前为止的大多数示例)和两个引用的样式。双引号样式提供转义序列。当不需要转义时,单引号样式很有用。所有流量标量可以跨越多行;换行符总是折叠

Example 2.17 Quoted Scalars

Example 2.18 Multi-line Flow Scalars

2.4. Tags

In YAML, untagged nodes are given a type depending on the application. The examples in this specification generally use the seq, map and str types from the fail safe schema. A few examples also use the int, float and null types from the JSON schema.

在YAML中,无标记节点根据应用场景给出了一种类型。本规范中的示例通常使用fail safe schema中的seqmapstr类型。一些例子也使用了JSON模式中的intfloatnull类型。

Example 2.19 Integers

Example 2.20 Floating Point

Example 2.21 Miscellaneous

Example 2.22 Timestamps

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 handle. Application-specific local tags may also be used.

显式输入用标记表示,并使用感叹号("!")符号。Global tags是URIs,可以使用handletag简写表示法中指定。Application-specific local tags也可以使用。

Example 2.23 Various Explicit Tags

Example 2.24 Global Tags

Example 2.25 Unordered Sets

Example 2.26 Ordered Mappings

2.5. Full Length Example

Below are two full-length examples of YAML. The first is a sample invoice; the second is a sample log file.

下面是两个完整的YAML示例。第一个是发票样本;第二个是示例日志文件。

Example 2.27 Invoice

Example 2.28 Log File