Search code examples
c++yaml-cpp

Parsing a YAML file?


How can I parse the following YAML file using yaml-cpp?

scene:
  - camera:
      film:
        width: 800
        height: 600
        filename: "out.svg"
  - shape:
      name: "muh"

I tried:

#include <yaml-cpp/yaml.h>

int main() {
    YAML::Node root_node = YAML::LoadFile("Scenes/StanfordBunny.flatland.yaml");
    
    // throws an exception
    int value = root_node["scene"]["camera"]["film"]["width"].as<int>();
}

How can I get the value of the width attribute? How can I get the name of the shape attribute?


Solution

  • The "-" in front of camera means it is an array of objects. So my guess would be:

    root_node["scene"][0]["camera"]["film"]["width"].as<int>();