Reputation: 66965
So lets take a look onto a littel bit modified example code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
void load(const std::string &file_path)
{
using boost::property_tree::ptree;
ptree pt;
std::ifstream script;
script.open(file_path.c_str());
read_xml(script, pt);
BOOST_FOREACH(boost::property_tree::ptree::value_type &v,
config.get_child("config.servecies"))
{
std::cout
<< "First data: " << v.first.data() << std::endl
<< "Second data: " << v.second.data() << std::endl;
}
}
I try it on with such xml:
<config>
<servecies>
<module>file</module>
<module>Admin</module>
<module>HR</module>
<notModule>MyNotModule</notModule>
</servecies>
</config>
it prints:
First data: module
Second data: file
First data: module
Second data: Admin
First data: module
Second data: HR
First data: notModule
Second data: MyNotModule
But when I try such json file (created from this xml via this web tool):
{
"config": {
"name": "myconfig",
"servecies": {
"module": [
"file",
"Admin",
"HR"
],
"notModule": "MyNotModule"
}
}
}
it prints:
First data: module
Second data:
First data: notModule
Second data: MyNotModule
How to make boost property_tree produce same results on JSON as it parses XML? How to find out if value_type is some sort of one dimentional array and iterate thru it?
Upvotes: 0
Views: 2823
Reputation: 473926
How to make boost property_tree produce same results on JSON as it parses XML?
Have Boost.PropertyTree output JSON. Then it will be able to input that JSON file.
Boost.PropertyTree is used for storing properties. It is a way to save properties in human-readable formats, and restore those properties later.
It is not a way to make a fast-and-dirty JSON/XML reader. It writes data in a specific format, and when it is given data to read, it expects that the data it is given is what it wrote. If you try to shove any old JSON down it, it will not necessarily return reasonable information. It writes valid JSON, but the structure of that JSON is specific to PropertyTree, and the reading code will expect that structure to exist.
Side note: I don't think that web tool is very good, since it added information to the JSON file that wasn't present in the original XML.
Upvotes: 4