0

I am using Boost's Property Tree library for storing my json files. For example, I have the following JSON file:

{
    "var" : true,
    "bar" : -1.56
}

Next I parse this file to ptree object, do my job and want to store output in MongoDB. For this I convert it back to JSON string:

boost::property_tree::ptree root;
boost::property_tree::read_json(file_path, root);
... // do my job
std::stringstream ss;
boost::property_tree::json_parser::write_json(ss, root);
std::string my_json_string = ss.str();

After this I push my results to MongoDB, by converting JSON string to BSON like this: bsonxx::from_json(my_json_string). As result I receive the following entity in database:

{
    "var" : "true",
    "bar" : "-1.56"
}

Is there a way to insert my JSON string to MongoDB with persistence types?

acm
  • 12,183
  • 5
  • 39
  • 68
Serbin
  • 803
  • 12
  • 27
  • What is the JSON in `my_json_string`? How did you get the JSON text for what is in the database? Without showing all the intermediate steps, there's no way to discover where the problem occurred. – xdg Aug 04 '17 at 14:35
  • This comes from this call: `std::string my_json_string = ss.str();`. It return a JSON string from Boost::PropertyTree. And it exactly what I need `"{"var":true;"bar":-1.56}"` – Serbin Aug 05 '17 at 19:08
  • The JSON example you just gave has a typo in it (semicolon is not a valid field separator). However, if I fix that and run it through bsoncxx::from_json, I get a valid BSON document and the "bar" field is type double. In order to diagnose, I need more information. What version of bsoncxx and libbson do you have installed? What platform/OS? Can you provide an [SSCCE](http://sscce.org) that demonstrates the problem that we can use for a reproduction? – xdg Aug 07 '17 at 01:28

0 Answers0