YAML CPP Introduction

YAML CPP Introduction


 YAML is a UNICODE based general-use data serialization language.

 Full form:  YAML Ain't Markup Language (yes it is!).

 It is commonly  used for storing configuration files, data persistence ,data auditing and online software           messaging.

 It is not a markup language and hence its code is more readable than others.

 It can store any type of data (even in binary form, if you so want).

 Basic Emitting in YAML 

The model for emitting YAML is std::ostream manipulators.

 A YAML::Emitter objects acts as an output stream, and its output is retrieved through the c_str() function (as in std::string). 
For example:

#include "yaml-cpp/yaml.h"
int main()
   YAML::Emitter out;
   out << "Hello, World!";
   std::cout << "Here's the output YAML:\n" << out.c_str(); // prints "Hello, World!"
   return 0;

YAML has three basic structures: 

Scalars, Sequences and Mappings.

 A document is formed by several nodes (or objects, if you prefer), that can be a scalar node, that holds information, or a map or sequence node, that hold other nodes. In a comparison to graphs, they can be branch or leaf nodes.

Scalars: as the name hints out, \these are simple values, sometimes with/without an individual identifying name. Can be string, numbers etc.
name: Harshit #this is a string scalar age: 21 #this is me saying "Hello World": > #This is a string scalar that spans over two lines. needs a haircut, badly.

Sequences: these are simply a list of nodes without any special identifications. They are easily accessible by index.
meals: ['1', '2', '3', '4'] #format Hi, this is a string sequence.

We can make squence of sequuence just like vector of vectors in STL.

Mappings: also referred to as hashes and dictionaries, this is a structure that allows you to relate identifiers and their informations in a more direct way. 

These identifiers (also referred to as keys) are usually a simple string name and the information in them can be of any type; as in they can be numbers, strings or even other maps and sequences. 
It is comparable with an std::map or, in some cases, with std::multimap.

The examples above, they are actually a map, where we give names to values. 

person: name: Harshit #maped value named "name". age: 21.#maped value named "age" description: > #maped value named ... World says hello to you too. needs a haircut, badly. 

meals: ['0930', '1230', '1600', '2130'] #sequence mapped to a name, "meals". 
days_skipped_gym: #this is a sequence of sequences mapped to a name (phew!) [Oct2020, 31] [Nov2020, 30] [Dec2020, 31] [Jan2021, 31] [Feb2021, 14, and_growing] #they do not need to be uniform! 

Of course, these can be used together to create more and more complex documents (like mapping of maps), which, in turn, can be used to store any kind of information.The language itself has some neat features, such as unique key identifiers (marked by a "? ") and variables marked by the '&' and '*' characters. ?.

A YAML::Emitter object acts as a state machine, and we use manipulators to move it between states. Here's a simple sequence:
Example of a simple grocery list in YAML:

YAML::Emitter out;
out << YAML::BeginSeq;
out << "biscuit";
out << "rice";
out << "milk";
out << YAML::EndSeq;

produces output::

- biscuit
- rice
- milk

Example of a simple map in YAML:

YAML::Emitter out;
out << YAML::BeginMap;
out << YAML::Key << "name";
out << YAML::Value << "Harshit Jain";
out << YAML::Key << "Branch";
out << YAML::Value << "CSE";
out << YAML::EndMap;

produces output::

name: Harshit Jain

We can nest(using BeginSeq and EndSeq) ,manipulate in other ways the outputs produced.


yaml-cpp uses CMake to support cross-platform building.

    The basic steps to build are:

  1. Download and install CMake (Resources -> Download).

    Note: If you don't use the provided installer for your platform, make sure that you add CMake's bin folder to your        path.

  1. Navigate into the source directory, and type:
mkdir build
cd build
  1. Run CMake. The basic syntax is:
cmake [-G generator] [-DYAML_BUILD_SHARED_LIBS=ON|OFF] ..
  • The generator is whatever type of build system you'd like to use. To see a full list of generators on your platform, just run cmake (with no arguments). For example:

    • On Windows, we might use "Visual Studio 2019 Win64" to generate a 64-bit Visual Studio 2019 solution.
    • On OS X, we might use "Xcode" to generate an Xcode project
    • On a UNIX-y system, simply omit the option to generate a makefile
  • yaml-cpp defaults to building a static library, but you may build a shared library by specifying -DYAML_BUILD_SHARED_LIBS=ON.

  • For more options on customizing the build, see the CMakeLists.txt file.

  1. Build it.

  2. To clean up, just remove the build directory.