Question

I am facing a problem with the new syntax. When parsing the following yaml file:

S1:
    data_type: spr
    guid: 1 
S2:
    data_type: spr
    guid: 2

using the following code:

#include "yaml.h"
int main () {
    YAML::Node testNode = YAML::LoadFile("data/Sprites.yaml");

    std::cout<<"type "<<testNode["S1"]["data_type"].as<std::string>()<<std::endl;
    std::cout<<"type "<<testNode[1]["data_type"].as<std::string>()<<std::endl;
    return 0;
}

The first line works and outputs "type spr", while the second line doesn't, throwing a YAML::TypedBadConversion < std::string >.

Shouldn't them both have the same output? Or does numbered indexes work only on sequences and not on maps? What am I doing wrong?

Was it helpful?

Solution

In YAML, maps are not ordered, so it doesn't make sense to ask for the "first entry" of a map. Instead, testNode[1] refers to the entry of the map with key 1, which doesn't exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top