A markup language such as YAML is well suited to representing data in a structured and encoded form. Modeled on XML as the original form and on the widely used JSON notation, YAML provides an even simpler, text-based data format for describing objects and variables. Unlike XML, YAML does not have to maintain strict compatibility with SGML, which is a clear advantage and can benefit performance. From the start, the goal of YAML was to provide a form of data and object structures that is easy for both people and machines to read.

Another key advantage of YAML is its simple concept for serializing data. It allows data structures to be processed step by step and thus maps a program flow that builds on itself. To represent hierarchical structures correctly, YAML uses simple indentation, for example. Its light weight comes, among other things, from doing without quotation marks, brackets, and tags. To keep compatibility with JSON as high as possible, however, brackets can optionally be used in the markup.

Structure and Data Types in YAML

As in related markup languages such as XML or JSON, YAML has different data types for describing the information it contains correctly. To support a high level of compatibility with other markup or scripting languages, the type implementation covers a very broad range. YAML generally describes data structures with associative lists, array lists, and scalars for single values. Here is a simple example.

  • Simple list with values
    • Hamburg
    • Berlin
    • Munich
  • Associative list with cities and area codes
    Hamburg: 040
    Berlin: 030
    Munich: 089

After processing by a PHP parser, this would result in statements such as the following.

  • Simple list as an array
    <?php array(‘Hamburg‘, ‘Berlin‘, ‘Munich‘); ?>
  • Associative list as an array
    <?php array(‘Hamburg‘ => 040, ‘Berlin‘ => 030, ‘Munich‘ => 089); ?>

YAML provides the following basic data types.

  • Strings
    Strings can be used without quotation marks, with single quotation marks, or with double quotation marks. If control characters such as line breaks are needed, the strings must be enclosed in double quotation marks and marked with the corresponding control characters such as pipes.
  • Numbers
    All number types known from PHP can be used. Integers are written as normal sequences of digits such as 125, an octal value is written as 014, for example, and a hexadecimal value as 0xCF. A float value appears as 13.4, and an exponential value is written in YAML as 1.2e+34.
  • Null
    A typical null value is written with the identifier null or the tilde ~.
  • Boolean values are written in YAML with the identifiers true or false.
  • Binary values
    A special data type is the binary value, which is often used for security keys or the content of image files and can be embedded directly in YAML. The instruction !!binary | followed by the binary values serves this purpose.
  • Date values
    A date can be written either in the ISO 8601 standard as 2018-12-08t13:26:00 or as a SimpleDate as 2018-12-08.

Where YAML Is Used

In practice, YAML is used on the one hand to transfer data between computer systems of very different kinds. Complex and nested data constructs and structures can be represented well in the process. Far more often, however, YAML markup is used as a configuration file for server systems. Here the serialized processing of the file is a clear advantage.

A server can, for example, be configured in stages over several consecutive program runs. The basic structure of such a YAML configuration file (for example sqlsettings.yml) and a configuration set for a database server contained in it are shown below as an example.

# Kommentarzeile in YAML
# Serverkonfiguration SQL

mysql:
driver: pdo_mysql
dbname: database_name
server:
host: localhost
port: 8080
user: root
password: password
locale: de_DE.UTF-8
charset: utf8

# Security-Token base64-codiert

hash-code: !!binary |
R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=

# Ende Serverkonfiguration SQL

Conclusion

Because of its simple structure and lightweight syntax, YAML is preferred for data transfers in configuration files, for example in server setups. Its high compatibility with other markup languages such as XML or JSON makes the format a good fit for use cases that call for easy readability and good performance. Originally intended purely for data exchange between PC systems, it is now used more as a configuration tool for databases and servers.

Compared with XML and JSON, YAML simplifies the notation further while remaining highly compatible in terms of structures and data types. JSON code can also be included in a YAML file as valid content without further effort. Other important YAML features are the use of aliases and type conversion. In more complex structures, YAML code is generally easier to read than a JSON or XML notation.

Share LinkedIn X Email