【问题标题】:Writing nested maps and sequences to a yaml file using YAML::Emitter使用 YAML::Emitter 将嵌套映射和序列写入 yaml 文件
【发布时间】:2018-02-07 09:58:25
【问题描述】:

我一直在尝试使用 YAML::Emitter 输出 yaml 文件。例如,我需要这样的东西作为我的 yaml 文件。

annotations:
  - run:
     type: range based
     attributes:
      start_frame:
       frame_number: 25
      end_frame:
       frame_number: 39     

到目前为止,使用我的代码

for (auto element : obj)
{
    basenode = YAML::LoadFile(filePath);  //loading a file throws exception when it is not a valid yaml file

    //Check if metadata is already there
    if (!basenode["file_reference"])
    {
        writeMetaData(element.getGttStream(), element.getTotalFrames(), element.getFileHash());
    }

    annotationNode["annotations"].push_back(element.getGestureName());

    annotationNode["type"] = "range based";
    output << annotationNode;

    attributesNode["attributes"]["start_frame"]["frame_number"] = element.getStartFrame();

    attributesNode["attributes"]["end_frame"]["frame_number"] = element.getEndFrame();

    output << typeNode;
    output << attributesNode;

    ofs.open(filePath, std::ios_base::app);
    ofs << std::endl << output.c_str();
}

我得到这样的输出

annotations:
  - run
type: range based
---
attributes:
 start_frame:
  frame_number: 26
 end_frame:
  frame_number: 57

我希望将最近推送的序列项下的“类型”和“属性”放入“注释”中,随后对所有以下节点都相同。

我什至尝试过使用类似的东西

annotationNode[0][type] = "基于范围"

输出是这样的

0:类型:“基于范围”

如何获取序列“注释”中最近推送的项目?

【问题讨论】:

  • 写完整的例子更有帮助;您的代码相当全面,但没有显示您的变量是什么类型(例如,annotationNode 何时定义?)

标签: c++ yaml-cpp


【解决方案1】:

如果您正在构建根节点annotationNode,那么只需构建它并输出一次。您不需要将typeNodeattributesNode 写入发射器。例如,你可以写

YAML::Node annotationNode;
for (auto element : obj) {
  YAML::Node annotations;
  annotations["name"] = element.getGestureName();
  annotations["type"] = ...;
  annotations["attributes"] = ...;
  annotationNode["annotations"] = annotations;
}

output << annotationNode;

【讨论】:

  • 感谢杰西的回答。我已经实现了它及其工作。也许我没有清楚地提出我的问题。我需要将“名称”作为序列项并将子项添加到其中,然后再添加子项。类似this
  • 也许可以澄清您的问题?如果您有一个可行的解决方案,请随时将其编写为解决方案,以便其他有您问题的人可以看到该解决方案。
  • 我关注了您的answer 作为参考。我会将您的评论标记为答案,但我仍然不知道如何实现here 所示的格式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多