Emit YAML迭代通过boost属性树(递归)

时间:2018-04-19 13:16:03

标签: c++ boost boost-propertytree yaml-cpp

我有一个小而复杂的树结构。使用,boost属性树作为容器我试图遍历树,然后使用yaml-cpp库将其发送到yaml文件。

例如,我有一个小的嵌套属性树:

fibonacci:
  type: series
  entities:
    golden_ratio:
      ratio: 2.3
    function:
      power_series: 2

我希望我的yaml文件看起来完全像这样。

我编写了一个递归函数来遍历树并发射到yaml。

//Member variable
YAML::Emitter m_out

void iterator(const boost::property_tree::ptree& tree, const std::string& key)
{
    for (const auto& item: tree)
    {
        if (item.second.data().empty()) //check if map node
        {
            m_out << YAML::BeginMap;
            m_out << YAML::Key << item.first;
        }
        else if (!item.second.data().empty()) //else it is key/value pair
        {
            m_out << YAML::Key << item.first;
            m_out << YAML::Value << item.second.data();
        }
        if (!item.second.empty()) //If the node has child
        {
            iterator(item.second, item.first);
        }
    }
}

我使用emtpy键调用该函数为iterator(root, "")。我知道属性树作为键/值对工作,而Yaml-cpp具有节点指定。在代码中,我只是假设基于值的树节点类型(没有值 - 映射节点,否则 - 键/值节点)

显然,由于我的逻辑错误,我发出的yaml文件不具备上面所示的所需树结构。我想创建一个递归函数,它可以迭代任何类型的树并将其发送到yaml文件。 是否有可能迭代树并随后递归地发送到yaml?如果是的话,我会很感激一些想法。

1 个答案:

答案 0 :(得分:1)

所以我把你想要的YAML并通过一个online converter来获得一个“可靠的”ptree表示(你有趣地忽略了这个问题)。

然后我继续进行简单的ptree往返进行健全性检查:

<强> Live On Coliru

#include <boost/property_tree/json_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;

std::istringstream sample_json();
ptree sample_ptree();

int main() {
    write_json(std::cout, sample_ptree());
}

std::istringstream sample_json() {
    return std::istringstream(R"({
        "fibonacci": {
            "type": "series",
            "entities": {
                "golden_ratio": {
                    "ratio": 2.3
                },
                "function": {
                    "power_series": 2
                }
            }
        }
    })");
}

ptree sample_ptree() {
    ptree pt;
    {
        auto stream = sample_json();
        read_json(stream, pt);
    }
    return pt;
}

打印

{
    "fibonacci": {
        "type": "series",
        "entities": {
            "golden_ratio": {
                "ratio": "2.3"
            },
            "function": {
                "power_series": "2"
            }
        }
    }
}

to_yaml取#1

最简单的当然是阅读相同的JSON,让yaml-cpp进行转换:

auto stream = sample_json();
std::cout << YAML::Load(stream) << "\n";

打印:

{fibonacci: {type: series, entities: {golden_ratio: {ratio: 2.3}, function: {power_series: 2}}}}

to_yaml#2:漂亮的打印

首先关闭

  • 命名很重要。 iterator未描述该功能,并且与well-known concept from the standard library
  • 发生冲突
  • key参数未使用
  • 您只有BeginMap,如果代码中没有EndMap,您对期望的树有何期待?
  • 请没有全局变量。它们使您的代码变得脆弱(非确定性,非幂等,非重入,非线程安全等)。只需将Emitter&作为参数传递。

我会更简单:

void to_yaml(ptree const& node, YAML::Emitter &m_out) {
    if (node.empty()) {
        m_out << YAML::Value << node.data(); 
    } else {
        m_out << YAML::BeginMap;
        for (auto const&item : node) {
            m_out << YAML::Key << item.first;
            to_yaml(item.second, m_out);
        }
        m_out << YAML::EndMap;
    }
}

现在,为了有一个方便的入口点,添加一个重载:

std::string to_yaml(ptree const& tree) {
    YAML::Emitter out;
    to_yaml(tree, out);
    return out.c_str();
}

现在您可以通过执行以下操作打印结果:

std::cout << to_yaml(sample_ptree()) << "\n";

打印:

fibonacci:
  type: series
  entities:
    golden_ratio:
      ratio: 2.3
    function:
      power_series: 2

完整列表

#include <iostream>

#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree;

std::istringstream sample_json();
ptree sample_ptree();

#include "yaml-cpp/yaml.h"

void to_yaml(ptree const& node, YAML::Emitter &m_out) {
    if (node.empty()) {
        m_out << YAML::Value << node.data(); 
    } else {
        m_out << YAML::BeginMap;
        for (auto const&item : node) {
            m_out << YAML::Key << item.first;
            to_yaml(item.second, m_out);
        }
        m_out << YAML::EndMap;
    }
}

std::string to_yaml(ptree const& tree) {
    YAML::Emitter out;
    to_yaml(tree, out);
    return out.c_str();
}

int main() {
    write_json(std::cout, sample_ptree());

    {
        auto stream = sample_json();
        std::cout << YAML::Load(stream) << "\n";
    }

    std::cout << to_yaml(sample_ptree()) << "\n";
}

std::istringstream sample_json() {
    return std::istringstream(R"({
        "fibonacci": {
            "type": "series",
            "entities": {
                "golden_ratio": {
                    "ratio": 2.3
                },
                "function": {
                    "power_series": 2
                }
            }
        }
    })");
}

ptree sample_ptree() {
    ptree pt;
    {
        auto stream = sample_json();
        read_json(stream, pt);
    }
    return pt;
}
相关问题