是否可以在yaml-cpp中设置发射器缩进的基本级别?

问题描述

是否有一种一次性方法来设置Emitter在yaml-cpp中发出的所有值的缩进基本级别?

我尝试使用Emitter::SetIndentEmitter::SetLocalIndent,但它们似乎只影响发射源根目录以下的项目,而不影响根目录项目本身

为说明起见,请考虑以下 所需 YAML输出。值已开始缩进4个空格(我右边的注释不属于输出内容)

    type: Circle            // Correctly indented by 1 level (4 spaces)
    r: 492.763875219
    x: 1286.75555556
    y: 1195.04
    regions:
      - id: 1750272850      // Correctly indented by 2 levels (8 spaces)
        width: 200
      - id: 524770566
        width: 42

我试图用下面的代码写出来:

void Shape::writeToYaml(std::ostream& os)
{
    YAML::Emitter out;      
    out.SetIndent(4);         // THIS DOES NOT HELP
    out << YAML::BeginMap;

    for (auto&& prop : properties())   // 'properties()' returns std::map<string,string>
        out << YAML::Key << prop.first << YAML::Value << prop.second;
    out << YAML::Key << PK_REGIONS << YAML::Value;
    out << YAML::BeginSeq; 
    for (auto&& region : m_regions)
        out << region.properties();  // 'properties()' returns std::map<string,string>

    out << YAML::EndSeq;
    out << YAML::EndMap;

    os << out.c_str();
}

相反,我得到的输出中根本没有缩进根值,而在根值以下缩进了太多

type:   Circle                         // WRONG: NOT INDENTED AT ALL
r:      492.763875219
x:      1286.75555556
y:      1195.04
regions:
        -       id:     2077164443     // WRONG: INDENTED BY TOO MUCH
                width:  200
        -       id:     2031385931
                width:  42

((我试图在不暴露API中的yaml-cpp类型的情况下改编现有的yaml编写代码,因此我需要能够动态创建Emitter,然后仅设置其基本缩进。我正在使用最新的0.6版本,昨天已下载)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)