YAML 简介 简介 基本语法 特色 结论

2025-05-26

YAML 简介

介绍

基本语法

特色

结论

我第一次接触 YAML 是在大约一年前,当时我使用它编写 OpenAPI 定义,以使用Swagger API 文档记录 RESTful API ,说实话,我真的很讨厌它。

作为 JSON 的“粉丝”,YAML 语法对我来说感觉很奇怪且不自然,所以有一段时间我没有关注它。

几个月前,当我开始接触 CI/CD 时,情况发生了变化,因为 Azure 和 GitLab 管道都需要 YAML 文件来设置。因此,我最终决定好好学习一下 YAML,在阅读了一些资料后,我发现它背后的理念非常吸引人。

在本文中,我将介绍 YAML 的基础知识,包括其主要目标、基本语法和一些更复杂的功能。

目录

介绍

YAML 是一种数据序列化语言,常用于配置文件,例如开放 API 规范CI/CD 管道

有趣的事实!🤓

根据YAML 1.0 规范文档 (2001-05-26),首字母缩略词“YAML”代表“另一种标记语言”,但后来在2002-04-07 规范中更改为递归首字母缩略词“YAML 不是标记语言”

正如最新规范中所述,YAML 旨在对处理数据的人们友好,并通过最大限度地减少结构字符的使用来实现“独特的清洁”,从而使数据以自然而有意义的方式出现。

最新规范还指出,YAML 1.2 符合 JSON作为官方子集的要求,这意味着大多数 JSON 文档都可以解析为 YAML。

YAML 通过使用基于缩进的范围(类似于 Python)轻松检查数据的结构。

另一个有趣的事实!🤓

DEV.to 文章使用 YAML 定义自定义变量,例如标题、描述、标签等。

基本语法

YAML 文档本质上是键值对的集合,其中值可以像字符串一样简单,也可以像树一样复杂。
以下是一些关于 YAML 语法的说明:

  • 缩进用于表示结构。不允许使用制表符,并且只要子节点的缩进量大于父节点,空格的数量就无关紧要。
  • 允许使用 UTF-8、UTF-16 和 UTF-32 编码。

字符串

# Strings don't require quotes:
title: Introduction to YAML

# But you can still use them:
title-w-quotes: 'Introduction to YAML'

# Multiline strings start with |
execute: |
    npm ci
    npm build
    npm test
Enter fullscreen mode Exit fullscreen mode

上述代码转换为 JSON 格式如下:

{
    "title": "Introduction to YAML",
    "title-w-quotes": "Introduction to YAML",
    "execute": "npm ci\nnpm build\nnpm test\n"
}
Enter fullscreen mode Exit fullscreen mode

数字

# Integers:
age: 29

# Float:
price: 15.99

# Scientific notation:
population: 2.89e+6
Enter fullscreen mode Exit fullscreen mode

上述代码转换为 JSON 格式如下:

{
    "age": 29,
    "price": 15.99,
    "population": 2890000
}
Enter fullscreen mode Exit fullscreen mode

布尔值

# Boolean values can be written in different ways:
published: false
published: False
published: FALSE
Enter fullscreen mode Exit fullscreen mode

以上所有内容将转换为 JSON 格式:

{
    "published": false
}
Enter fullscreen mode Exit fullscreen mode

空值

# Null can be represented by simply not setting a value:
null-value: 

# Or more explicitly:
null-value: null
null-value: NULL
null-value: Null
Enter fullscreen mode Exit fullscreen mode

以上所有内容将转换为 JSON 格式:

{
    "null-value": null
}
Enter fullscreen mode Exit fullscreen mode

日期和时间戳

可以使用 ISO 格式的日期,如下所示:

date: 2002-12-14
canonical: 2001-12-15T02:59:43.1Z
iso8601: 2001-12-14t21:59:43.10-05:00
spaced: 2001-12-14 21:59:43.10 -5
Enter fullscreen mode Exit fullscreen mode

序列

序列允许我们在 YAML 中定义列表:

# A list of numbers using hyphens:
numbers:
    - one
    - two
    - three

# The inline version:
numbers: [ one, two, three ]
Enter fullscreen mode Exit fullscreen mode

上述两个序列都将解析为 JSON:

{
    "numbers": [
        "one",
        "two",
        "three"
    ]
}
Enter fullscreen mode Exit fullscreen mode

嵌套值

我们可以使用上述所有类型来创建具有嵌套值的对象,如下所示:

# Nineteen eighty four novel data.
nineteen-eighty-four:
    author: George Orwell
    published-at: 1949-06-08
    page-count: 328
    description: |
        A Novel, often published as 1984, is a dystopian novel by English novelist George Orwell.
        It was published in June 1949 by Secker & Warburg as Orwell's ninth and final book.
Enter fullscreen mode Exit fullscreen mode

转换为 JSON 格式如下:

{
    "nineteen-eighty-four": {
        "author": "George Orwell",
        "published-at": "1949-06-08T00:00:00.000Z",
        "page-count": 328,
        "description": "A Novel, often published as 1984, is a dystopian novel by English novelist George Orwell.\nIt was published in June 1949 by Secker & Warburg as Orwell's ninth and final book.\n"
    }
}
Enter fullscreen mode Exit fullscreen mode

对象列表

将序列和嵌套值组合在一起,我们可以创建一个对象列表。

# Let's list books:
- nineteen-eighty-four:
    author: George Orwell
    published-at: 1949-06-08
    page-count: 328
    description: |
        A Novel, often published as 1984, is a dystopian novel by English novelist George Orwell.

- the-hobbit:
    author: J. R. R. Tolkien
    published-at: 1937-09-21
    page-count: 310
    description: | 
        The Hobbit, or There and Back Again is a children's fantasy novel by English author J. R. R. Tolkien.
Enter fullscreen mode Exit fullscreen mode

特色

以下是一些引起我注意的更复杂的特性,也是 YAML 与 JSON 的区别。

评论

您可能已经在我之前的例子中注意到,YAML 允许以 开头的注释#

# This is a really useful comment.
Enter fullscreen mode Exit fullscreen mode

节点锚点的可重用性

节点锚点标记一个节点以供将来引用,这使我们能够重复使用节点。我们使用字符来标记节点&,并使用以下方式引用它*

在下面的例子中,我们将定义一个书籍列表并重用作者数据,因此我们只需要定义一次:

# The author data:
author: &gOrwell 
    name: George
    last-name: Orwell

# Some books:
books: 
    - 1984:
        author: *gOrwell 
    - animal-farm:
        author: *gOrwell
Enter fullscreen mode Exit fullscreen mode

上述代码解析为 JSON 后将如下所示:

{
    "author": {
        "name": "George",
        "last-name": "Orwell"
    },
    "books": [
        {
            "1984": {
                "author": {
                    "name": "George",
                    "last-name": "Orwell"
                }
            }
        },
        {
            "animal-farm": {
                "author": {
                    "name": "George",
                    "last-name": "Orwell"
                }
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

带有标签的显式数据类型

正如我们在前面的例子中看到的,YAML 会自动检测值的类型,但我们可以指定我们想要的类型
我们通过在值前面加上 来指定类型!!

以下是一些示例:

# The following value should be an int, no matter what:
should-be-int: !!int 3.2

# Parse any value to string:
should-be-string: !!str 30.25

# I need the next value to be boolean:
should-be-boolean: !!bool yes
Enter fullscreen mode Exit fullscreen mode

这将转换为 JSON 格式:

{
    "should-be-int": 3,
    "should-be-string": "30.25",
    "should-be-boolean": true
}
Enter fullscreen mode Exit fullscreen mode

结论

阅读和撰写有关 YAML 的文章并对其进行实验非常有趣。

我喜欢的:我特别喜欢阅读关于 YAML 在代码整洁性和可读性方面的目标,以及它是如何实现这些目标的。我终于可以好好学习语法了,感觉好多了😅。

我不喜欢的:我不喜欢需要一个解析器(这意味着安装一个新的依赖项)来将 YAML 与我使用的主要技术(node.js 和 .NET Core)一起使用。

不过,我现在会考虑 YAML,尤其是当我需要 JSON 无法涵盖的功能,比如可重用性、显式类型或注释时。我相信现在使用管道也会更容易。

此外,我强烈建议阅读YAML 1.2 规范文档(第 3 次审查)-简介,以了解有关 YAML 目标、起源和与其他语言的关系的更多信息。

你用 YAML 做什么?💬

您在使用 YAML 吗?它用于什么用途?您对它有什么看法?

文章来源:https://dev.to/paulasantamaria/introduction-to-yaml-125f
PREV
掌握 NPM 脚本简介内置脚本和别名执行多个脚本理解错误静默或大声运行脚本从文件引用脚本前后访问环境变量传递参数命名约定文档结论
NEXT
JSDoc 使用 JSDoc 记录你的 Javascript 代码 简介 安装 使用 关于 JSDoc 的其他精彩内容