space_voyager
space_voyager

Reputation: 2034

yaml-cpp create a new empty map?

How can I create a new YAML parameter that is an empty map (the use-case is to populate it in some later part of the code)? Minimal working example:

YAML::Node node  = YAML::LoadFile("config.yaml");
node["new_map"] = "{}"; // this creates a string, but I want a map
std::ofstream fout("config.yaml");
fout << node;

Upvotes: 0

Views: 1264

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117483

You can use the YAML::Node constructor that takes a NodeType as an argument to force it to be a Map:

#include <yaml-cpp/yaml.h>

#include <iostream>

int main() {
    YAML::Node node;
    node["new_map"] = YAML::Node(YAML::NodeType::Map);
    std::cout << node;
}

Output:

new_map:
  {}

Upvotes: 4

Related Questions