Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost property write_json incorrect behaviour

I am coding a JSON wrapper for Boost property tree. Currently the focus is on writing the resulting JSON into a string or a file.

Using boost::property_tree::json_parser::write_json(ss, *pt) the resulting property tree is written in a string.

But this method do not understand what is a true, false, null or a number. Everything is converted to a string.

Reading the Boost documentation this is a limitation of the library. Is there any way to modify this behavior?

like image 262
mariolpantunes Avatar asked Dec 16 '22 17:12

mariolpantunes


1 Answers

Link In this link is a fix for the problem.

It involves changing boost code, because of that I tried another alternative. My solution involves regular expressions:

std::string JSONObject::toString() const
{
    boost::regex exp("\"(null|true|false|[0-9]+(\\.[0-9]+)?)\"");
    std::stringstream ss;
    boost::property_tree::json_parser::write_json(ss, *pt);
    std::string rv = boost::regex_replace(ss.str(), exp, "$1");

    return rv;
}

Basically I search for the keywords: true, false, null and any type of number. The matches are replaced with the same without quotes.

like image 191
mariolpantunes Avatar answered Jan 04 '23 07:01

mariolpantunes