2

I am completely aware of the duplicate keys problem with yaml spec:

The content of a mapping node is an unordered set of key: value node pairs, with the restriction that each of the keys is unique.

For instance, I have emitted a yaml file using yaml-cpp library with a nested tree structure which looks like this:

work:
  - run:
      type: workout
      figures:
        start_time:
           start: 9
        end_time:
           end: 12
  - run:
     type: workout
     figures:
        start_time:
           start: 16
        end_time:
           end: 18

Evidently, it has duplicate keys and yaml-validator treats it as a normal file. But when i try to parse it using yaml-cpp

void parser(const std::string& path)
{
    const YAML::Node& baseNode = YAML::Loadfile(path);

    for (const auto& item : baseNode["work"]);
    {
        switch (item.second.Type()) //Error not a valid yaml node
        {
            //If it is a NullNode
            case YAML::NodeType::Null:
            break;

            //If it is a ScalarNode
            case YAML::NodeType::Scalar:

                scalarNodeIterator(item.second, item.first.as<std::string>());
                break;

            //If it is a SequenceNode
            case YAML::NodeType::Sequence:

                sequenceNodeIterator(item.second, item.first.as<std::string>());
                break;

            //If it is a MapNode
            case YAML::NodeType::Map:

                mapNodeIterator(item.second, item.first.as<std::string>());
                break;
        }
    }
}

What do i want:

As you can see i have my own node dependent iterators to read through any kind of yaml tree. I need to use duplicate keys and when i do, the parser should work as intended.

Is it possible to implement a parser which can read through a node like run? If yes, how can i achieve it? If not, any other solutions to emit a yaml file with readable duplicate keys?

1
  • Actually your example does not have duplicate keys. Apart from that, duplicate keys should be checked above the parser level, as the parser cannot guess in every case if two keys are equal. Commented Apr 20, 2018 at 20:51

1 Answer 1

2

Your example does not have duplicate keys, as @tinita pointed out in a comment.

Instead, under the "work" key, you have a list of maps, each of which have the key "run" and the value a map.

That's why you're getting the error. When you iterate

for (const auto& item : baseNode["work"])

item will represent a node (since you're iterating through a sequence), not a key/value pair. You then would have to iterate through its (one) element to read the key/value pairs.

Sign up to request clarification or add additional context in comments.

Comments

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.