c++yaml-cpp

yaml-cpp Easiest way to iterate through a map with undefined values


I'd like to obtain every node in a map without knowing the keys.

My YAML looks like this:

characterType :
 type1 :
  attribute1 : something
  attribute2 : something
 type2 :
  attribute1 : something
  attribute2 : something

I don't know how many "type"s will be declared or what the name of those keys will be. That's why I'm trying to iterate through the map.

struct CharacterType{
  std::string attribute1;
  std::string attribute2;
};

namespace YAML{
  template<>
  struct convert<CharacterType>{
    static bool decode(const Node& node, CharacterType& cType){ 
       cType.attribute1 = node["attribute1"].as<std::string>();
       cType.attribute2 = node["attribute2"].as<std::string>();
       return true;
    }
  };
}

---------------------
std::vector<CharacterType> cTypeList;

for(YAML::const_iterator it=node["characterType"].begin(); it != node["characterType"].end(); ++it){
   cTypeList.push_back(it->as<CharacterType>());
}

The previous code doesn't give any trouble when compiling but then at execution time I get this error: terminate called after throwing an instance of YAML::TypedBadConversion<CharacterType>

I've also tried using a subindex instead of the iterator, getting the same error.

I'm sure I'm doing something wrong, I just can't see it.


Solution

  • When you iterate through a map, the iterator points to a key/value pair of nodes, not a single node. For example:

    YAML::Node characterType = node["characterType"];
    for(YAML::const_iterator it=characterType.begin();it != characterType.end();++it) {
       std::string key = it->first.as<std::string>();       // <- key
       cTypeList.push_back(it->second.as<CharacterType>()); // <- value
    }
    

    (The reason that your code compiled, even though your node is a map node, is that YAML::Node is effectively dynamically typed, so its iterator has to act (statically) as both a sequence iterator and a map iterator.)