Game Development Reference
In-Depth Information
20.2 YAML Ain't Markup Language
Human readability is one of YAML's design goals. It is achieved by minimal use
of structural characters. YAML's syntax is as minimally intrusive as possible,
and it reads like natural note-taking: hierarchical structure is achieved through
indentation, key-value pairs are associated with colons and lists are created using
simple dashes.
We will briefly describe the most common elements of its syntax. Further detail
can be found in the YAML 1.2 specification [Ben-Kiki et al. 09], freely available
online. YAML's base type, known as scalar, can represent any primitive data type:
strings, numbers, booleans, etc. These can be structured using two kinds of YAML
containers: sequences and mappings, which can later be translated into C++ STL
vectors and maps (Listing 20.1).
# mapping of scalars
pi: 3.1415
language: YAML
# sequence of scalars
- tutorial
- play
- options
# mapping of sequence of mappings of scalars
resolutions:
- width: 1024
height: 768
- width: 800
height: 600
Listing 20.1. Sample YAML syntax introducing hierarchical data structures.
These containers can be combined at will to describe the most complex data
structures, as we will see in the last section of this article. YAML mappings and
sequences can also be written in a more compact way, called flow style, to make
them fit on a single line; for example, to represent colors, vectors, or matrices
(Listing 20.2).
# mapping of sequence of scalars
gravity: [ 0, -9.81, 0 ]
# mapping of mapping of scalars
yellow: { r: 1.0, g: 1.0, b: 0.0, a: 1.0 }
Listing 20.2. Flow-style notation of YAML sequences and mappings.
Search WWH ::




Custom Search