Example of writing a YAML document

#include <x/yaml/writer.H>
#include <x/yaml/newdocumentnode.H>
#include <x/yaml/newsequencenode.H>
#include <x/yaml/newmappingnode.H>
#include <x/yaml/newscalarnode.H>

#include <cstdlib>

// A list of continents, the highest point in each continent, and which country
// it's in.

struct highest_point {
	const char *continent;
	const char *name;
	const char *country;
};

const highest_point continent_list[]={
	{"Africa", "Mount Kilimanjaro","Tanzania"},
	{"Antarctica", "Vinson Massif","Antarctica"},
	{"Asia", "Mount Everest","China/Nepal"},
	{"Australia", "Puncak Jaya","Papua, Indonesia"},
	{"Europe", "Mount Elbrus","Russia"},
	{"North America", "Mount McKinley","United States"},
	{"South America", "Aconcagua","Argentina"},
};

// Write a mapping from a highest_point object

// Constructs an x::yaml::newnode that represents a mapping created from a
// highest_point object.

x::yaml::newnode highest_point_to_yaml(const highest_point &hp)
{
	// x::yaml::newmapping::create constructs an x::yaml::newnode that
	// invokes a functor that creates a mapping, when writing the
	// document. Note that hp is captured by value, so that the
	// highest_point argument, that's passed by reference, is safely
	// tucked away in the lambda.

	return x::yaml::newmapping::create
		([hp]
		 (x::yaml::newmapping &info)
		 {
			 std::list<std::pair<x::yaml::newnode,
					     x::yaml::newnode> > list;

			 // Fill the container with the mapping.

			 list.push_back(std::make_pair
					(x::yaml::newscalarnode::create
					 ("continent"),
					 x::yaml::newscalarnode::create
					 (hp.continent)));
			 list.push_back(std::make_pair
					(x::yaml::newscalarnode::create
					 ("highest_point"),
					 x::yaml::newscalarnode::create
					 (hp.name)));
			 list.push_back(std::make_pair
					(x::yaml::newscalarnode::create
					 ("country"),
					 x::yaml::newscalarnode::create
					 (hp.country)));

			 info.style=YAML_FLOW_MAPPING_STYLE;
			 info(list.begin(), list.end());
		 });
}

x::yaml::newnode list_of_highest_points_to_yaml()
{
	// x::yaml::newsequence<>::create constructs an x::yaml::newnode that
	// invokes the given functor to create a sequence, when writing the
	// document.

	return x::yaml::newsequence::create
		([]
		 (x::yaml::newsequence &info)
		 {
			 // Iterate over the array. Use highest_point_to_yaml()
			 // to construct a node for each element in the array.

			 std::list<x::yaml::newnode> list;

			 for (const highest_point &hp:continent_list)
			 {
				 list.push_back(highest_point_to_yaml(hp));
			 }
			 info(list.begin(), list.end());
		 });
}

void writeyaml()
{
	x::yaml::newdocumentnode doc=x::yaml::make_newdocumentnode
		([]
		 {
			 return list_of_highest_points_to_yaml();
		 });

	x::yaml::writer
		::write_one_document(doc,
				     std::ostreambuf_iterator<char>(std::cout));
}

int main(int argc, char **argv)
{
	try {
		writeyaml();
	} catch (const x::exception &e)
	{
		std::cerr << e << std::endl;
		exit(1);
	}
	return 0;
}

writeyaml() constructs a single x::yaml::newdocumentnode node, and write_one_document() invokes the node's lambda to return the document's top level x::yaml::newnode, and writes it to std::cout.

list_of_highest_points_to_yaml() returns a sequence node as the top level x::yaml::newnode. Its lambda iterates over the continent_list array, and calls highest_point_to_yaml() to instantiate a x::yaml::newnode for the corresponding array element, and adds it to the sequence's container.

highest_point_to_yaml() constructs a a mapping node with a lambda that captures its highest_point by value, safe and sound. When called to write the mapping, the contents of the mapping are constructed, as scalar values.

This example produces the following output:

$ g++ -std=c++20 -fno-omit-frame-pointer -pthread -o writer writer.C -lcxxyaml -lcxx
$ ./writer
- {continent: Africa, highest_point: Mount Kilimanjaro, country: Tanzania}
- {continent: Antarctica, highest_point: Vinson Massif, country: Antarctica}
- {continent: Asia, highest_point: Mount Everest, country: China/Nepal}
- {continent: Australia, highest_point: Puncak Jaya, country: 'Papua, Indonesia'}
- {continent: Europe, highest_point: Mount Elbrus, country: Russia}
- {continent: North America, highest_point: Mount McKinley, country: United States}
- {continent: South America, highest_point: Aconcagua, country: Argentina}