YAML is designed to be human-readable and maps directly to native data types like lists, dictionaries, and strings. Here is a brief summary of the essential syntax:
1. Basic Structures
Key-Value Pairs: Created with a colon and a space.
key: valueComments: Begin with a hash symbol.
# This is a commentDocument Separators: Use
---to start a new document and...to end one (optional).
2. Collections (Arrays and Objects)
Lists (Sequences): Use a dash followed by a space for each item.
YAML- Item 1 - Item 2Dictionaries (Mappings): Nested using indentation (usually 2 spaces).
YAMLparent: child1: value child2: value
3. Multiline Strings (Scalars)
Literal Block (
|): Preserves newlines and trailing whitespace.Folded Block (
>): Folds newlines into spaces (converts a block into a single paragraph).
4. Data Types
YAML automatically detects types, but you can explicitly cast them using !!.
Strings: Can be unquoted, single-quoted (literal), or double-quoted (supports escape sequences like
\n).Numbers: Integers (
12) and Floats (3.14).Booleans:
true,false,yes,no.Nulls:
nullor~.
5. Advanced Features
Anchors (
&) and Aliases (*): Used to duplicate or reference data to avoid repetition.YAMLdefault: &base adapter: postgres development: <<: *baseComplex Keys: Use
?for keys that are themselves objects or lists."<<" : is the merge operator, which indicates the section to be replaced with the alias indicated by "*" after "<<".
Quick Reference Table
| Feature | Syntax Example | Notes |
| Mapping | name: Gemini | Requires space after : |
| List | - apple | Requires space after - |
| Inline List | [1, 2, 3] | JSON-style syntax |
| Inline Map | {a: 1, b: 2} | JSON-style syntax |
| Literal String | ` | ` |
| Folded String | > | Newlines become spaces |
No comments:
Post a Comment