Boost C++ Libraries: Ticket #9721: ptree write_json does not conform to JSON standard https://svn.boost.org/trac10/ticket/9721 <p> According to the JSON standard documented at <a class="ext-link" href="http://www.json.org"><span class="icon">​</span>http://www.json.org</a>, only string values should have quotes around them. The Boost write_json method puts quotes around all values, not just strings. </p> <p> For example, the following program: </p> <p> #include &lt;cstdlib&gt; #include &lt;string&gt; #include &lt;iostream&gt; #include &lt;boost/property_tree/ptree.hpp&gt; #include &lt;boost/property_tree/json_parser.hpp&gt; </p> <p> using boost::property_tree::ptree; using boost::property_tree::read_json; using boost::property_tree::write_json; </p> <p> int main() { </p> <blockquote> <p> ptree pt; </p> </blockquote> <p> </p> <blockquote> <p> pt.put ("string", "string value"); pt.put ("integer", 1); pt.put ("float", 3.14159); </p> </blockquote> <blockquote> <p> std::ostringstream buf; write_json (buf, pt); </p> </blockquote> <blockquote> <p> std::cout &lt;&lt; buf.str(); return 0; </p> </blockquote> <p> } </p> <p> produces this output: </p> <p> { </p> <blockquote> <p> "string": "string value", "integer": "1", "float": "3.14159" </p> </blockquote> <p> } </p> <p> According to the JSON standard, the output should be: </p> <p> { </p> <blockquote> <p> "string": "string value", "integer": 1, "float": 3.14159 </p> </blockquote> <p> } </p> <p> (no quotes around the numeric values) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9721 Trac 1.4.3 Sebastian Redl Thu, 27 Feb 2014 15:02:09 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/9721#comment:1 https://svn.boost.org/trac10/ticket/9721#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> <code>ptree</code> internally only has strings. <code>put</code> converts from the values you put in to strings, as <code>get</code> converts back. However, the tree itself only contains strings, and therefore so does the output JSON. </p> <p> Making a <code>ptree</code> variant that preserves JSON types is a future plan, but far off. </p> Ticket anonymous Sun, 22 May 2016 19:29:50 GMT <link>https://svn.boost.org/trac10/ticket/9721#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:2</guid> <description> <p> Yet no update after years.. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 01 Jun 2016 12:18:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:3</guid> <description> <p> there should be some updates about this problem, or the boost json can only be used as reader not writer<br /> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 06 Mar 2017 19:11:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:4</guid> <description> <p> Still nothing??? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 23 Mar 2017 01:20:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:5</guid> <description> <p> A conformant JSON writer should be in Boost since long ago. This ticket has been resolved as invalid, I can't believe it! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 10 May 2017 23:40:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:6</guid> <description> <p> rapidjson seems to do the job. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 10 May 2017 23:40:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:6</guid> <description> <p> rapidjson seems to do the job. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 19 Oct 2017 20:05:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:7</guid> <description> <p> No way this is invalid! Proper JSON parsing is absolutely necessary. </p> </description> <category>Ticket</category> </item> <item> <author>rnistuk@…</author> <pubDate>Mon, 30 Oct 2017 22:25:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:8</guid> <description> <p> Here's my fix for numbers: </p> <pre class="wiki">#include &lt;string&gt; #include &lt;boost/regex.hpp&gt; std::string fix_json_numbers(const std::string &amp;json_str) { boost::regex re("\\\"([0-9]+\\.{0,1}[0-9]*)\\\""); return boost::regex_replace(json_str, re, "$1"); } </pre><p> And here's a unit test: </p> <pre class="wiki">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); } </pre><p> NOTE: I've modified the unit test a bit to call the fix_json_numbers function directly, I have not compiled the unit test. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 16 Nov 2017 07:41:44 GMT</pubDate> <title>status changed; resolution deleted https://svn.boost.org/trac10/ticket/9721#comment:9 https://svn.boost.org/trac10/ticket/9721#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">invalid</span> </li> </ul> <p> This looks like a valid problem and still seen in 1.64. PLs look into this issue. </p> Ticket anonymous Thu, 16 Nov 2017 07:43:13 GMT <link>https://svn.boost.org/trac10/ticket/9721#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:10</guid> <description> <blockquote> <p> <em> Write value Str data = create_escapes(pt.template get_value&lt;Str&gt;()); stream &lt;&lt; Ch('"') &lt;&lt; data &lt;&lt; Ch('"'); </em></p> </blockquote> <p> This might be the code to be examined. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 29 Nov 2017 13:51:50 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:11</guid> <description> <p> Yep, i just wanted to use it for with JSON, issue still exist in 1.65 </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 29 Dec 2017 19:22:06 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:12</guid> <description> <p> What a shame! This ticket is 4 years old! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 09 Jan 2018 23:34:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:13</guid> <description> <p> Ok, rapidjson it is. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 01 Mar 2018 16:25:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9721#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:14</guid> <description> <p> yup i was doing research today to pick a json parser and i stumbled upon this thing... indeed rapidjson it is! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 22 May 2018 09:17:55 GMT</pubDate> <title>version changed https://svn.boost.org/trac10/ticket/9721#comment:15 https://svn.boost.org/trac10/ticket/9721#comment:15 <ul> <li><strong>version</strong> <span class="trac-field-old">Boost 1.55.0</span> → <span class="trac-field-new">Boost 1.67.0</span> </li> </ul> <p> This is still an issue for 1.67. Odd that for such a trivial requirement, boost has a long-pending issue. </p> Ticket anonymous Tue, 19 Jun 2018 15:34:54 GMT <link>https://svn.boost.org/trac10/ticket/9721#comment:16 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9721#comment:16</guid> <description> <p> This is huge shortcoming! This should at the very least be documented using a proper disclaimer in the JSON section... </p> </description> <category>Ticket</category> </item> </channel> </rss>