Understanding YAML Syntax
YAML Ain’t Markup Language (YAML) is a straightforward, human-readable data serialization standard that can be used in conjunction with all programming languages and is often used to write configuration files. Despite its simplicity, YAML offers a rich feature set that aligns well with JSON and XML. This cheat sheet aims to provide a comprehensive guide to YAML syntax and its usage through various examples. Starting with the basics and progressively covering more advanced topics.
YAML Basics
YAML files often begin with ‘—‘, indicating the start of a document. This header is not always required but can be useful in delineating between documents in a multi-document file.
---
# This is a YAML comment
key: value
As seen above, key-value pairs are defined with a colon followed by a space. Comments are preceded by the ‘#’ character.
Lists and Dictionaries
In YAML, lists and dictionaries are the bread and butter of data structure representations.
- List item 1
- List item 2
- List item 3
dictionary:
key1: value1
key2: value2
Dictionaries (also known as maps or hashes) are collections of key-value pairs, with each pair separated by new lines and beginning with a new indentation level. Lists are sequences of items, prefaced by ‘- ‘.
Complex Structures
YAML also supports the nesting of dictionaries and lists to create more complex structures.
programming_languages:
dynamic:
- Python
- Ruby
statically_typed:
- C
- Java
- name: John Doe
age: 30
skills:
- Programming
- Writing
We can see above that the combinations of lists and dictionaries can represent intricate configurations and arrangements of data.
Advanced Data Types
YAML also includes several advanced data types such as boolean, null, numbers (both floats and integers), and strings.
isProgrammer: true
hasCoffee: false
itemsCount: 73
# Special null value
gone: ~ # same as 'gone: null'
# Numbers¬
integer: 25
float: 3.14159
# Strings
singleQuoted: 'Contains special characters: ":{}'
doubleQuoted: "Supports escape characters \n \t"
literalStyle: |
This will be a
multi-line string
foldedStyle: >
This too will be a
multi-line string
but it will be
folded into a single
line when used
Strings can be enclosed in single or double quotes, with the latter supporting escape sequences. Literal and folded block scalars use ‘|’ and ‘>’, respectively, for better representation of multi-line strings.
References and Merging
YAML allows the reuse of previously defined nodes with an alias and anchor, keeping the document DRY (Don’t Repeat Yourself).
defaults: &defaults
adapter: postgres
host: localhost
development:
<<: *defaults
database: dev_db
Here, ‘defaults’ is an anchor, which is then bolted to the ‘development’ dictionary. It propagates all values from the anchor to the referencing node.
Conclusion
YAML syntax is vast yet intuitive, forming a pivotal element in modern development architectures. It integrates seamlessly with various technologies, offering an efficient means to maintain structured data for configuration. Understanding its syntax and features brings clarity and productivity to your development workflows.