YAML – 语法字符
YAML - 语法字符
Section titled “YAML - 语法字符”YAML 使用一组指示字符来定义其结构和特性。理解这些字符是编写和阅读 YAML 文档的关键。本章概述了最常见的语法字符及其作用。
指示字符具有定义 YAML 文档结构和内容的特殊含义。下表总结了这些字符:
| 字符 | 功能 |
|---|---|
| - | 表示块序列项(列表项)。 |
| ? | 在块映射中表示复杂的映射键。 |
| : | 在映射中分隔键和值。 |
| , | 在流集合(流序列或流映射)中分隔条目。 |
| [ | 开始流序列(内联列表)。 |
| ] | 结束流序列。 |
| { | 开始流映射(内联字典/对象)。 |
| } | 结束流映射。 |
| # | 表示注释。从 ’#’ 到行尾的所有内容都会被忽略。 |
| & | 表示节点的锚点属性(用于创建可重用节点)。 |
| * | 表示对锚定节点的别名(用于重用节点)。 |
| ! | 表示节点的标签(用于指定数据类型)。可以是单个 !(特定于应用的)或双重 !!(YAML 核心类型)。 |
| | | 表示字面量块标量(保留换行符)。 |
| > | 表示折叠块标量(将换行符折叠成空格,保留段落分隔符)。 |
| ‘ | 单引号:环绕流标量,通过双写内部的单引号(”)进行转义。 |
| “ | 双引号:环绕流标量,允许 C 风格的转义序列(例如,\n, \t)。 |
| % | 表示指令,例如 %YAML 或 %TAG。 |
| --- | 文档分隔符,标记流中新 YAML 文档的开始。 |
| … | 文档结束标记,指示流中文档的结束。 |
以下示例演示了其中几个字符的用法:
%YAML 1.2---# This is a YAML document demonstrating various syntax charactersuser_profile: name: &user_name "Alice Wonderland" # Anchor for the name age: 30 is_active: true roles: # A block sequence (list) - admin - editor contact_info: {email: "alice@example.com", phone: "123-456-7890"} # A flow mapping bio: | Alice is a curious individual. Loves exploring new worlds. notes: > Prefers tea over coffee. Usually available in the mornings.
# Another top-level key referencing the anchored nameprimary_contact_name: *user_name # Alias to the name
# A list of tags (strings)preferred_tags: ["fantasy", "adventure", "classic"]
---# Start of a second document in the streamanother_item: Simple value...此示例展示了注释 (#)、指令 (%YAML)、文档分隔符 (---)、映射 (:)、块序列 (-)、流集合 ({}, [])、锚点 (&)、别名 (*) 以及块标量 (|, >)。