Search code examples
c++boostboost-propertytree

Boost ptree array of numbers


I use the following code to create an array of the numbers.

After running the following code, I reveive the following results:

{
    "": "1.100000",
    "": "2.200000",
    "": "3.300000"
}

It is good except for my desired result has to be an array of numbers not string. Adding a number directly by boost::property_tree::ptree(x) gives me an error too. How can I produce my output json results?

{
    "": 1.100000,
    "": 2.200000,
    "": 3.300000
}

Code:

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

int main()
{
    boost::property_tree::ptree pt;
    std::vector<double> Vec={1.1,2.2,3.3};
    for(double x:Vec)
    {
        std::string x_string=std::to_string(x);
        pt.push_back(
            std::make_pair("", 
            boost::property_tree::ptree(x_string)) );

    }
    boost::property_tree::json_parser::write_json(std::cout, pt);
    std::cout<<std::endl;
    return 0;
}

Solution

  • PTree has no such features.

    Everything is text in the serialized formats. Even if the chosen backend format could support (limited) typed data.

    Documentation proof:

    enter image description here

    As I keep re-stating:

    Boost does not have an XML library.

    Boost does not have a JSON library.

    Boost has a Property Tree library. It deals with Property Trees. Not JSON, XML or whatever else.