简体   繁体   中英

yams-cpp nested sequence returning map instead of value

I am trying to parse the following config.yaml file.

config.yaml

foo:
  bar:
    baz: [1, 2, 3, 4]
    bam: "some_string_value"

test.cpp

YAML::Node configObj = YAML::LoadFile("cfig.yaml"); // loads file just fine

YAML::Node fooObj = configObj["foo"]; // this Node object is a Map

// iterate over foo node to get bar node
for( auto it = fooObj.begin(); it != fooObj.end(); ++it){
    YAML::Node barMap = it->second; // this Node object is a Map

    // iterate over bar node to get bad node
    for( auto i = barMap.begin(); i != barMap.end(); ++i){
        YAML::Node bazMap = i->second; // this node is a sequence

        for( std::size_t i=0; i<bazMap.size(); i++         
            auto index = bazMap[i].as<int>(); // <<< This is the problem
        }
    }
}

The problem as far as I can see is that I am expecting index to be an int but bazMap[i].as<int>() I am expecting to be 1 the first loop, 2 the second, etc. What I am getting instead is bazMap[i].as<int>() is of type map. What am I missing in my understanding here?

Thanks,

Bruce

Update The answer was that I stopped early in my nested for loops.

The answer that I found rests in that I was unclear on what I was getting.

In the config.yaml file I was expecting the following:

foo.Type() == Map
bar.Type() == Sequence
baz.Type() == Sequence

but what I was getting was:

foo.Type() == Map
bar.Type() == Map
baz.Type() == Sequence

all I had to do to resolve this issue was to change the config file.

foo:
  bar:
    - baz: [1, 2, 3, 4]
    - bam: "some_string_value"

Then it parsed exactly as I had expected.

So the issue was the format of the config file and not in the parsing logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM