Opened 9 years ago

Last modified 4 years ago

#9721 reopened Bugs

ptree write_json does not conform to JSON standard

Reported by: Mark Pfeifer <Mark_Pfeifer@…> Owned by: Sebastian Redl
Milestone: To Be Determined Component: property_tree
Version: Boost 1.67.0 Severity: Problem
Keywords: Cc:

Description

According to the JSON standard documented at http://www.json.org, only string values should have quotes around them. The Boost write_json method puts quotes around all values, not just strings.

For example, the following program:

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

using boost::property_tree::ptree; using boost::property_tree::read_json; using boost::property_tree::write_json;

int main() {

ptree pt;

pt.put ("string", "string value"); pt.put ("integer", 1); pt.put ("float", 3.14159);

std::ostringstream buf; write_json (buf, pt);

std::cout << buf.str(); return 0;

}

produces this output:

{

"string": "string value", "integer": "1", "float": "3.14159"

}

According to the JSON standard, the output should be:

{

"string": "string value", "integer": 1, "float": 3.14159

}

(no quotes around the numeric values)

Change History (17)

comment:1 by Sebastian Redl, 9 years ago

Resolution: invalid
Status: newclosed

ptree internally only has strings. put converts from the values you put in to strings, as get converts back. However, the tree itself only contains strings, and therefore so does the output JSON.

Making a ptree variant that preserves JSON types is a future plan, but far off.

comment:2 by anonymous, 6 years ago

Yet no update after years..

comment:3 by anonymous, 6 years ago

there should be some updates about this problem, or the boost json can only be used as reader not writer

comment:4 by anonymous, 6 years ago

Still nothing???

comment:5 by anonymous, 6 years ago

A conformant JSON writer should be in Boost since long ago. This ticket has been resolved as invalid, I can't believe it!

comment:6 by anonymous, 5 years ago

rapidjson seems to do the job.

comment:6 by anonymous, 5 years ago

rapidjson seems to do the job.

comment:7 by anonymous, 5 years ago

No way this is invalid! Proper JSON parsing is absolutely necessary.

comment:8 by rnistuk@…, 5 years ago

Here's my fix for numbers:

#include <string>
#include <boost/regex.hpp>

  std::string fix_json_numbers(const std::string &json_str)
    {
        boost::regex re("\\\"([0-9]+\\.{0,1}[0-9]*)\\\"");
        return  boost::regex_replace(json_str, re, "$1");
    }

And here's a unit test:

BOOST_AUTO_TEST_CASE(test_fix_json_str)
    {
        std::string json_str_in ="{\"cmd\":\"test\",\"data\":{\"integer\":\"283\",\"float\":\"34.433\"}";
        std::string json_str_accepted ="{\"cmd\":\"test\",\"data\":{\"integer\":283,\"float\":34.433}";
        std::string json_str_actual = fix_json_numbers(json_str_in);
        BOOST_CHECK_EQUAL(json_str_accepted, json_str_actual);
    }

NOTE: I've modified the unit test a bit to call the fix_json_numbers function directly, I have not compiled the unit test.

comment:9 by anonymous, 5 years ago

Resolution: invalid
Status: closedreopened

This looks like a valid problem and still seen in 1.64. PLs look into this issue.

comment:10 by anonymous, 5 years ago

Write value Str data = create_escapes(pt.template get_value<Str>()); stream << Ch('"') << data << Ch('"');

This might be the code to be examined.

comment:11 by anonymous, 5 years ago

Yep, i just wanted to use it for with JSON, issue still exist in 1.65

comment:12 by anonymous, 5 years ago

What a shame! This ticket is 4 years old!

comment:13 by anonymous, 5 years ago

Ok, rapidjson it is.

comment:14 by anonymous, 5 years ago

yup i was doing research today to pick a json parser and i stumbled upon this thing... indeed rapidjson it is!

comment:15 by anonymous, 4 years ago

Version: Boost 1.55.0Boost 1.67.0

This is still an issue for 1.67. Odd that for such a trivial requirement, boost has a long-pending issue.

comment:16 by anonymous, 4 years ago

This is huge shortcoming! This should at the very least be documented using a proper disclaimer in the JSON section...

Note: See TracTickets for help on using tickets.