Opened 9 years ago
Last modified 4 years ago
#9721 reopened Bugs
ptree write_json does not conform to JSON standard
Reported by: | 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 , 9 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:3 by , 6 years ago
there should be some updates about this problem, or the boost json can only be used as reader not writer
comment:5 by , 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:8 by , 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 , 5 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
This looks like a valid problem and still seen in 1.64. PLs look into this issue.
comment:10 by , 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:14 by , 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 , 4 years ago
Version: | Boost 1.55.0 → Boost 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 , 4 years ago
This is huge shortcoming! This should at the very least be documented using a proper disclaimer in the JSON section...
ptree
internally only has strings.put
converts from the values you put in to strings, asget
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.