2

I am using yaml-cpp for my configuration files parsing because I have to be able to parse yaml and json files and this is the only cpp library supporting yaml 1.2 I have found. While parsing, I do not ask for key because I do not know what component is current configuration file for. Also I am using ncurses for displaying the configuration file content.

While parsing configuration files which include arrays I am not getting these arrays. Program just skips them.

Example of yaml configuration file:

SLICE_POSITION: 285
SLICE_HEIGHT: 15
OUTPUT_ADDRESS: "localhost:3335"
VIRTCAM_IDS:
            - 0
            - 1
            - 2

Example of json configuration file:

{
"width" : 1366,
"mappings" : {
  "x" : [ "mt_position_x" ],
  "y" : [ "mt_position_y" ]
},
"active_quadrangle" : {
  "bottom_left" : "[1472;4698;0]",
  "bottom_right" : "[5654;4698;0]",
  "top_left" : "[1472;1408;0]",
  "top_right" : "[5654;1408;0]"
},
"x" : 0.0,
"y" : 0.0
}

My code:

YAML::Node config = YAML::LoadFile(fileName);
for(YAML::const_iterator it = config.begin(); it != config.end();) {
    const char* key = (it->first.as<std::string>()).c_str();
    mvprintw(i, 4, key);
    i++; // row number
    ++it;
}

Keys I am getting from yaml file:

VIRTCAM_IDS                                   
SLICE_POSITION                                
SLICE_HEIGHT                                  
OUTPUT_ADDRESS   

Keys I am getting from json file:

uuid                                          
mappings                                      
width                                         
device                                        
sensor_type                                   
target                                        
height                                        
x                                             
active_quadrangle                             
y   

So can somebody tell me, how to parse this, so I can get to the arrays(and their values)? Also is there any way I can get items in right order?

Thanks for any answer!

1 Answer 1

1

When you're looping through the map, you're just reading the keys with it->first. To read the values too, you need it->second:

YAML::Node config = YAML::LoadFile(fileName);
for (YAML::const_iterator it = config.begin(); it != config.end(); ++it) {
  std::string key = it->first.as<std::string>();
  YAML::Node value = it->second;
  // here, you can check what type the value is (e.g., scalar, sequence, etc.)
  switch (value.Type()) {
    case YAML::NodeType::Scalar: // do stuff
    case YAML::NodeType::Sequence: // do stuff
    // etc.
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh thank you, I tried this, but I called .Type() on iterator, so it threw exception on me. It haven't occurred to me to try it on iterator value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.