通过提升属性树(递归方式)发出 YAML 迭代
Emit YAML iterating through boost property tree (recursively)
我有一个小而复杂的树结构。使用,提升属性树作为容器,我正在尝试遍历树,随后使用 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?如果是,我将不胜感激一些想法。
所以我拿了你想要的 YAML 并通过在线转换器来获得"可靠"的 ptree 表示(有趣的是,你把它排除在问题之外)。
然后我继续做一个简单的 ptree 往返以进行健全性检查:
住在科里鲁
#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
没有描述函数,并且与标准库中众所周知的概念相冲突。 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;
}
相关文章:
- 如何在c++中为模板函数实例创建快捷方式
- QAbstractTableModel,并为单行发出dataChanged
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 在c代码之间共享数据的最佳方式
- 在C++中将函数压缩为两种方式
- 以螺旋方式打印矩阵的程序.(工作不好)
- g++ 在某个类成员未初始化时不发出警告
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- MacOS通过在莫哈韦"wchar.h"下破碎的自制啤酒发出叮当声
- 创建引用向量的优雅方式
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 不同/较旧的处理器运行c++代码的方式是否不同
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 重载方法的方式会在使用临时调用时生成编译器错误
- 在reactor中存储eventHandlers的最佳方式是什么
- 通过提升属性树(递归方式)发出 YAML 迭代
- 发出信号并保持pthread打开的有效方式
- 从另一个线程发出信号的最快方式