YAML tags are powerful features that provide explicit type information and custom data handling in YAML documents. They allow users to define and interpret data beyond YAML's basic types.
Tags serve several crucial functions in YAML:
YAML tags use the following syntax:
!!tag_name value
The double exclamation marks (!!) indicate a global tag, while a single exclamation mark (!) denotes a local tag.
YAML provides several built-in tags for common data types:
!!str
: String!!int
: Integer!!float
: Floating-point number!!bool
: Boolean!!null
: Null value!!seq
: Sequence (list)!!map
: Mapping (dictionary)age: !!int "42"
pi: !!float "3.14159"
is_valid: !!bool "true"
names: !!seq
- Alice
- Bob
- Charlie
person: !!map
name: John Doe
age: 30
Users can define custom tags for specific data types or objects:
%TAG !obj! tag:example.com,2023:
---
myObject: !obj!CustomClass
property1: value1
property2: value2
YAML parsers use tag resolution to determine how to interpret tagged data. This process involves:
To deepen your understanding of YAML and its features, explore these related topics:
By mastering YAML tags, you'll enhance your ability to create precise and flexible YAML documents, improving data representation and interoperability in your projects.