Reputation: 852
I want to print the json array using below code snippet but it is not giving the desired result . Here is the output
{
"prefix": "standard",
"faceID": "42"
}
{
"prefix1": "standard2",
"faceID2": "44"
}
This is produced by below code snippet:
#include <boost/serialization/string.hpp>
#include <string>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree;
using boost::property_tree::write_json;
void create_array(ptree parent)
{
std::stringstream oss;
write_json(oss,parent);
std::string serialized_strings(oss.str());
std::cout << oss.str() << std::endl;
}
int main()
{
ptree pt,pt1;
pt.put("prefix","standard");
pt.put("faceID",42);
create_array(pt);
pt1.put("prefix1","standard2");
pt1.put("faceID2",44);
create_array(pt1);
}
expected output:
[
{
"prefix": "standard",
"faceID": "42"
},
{
"prefix1": "standard2",
"faceID2": "44"
}
]
Upvotes: 1
Views: 663
Reputation: 392931
Just to make it absolutely clear:
The documentation states that
The property tree dataset is not typed, and does not support arrays as such. Thus, the following JSON / property tree mapping is used [...]
It continues to describe that each ptree
represents a JSON object, always.
You need to remember that Boost Property Tree is not a JSON library. It is a Property Tree library, that optionally uses a subset of JSON for interoperability purposes. Therefore you cannot have arbitrary JSON things: You can not have a top-level arrays, you cannot have lone values, you cannot have actual numeric types, null, booleans etc.
You can only have Property Trees serialized using the described mappings.
Upvotes: 1