I need to add unnamed nodes to a boost::property_tree::ptree just like it's JSON parser does for arrays. However when I want to do it I get such assertion during runtime:
Assertion failed: !p.empty() && "Empty path not allowed for put_child.", file C:\Program Files\Boost\boost\include/boost/property_tree/detail/ptree_implementation.hpp, line 877
I do it like
tree.add_child(name, child);
where tree and child are both ptree-s and name char*.
How could I do it like the JSON parser for ptree-s does?
I don't think Boost.Property_tree has a good reason for disallowing empty paths in add_child
or put_child
. The way root detection is implemented in their internal path utilities requires non-empty paths.
You can get around this by not using their pathing utilities when adding array elements.
using boost::property_tree::ptree;
ptree pt;
pt.put_child( "path.to.array", ptree() );
auto& array = pt.get_child( "path.to.array" );
array.push_back( std::make_pair( "", ptree("foo") ) );
array.push_back( std::make_pair( "", ptree("bar") ) );
boost::property_tree::json_parser::write_json( std::cout, pt, false );
// {"path":{"to":{"array":["foo","bar"]}}}
I landed here trying to figure out a similar problem. It took me a while to solve it, so hopefully this post helps others.
For me, the key to solving the problem was remembering that a ptree is a collection of boost::property_tree::ptree::value_type. So the problem reduces to "how can I add the value_types from one ptree into another".
Ptree provides a few methods for value_type insertion:
iterator push_front(const value_type &);
iterator push_back(const value_type &);
iterator insert(iterator, const value_type &);
Ptree doesn't have a const_reference typedef, so we cannot make use of std::copy with back_inserter iterator. But we can use std::for_each with a bound function.
#include <algorithm>
#include <functional>
#include <boost/property_tree/ptree.hpp>
using namespace std;
using namespace boost::property_tree;
...
ptree child;
child.put("Value1", 1);
child.put("Value2", 2);
ptree parent;
std::for_each(child.begin(),
child.end(),
std::bind(&ptree::push_back, &parent, placeholders::_1));
Now if parent is output as XML it would contain:
<Value1>1</Value1>
<Value2>2</Value2>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With