yaml-cpp parsing map with sequence as key -


i getting started yaml-cpp, , having troubles decoding 1 part of file. file want decode looks this:

leds:     - [251, 252,253]         - [254, 250,220]      - [230, 231,230]  

this how have been trying implement far not working.

        namespace yaml {          template<>         struct convert<led_yaml>         {           static node encode(const led_yaml& led_data)           {             node node;             node.push_back(led_data.r);             node.push_back(led_data.g);             node.push_back(led_data.b);             return node;           }           static bool decode(const node& node, led_yaml& led_data)           {             led_data.r = node[0].as<int>();             led_data.g = node[1].as<int>();             led_data.b = node[2].as<int>();             return true;           }         };     }     void yaml_ledparser(std::string filename)     {       try       {           yaml::node led_set = yaml::loadfile(filename);            for(std::size_t = 0; i< led_set.size();++i)           {             if(led_set["leds"])             {                 std::cout<<"iteration\n";                 led_collec =  it->second.as<led_collection_yaml>();             }              if(led_set.issequence())             {               //dummy = led_set[i].as<<led_yaml>();               led_collection.push_back(led_set[i].as<led_yaml>());               //std::cout<<"got sequence!"<<"\n";               std::cout<<"r: "<<led_collection[i].r<<"\n";             }           }            //led_collec = led_set["leds"].as<led_collection>();       }       catch(yaml::parserexception& e)       {         std::cout << "failed load file: "<<e.what()<<"\n";         return;       }     } 

the problem facing don't know how parse list of sequences

the simplest thing do, if know that's structure of yaml, is:

std::vector<led_yaml> leds = led_set["leds"].as<std::vector<led_yaml>>(); 

if want iterate through sequence manually:

for (auto element : led_set["leds"]) {   led_yaml led = element.as<led_yaml>();   // led } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -