BudiBadu Logo
Samplebadu

YAML by Example: Tags

1.2

Explicit typing. Force specific data types.

Code

# Explicit string
zip: !!str 12345

# Explicit float
pi: !!float 3.14

# Explicit set (unique values)
set: !!set
  ? item1
  ? item2
  ? item3

# Binary data (base64 encoded)
image: !!binary |
  R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
  OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
  +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
  AgjoEwnuNAOei+dd9RNHpwVQRZob1PGYFC6HyGBAA7

# Application specific tags
my_object: !MyClass { name: foo }

Explanation

Tags explicitly specify data types using double exclamation marks for standard types like !!str, !!int, or !!float. This overrides automatic type detection when you need a number stored as a string or want to force a specific format.

The !!binary tag lets you embed Base64 encoded binary data directly in YAML files. Application-specific tags with a single exclamation mark can trigger custom parsing behavior or object instantiation in your code.

Code Breakdown

2
!!str 12345 forces the number to be treated as a string.
13
!!binary indicates the following block is Base64 encoded data.