Opened 13 years ago
Closed 13 years ago
#3827 closed Bugs (fixed)
Embeded null written as \0 rather than \u0000 in property_tree::write_json
| Reported by: | Owned by: | Sebastian Redl | |
|---|---|---|---|
| Milestone: | Boost 1.42.0 | Component: | property_tree |
| Version: | Boost Development Trunk | Severity: | Problem |
| Keywords: | Cc: |
Description
Here's an example
#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<unsigned char, 8> 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 );
}
This produces the output
{
"binary": "\u0003\0WA\0M\uFFFFz"
}
but from the description of JSON strings at http://www.json.org/ and the more detailed version at http://www.ietf.org/rfc/rfc4627.txt 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. )
Change History (2)
comment:1 by , 13 years ago
comment:2 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
(In [59738]) 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.
Note:
See TracTickets
for help on using tickets.

The fix is pretty easy, heres a patch from svn diff
=================================================================== --- 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<Ch>::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');