Question

I want to make hierarchical data with yaml, unfortunately, I'm not much used to this format, but I'd love to use it because it's human friendly.

Here is my yaml:

items:
    list1:
        itemA:
            item property a
        itemB:
    list2:
        itemC:
        itemD:

I'm using yaml-cpp and when I do doc["items"]["list1"]["itemA"], I end up with exception TypedKeyNotFound, and I don't think I understand well how yaml is supposed to be used, I do

doc["items"]["list1"]["itemA"].Type()

but I still have this exception.

Was it helpful?

Solution

Well I managed to better understand how yaml works, and also how it can be parsed. I don't want to get data like this a["fdfds"]["frwrew"]["vbxvxc"], because I don't want to require to know the key before parsing. I managed to make a code which shows the structure of a document using mostly maps and sequences, here it is.

int spaces = 0; // define it in global scope, since unroll is a recursive function.
void unroll(const YAML::Node & node)
{
switch(node.Type())
{
    case YAML::NodeType::Map:       
    {
        for(auto it = node.begin(); it != node.end(); ++ it)
        {
            string s; it.first() >> s;
            indent();
            cout << s << "\n";
            const YAML::Node & dada = it.second();
            spaces ++;
            unroll(dada);
            spaces--;
            cout << "\n";
        }
        break;
    }

    case YAML::NodeType::Scalar:
    {
        indent();
        string s; node >> s;
        cout << "found scalar " << s << "\n";
        break;
    }
    case YAML::NodeType::Null:
    {
        indent();
        cout << "null";
        break;
    }
    case YAML::NodeType::Sequence:
    {
        //cout << "sequence";
        for(auto it = node.begin(); it != node.end(); ++ it)
        {
            string s; *it >> s;
            indent();
            cout << s << "\n";
        }
        break;
    }
    default: cout << "error: undefined";    break;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top