The way of yaml parsing? (yaml-cpp)

1.3k views Asked by At

I've made a yaml file like below.

Define1: &Define1
  0: zero
  1: one

Define2:
  <<: *Define1
  2: two

And tried in Online YAML parser. The result is like below. ( Just get how nodes are constructed. )

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "0": "zero", 
    "1": "one", 
    "2": "two"
  }
}

Of course I expected 'yaml-cpp' would parse same way but it's somehow different.

I guess it's like this. (Almost sure)

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "Define1": {
      "0": "zero", 
      "1": "one"
    },  
    "2": "two"
  }
}

What the hell! Then do I have to check node type while looping?

Is this a known issue? or 'yaml-cpp' just parse that way?

This code is how I did.

// already parsed
const YAML::Node& node = &(docYAML)["Define2"];

for (YAML::Iterator it=node->begin(); it!=node->end(); ++it)
{
    const YAML::Node& nodeList = it.second();

    std::string str;
    nodeList[0] >> str;
}
1

There are 1 answers

1
Jesse Beder On BEST ANSWER

yaml-cpp doesn't implement the "merge" key yet. If you want to follow the issue until it's implemented, see http://code.google.com/p/yaml-cpp/issues/detail?id=41.

For now, yaml-cpp is actually parsing your YAML file as:

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "<<": {
      "0": "zero", 
      "1": "one"
    },  
    "2": "two"
  }
}