八、YAML
00 分钟
2024-6-26
2024-7-22
type
status
date
slug
summary
tags
category
icon
password
文章状态

什么是 YAML ?

YAML(读作 /ˈjæməl/ )的设计目标就是方便人类读写。实质上它是一种通用数据串行化格式
其基本语法规则如下:
其基本语法规则如下:
  • 使用空白与缩进表示层次,可以不使用花括号和方括号;
  • 可使用 # 书写注释,比起 JSON 是很大的改进;
  • 对象(又称字典)的格式与 JSON 基本相同,但 Key 不需要使用双引号;
  • 数组(又称列表)是使用 - 开头的清单形式;
  • 表示对象的 : 和表示数组的 - 后面都必须要有空格;
  • 可使用 --- 在一个文件里分隔多个 YAML 对象。
YAML 支持的 数据结构 有三种:
  • 对象
    • 又称为 映射 (mapping) / 哈希 (hashes) / 字典 (dictionary)
      键值对的集合
  • 数组
    • 又称为 序列 (sequence) / 列表 (list)
      一组按次序排列的值
  • 纯量
    • scalars
      单个的、不可再分的值
How data is stored in YAML
How data is stored in YAML
YAML can contain different kinds of data blocks:
  • Sequence: values listed in a specific order. A sequence starts with a dash and a space (). You can think of a sequence as a Python list or an array in Bash or Perl.
  • Mapping: key and value pairs. Each key must be unique, and the order doesn't matter. Think of a Python dictionary or a variable assignment in a Bash script.
There's a third type called scalar, which is arbitrary data (encoded in Unicode) such as strings, integers, dates, and so on. In practice, these are the words and numbers you type when building mapping and sequence blocks, so you won't think about these any more than you ponder the words of your native tongue.
When constructing YAML, it might help to think of YAML as either a sequence of sequences or a map of maps, but not both.

对象

数组

用于表示键值对的数据结构。每个键值对由一个键和一个值组成,键和值之间用冒号(:)分隔。
 
用于表示有序列表的数据结构。数组中的每个元素通常缩进一个层级,并以破折号(-)开头。

复合结构 ⇒ 对象 + 数组

纯量

标量是 YAML 中最基本的数据类型,包括字符串、整数、浮点数、布尔值、日期和时间等。标量通常用于表示简单的值。纯量是最基本的、不可再分的值。
YAML 支持多种纯量类型,包括但不限于以下几种:
YAML 支持多种纯量类型,包括但不限于以下几种:
  1. 字符串(Strings)
    1. 整数(Integers)
      1. 浮点数(Floats)
        1. 布尔值(Booleans)
          1. truefalse 表示。
        1. 日期(Dates)
          1. 时间(Times)
            1. 日期时间(Date-Times)
              1. 使用 ISO 8601 格式的时间表示法
                ISO 8601 是一种国际标准,用于表示日期和时间。
                ISO 8601 是一种国际标准,用于表示日期和时间。
                具体来说:
                • 2023-04-01 表示日期,格式为 YYYY-MM-DD
                • T 是日期和时间之间的分隔符。
                • 12:30:00 表示时间,格式为 HH:MM:SS
                • Z 表示 UTC 时间,即零时区。
                即,2023-04-01T12:30:00Z 表示 UTC 时间 2023 年 4 月 1 日 12 时 30 分 0 秒。
            1. 空值(Nulls)
              1. 二进制数据(Binary Data)

                字符串

                字符串是最常见,也是最复杂的一种数据类型。在 YAML 中,纯量字符串(Scalar Strings)可以通过多种方式表示,具体取决于字符串的内容和格式要求。
                无引号字符串
                默认不使用引号表示
                单引号字符串、双引号字符串
                若字符串之中包含 空格或特殊字符,需要放在引号之中——单引号和双引号都可以使用,双引号不会对特殊字符转义。
                多行字符串
                字符串可以写成多行,从第二行开始,必须有一个 单空格 缩进。换行符会被转为空格。
                多行字符串可以使用 | 保留换行符,也可以使用 > 折叠换行。
                + 表示保留文字块末尾的换行,- 表示删除字符串末尾的换行。
                 
                引用 REF :
                上一篇
                九、 docker compose
                下一篇
                七、Docker 资源限制