Boost C++ Libraries: Ticket #3827: Embeded null written as \0 rather than \u0000 in property_tree::write_json https://svn.boost.org/trac10/ticket/3827 <p> Here's an example </p> <pre class="wiki">#include "boost/property_tree/json_parser.hpp" #include "boost/array.hpp" int main() { boost::property_tree::ptree pt; // Some random data to encode boost::array&lt;unsigned char, 8&gt; data = { 3,0,'W','A',0,'M',255,'z' }; pt.put("binary", std::string(data.begin(), data.end() ) ); boost::property_tree::write_json(std::cout, pt ); } </pre><p> This produces the output </p> <pre class="wiki">{ "binary": "\u0003\0WA\0M\uFFFFz" } </pre><p> but from the description of JSON strings at <a class="ext-link" href="http://www.json.org/"><span class="icon">​</span>http://www.json.org/</a> and the more detailed version at <a class="ext-link" href="http://www.ietf.org/rfc/rfc4627.txt"><span class="icon">​</span>http://www.ietf.org/rfc/rfc4627.txt</a> it would appear that the correct encoding of the 0 character is \u00000 rather than \0 ( which is correctly handled for other non-prinatable characters. ) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3827 Trac 1.4.3 Michael Anderson <drmikeando@…> Fri, 08 Jan 2010 00:06:31 GMT <link>https://svn.boost.org/trac10/ticket/3827#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3827#comment:1</guid> <description> <p> The fix is pretty easy, heres a patch from svn diff </p> <pre class="wiki">=================================================================== --- boost/property_tree/detail/json_parser_write.hpp (revision 58794) +++ boost/property_tree/detail/json_parser_write.hpp (working copy) @@ -29,8 +29,7 @@ typename std::basic_string&lt;Ch&gt;::const_iterator e = s.end(); while (b != e) { - if (*b == Ch('\0')) result += Ch('\\'), result += Ch('0'); - else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b'); + if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b'); else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f'); else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n'); else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r'); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Sebastian Redl</dc:creator> <pubDate>Wed, 17 Feb 2010 18:43:57 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3827#comment:2 https://svn.boost.org/trac10/ticket/3827#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">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/59738" title="Turns out JSON doesn't allow \0 as an escape sequence. Also, don't ...">[59738]</a>) Turns out JSON doesn't allow \0 as an escape sequence. Also, don't rely on is_print for figuring out which characters to escape, but instead follow the spec. Fixes bug 3827. </p> Ticket