Boost C++ Libraries: Ticket #9496: property_tree write_json should output correct types in JSON https://svn.boost.org/trac10/ticket/9496 <p> When using boost::property_tree::write_json it will write all types as strings, which is unwanted when wanting to preserve the correct types for data. </p> <ul><li>The boolean value is stored as the string "true" or "false", </li><li>Null is stored as the string "null" </li><li>Numeric values are also stored as strings </li><li>Floating point values might end up in their scientific, lossy representations </li></ul><p> For my purposes, I need to transfer numeric values (unix timestamps represented as double) in their most accurate representation using JSON and boolean values to represent correct boolean values without writing another text-to-bool converter. </p> <p> I will attach a patch that fixes this. For every value in the property_tree, it will </p> <ul><li>try to match is with boolean true or false, and write that value unquoted to the JSON output </li><li>try to read it as an integer and if that succeeds, write it unquoted to the JSON output </li><li>try to read it as a double value and if that succeeds, write it unquoted with appropriate numeric precision (based on std::numeric_limits) to the JSON output </li><li>try to see if the string matches 'null' and if it does, it will write null unquoted to the JSON output </li><li>if all else fails, write the value as a quoted string to the JSON output </li></ul><p> This will make it a lot easier to interpret the JSON on the receiving and, and greatly improve accuracy for double values. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9496 Trac 1.4.3 Egbert van der Wal <ewal@…> Sun, 15 Dec 2013 12:10:55 GMT attachment set https://svn.boost.org/trac10/ticket/9496 https://svn.boost.org/trac10/ticket/9496 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_property_tree_json.patch</span> </li> </ul> <p> Patch on SVN Trunk to improve the JSON output of the boost::property_tree::write_json function </p> Ticket anonymous Mon, 10 Feb 2014 13:28:34 GMT <link>https://svn.boost.org/trac10/ticket/9496#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9496#comment:1</guid> <description> <p> Any idea if/when this will be merged for a new release of Boost? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Sebastian Redl</dc:creator> <pubDate>Mon, 10 Feb 2014 15:01:49 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9496#comment:2 https://svn.boost.org/trac10/ticket/9496#comment:2 <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> I'm not going to include this. </p> <p> With or without this patch the behavior is not right: without it, everything becomes a string when reading, and is written as such. With it, everything still turns into a string on reading, but now some strings suddenly become not-strings when writing. </p> <p> In other words, this makes the writer harder to understand, and possibly incorrect - what if I actually want to write "2.0" to the JSON file? Maybe the JSON represents a music collection which contains the album by Garbage of that name. </p> <p> I do hope to eventually support a ptree that uses a variant value type to accurately represent JSON, but until then I will stick with pure strings as the simple and obvious thing. If that is insufficient for your needs, I'm afraid you'll have to use a real JSON library. </p> Ticket Egbert van der Wal <ewal@…> Mon, 10 Feb 2014 15:12:11 GMT <link>https://svn.boost.org/trac10/ticket/9496#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9496#comment:3</guid> <description> <p> While your response definitely shows a flaw in my patch, this does not invalidate the ticket, does it? It still remains an unwanted generalization of everything to string in using Boost's property_tree. </p> <p> Of course there are alternative libraries that will output JSON. My choice for using Boost property_tree is mainly based on my quest to minimize dependencies. Since my code already depends on Boost anyway, it's quite convenient to use as much from Boost as possible. </p> <p> A patch that actually remembers the type of data that was stored in the property_tree would have a chance to be accepted? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Sebastian Redl</dc:creator> <pubDate>Mon, 10 Feb 2014 16:09:16 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9496#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9496#comment:4</guid> <description> <p> Yes, it would be, now that I have time to work on PTree again. But I've only started going through the bug list, so keep <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/4786"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/4786</a> in mind before you start working on this; I'm not sure, but it might contain patches to that effect. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Sebastian Redl</dc:creator> <pubDate>Mon, 10 Feb 2014 16:15:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9496#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9496#comment:5</guid> <description> <p> Actually, if you do anything, please add it to <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/6351"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/6351</a>, which is exactly about that. </p> </description> <category>Ticket</category> </item> </channel> </rss>