Hackless
Hackless

Reputation: 397

How to list all keys of a generic yaml by yaml-cpp

If a yaml document contains a mix of sequences and maps and scalars, and those collection types are themselves multi-level deep, is there a built-in function or an easy way to list all the keys, but not the final value at the leaf? Assuming the keys are strings.

Upvotes: 1

Views: 2684

Answers (1)

Jesse Beder
Jesse Beder

Reputation: 34054

You'll have to recurse on the nodes in your document, checking the type of each:

switch (node.Type()) {
  case Null: // ...
  case Scalar: // ...
  case Sequence:
    for (auto it = node.begin(); it != node.end(); ++it) {
      auto element = *it;
      // recurse on "element"
    }
    break;
  case Map:
    for (auto it = node.begin(); it != node.end(); ++it) {
      auto key = it->first;
      auto value = it->second;
      // recurse on "key" and "value"
      // if you're sure that "key" is a string, just grab it here
    }
    break;
  case Undefined: // ...
}

Upvotes: 2

Related Questions