5

I want to use yaml-cpp for a project of mine to generate a yaml-file however I have trouble figuring out how exactly I have to go about this. The yaml file I need to emit should look like this:

action_counts:
  version: 0.3
  subtree:
    - name: system
      local:
      - name: adder
        action_counts:
          - name: add
            counts: 1000
          - name: idle
            counts: 10000

So far I haven't been able to write the last four lines. I know that a '-' represents an array but I don't know how I can print the name and counts keys like that.

I have written some bits of code to experiment with yaml-cpp. The code looked like this:

    YAML::Node node;

    node["action_counts"] = YAML::Null;
    node["action_counts"]["version"] = "0.3";

    node["action_counts"]["subtree"].push_back("system");

    std::ofstream fout("fileUpdate.yaml"); 
    fout << node; 

    return 0;

And the output it produces is this:

action_counts:
  version: 0.3
  subtree:
    - system

The last line here is wrong but I haven't managed to find out how to print - name: system instead of this. How do I do this?

Once thats done how do I print the rest? Is local a part of the - name array? I think once I know how to format those to two lines I can figure out the rest by myself.

2 Answers 2

6

Lots of examples of creating messages can be found in the tests of yaml-cpp library.

The desired structure can be formed as follows:

#include <fstream>
#include "yaml-cpp/yaml.h"

int main() {
    YAML::Node action_1;
    action_1["name"] = "add";
    action_1["counts"] = 1000;

    YAML::Node action_2;
    action_2["name"] = "idle";
    action_2["counts"] = 10000;

    YAML::Node local_item;
    local_item["name"] = "adder";
    local_item["action_counts"].push_back(action_1);
    local_item["action_counts"].push_back(action_2);

    YAML::Node local;
    local.push_back(local_item);

    YAML::Node subtree_item;
    subtree_item["name"] = "system";
    subtree_item["local"] = local;

    YAML::Node root;
    root["action_counts"]["version"] = "0.3";
    root["action_counts"]["subtree"].push_back(subtree_item);

    std::ofstream fout("fileUpdate.yaml");
    fout << root;

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The value in the array is just another mapping with the keys name and local, so you need to put that mapping into the array, e.g.:

YAML::Node systemTree;

systemTree["name"] = "system";
/* assuming you have put together adderTree similarly */
systemTree["local"].push_back(adderTree);

YAML::Node root;

root["action_counts"] = YAML::Null;
root["action_counts"]["version"] = "0.3";

root["action_counts"]["subtree"].push_back(systemTree);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.