Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59738 - in trunk: boost/property_tree/detail libs/property_tree/test
From: sebastian.redl_at_[hidden]
Date: 2010-02-17 13:43:56


Author: cornedbee
Date: 2010-02-17 13:43:56 EST (Wed, 17 Feb 2010)
New Revision: 59738
URL: http://svn.boost.org/trac/boost/changeset/59738

Log:
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.
Text files modified:
   trunk/boost/property_tree/detail/json_parser_read.hpp | 3 +--
   trunk/boost/property_tree/detail/json_parser_write.hpp | 40 ++++++++++++++++++++--------------------
   trunk/libs/property_tree/test/test_json_parser.cpp | 4 ++--
   3 files changed, 23 insertions(+), 24 deletions(-)

Modified: trunk/boost/property_tree/detail/json_parser_read.hpp
==============================================================================
--- trunk/boost/property_tree/detail/json_parser_read.hpp (original)
+++ trunk/boost/property_tree/detail/json_parser_read.hpp 2010-02-17 13:43:56 EST (Wed, 17 Feb 2010)
@@ -128,7 +128,6 @@
                 {
                     case Ch('\"'): c.string += Ch('\"'); break;
                     case Ch('\\'): c.string += Ch('\\'); break;
- case Ch('0'): c.string += Ch('\0'); break;
                     case Ch('b'): c.string += Ch('\b'); break;
                     case Ch('f'): c.string += Ch('\f'); break;
                     case Ch('n'): c.string += Ch('\n'); break;
@@ -246,7 +245,7 @@
                         ;
                 
                 escape
- = chset_p(detail::widen<Ch>("\"\\0bfnrt").c_str())[typename Context::a_escape(self.c)]
+ = chset_p(detail::widen<Ch>("\"\\bfnrt").c_str())[typename Context::a_escape(self.c)]
                         | 'u' >> uint_parser<unsigned long, 16, 4, 4>()[typename Context::a_unicode(self.c)]
                         ;
                 

Modified: trunk/boost/property_tree/detail/json_parser_write.hpp
==============================================================================
--- trunk/boost/property_tree/detail/json_parser_write.hpp (original)
+++ trunk/boost/property_tree/detail/json_parser_write.hpp 2010-02-17 13:43:56 EST (Wed, 17 Feb 2010)
@@ -21,37 +21,37 @@
 
     // Create necessary escape sequences from illegal characters
     template<class Ch>
- std::basic_string<Ch> create_escapes(const std::basic_string<Ch> &s,
- const std::locale &loc)
+ std::basic_string<Ch> create_escapes(const std::basic_string<Ch> &s)
     {
         std::basic_string<Ch> result;
         typename std::basic_string<Ch>::const_iterator b = s.begin();
         typename std::basic_string<Ch>::const_iterator e = s.end();
         while (b != e)
         {
- if (*b == Ch('\0')) result += Ch('\\'), result += Ch('0');
+ // This assumes an ASCII superset. But so does everything in PTree.
+ // We escape everything outside ASCII, because this code can't
+ // handle high unicode characters.
+ if (*b == 0x20 || *b == 0x21 ||
+ (*b >= 0x23 && *b <= 0x5B) || (*b >= 0x5D && *b <= 0xFF))
+ result += *b;
             else 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');
- else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
+ else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
             else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\');
             else
             {
- if (std::isprint(*b, loc))
- result += *b;
- else
- {
- const char *hexdigits = "0123456789ABCDEF";
- unsigned long u = (std::min)(static_cast<unsigned long>(*b), 0xFFFFul);
- int d1 = u / 4096; u -= d1 * 4096;
- int d2 = u / 256; u -= d2 * 256;
- int d3 = u / 16; u -= d3 * 16;
- int d4 = u;
- result += Ch('\\'); result += Ch('u');
- result += Ch(hexdigits[d1]); result += Ch(hexdigits[d2]);
- result += Ch(hexdigits[d3]); result += Ch(hexdigits[d4]);
- }
+ const char *hexdigits = "0123456789ABCDEF";
+ unsigned long u = (std::min)(static_cast<unsigned long>(*b),
+ 0xFFFFul);
+ int d1 = u / 4096; u -= d1 * 4096;
+ int d2 = u / 256; u -= d2 * 256;
+ int d3 = u / 16; u -= d3 * 16;
+ int d4 = u;
+ result += Ch('\\'); result += Ch('u');
+ result += Ch(hexdigits[d1]); result += Ch(hexdigits[d2]);
+ result += Ch(hexdigits[d3]); result += Ch(hexdigits[d4]);
             }
             ++b;
         }
@@ -72,7 +72,7 @@
         {
             
             // Write value
- Str data = create_escapes(pt.template get_value<Str>(), stream.getloc());
+ Str data = create_escapes(pt.template get_value<Str>());
             stream << Ch('"') << data << Ch('"');
 
         }
@@ -102,7 +102,7 @@
             for (; it != pt.end(); ++it)
             {
                 stream << Str(4 * (indent + 1), Ch(' '));
- stream << Ch('"') << create_escapes(it->first, stream.getloc()) << Ch('"') << Ch(':');
+ stream << Ch('"') << create_escapes(it->first) << Ch('"') << Ch(':');
                 if (it->second.empty())
                     stream << Ch(' ');
                 else

Modified: trunk/libs/property_tree/test/test_json_parser.cpp
==============================================================================
--- trunk/libs/property_tree/test/test_json_parser.cpp (original)
+++ trunk/libs/property_tree/test/test_json_parser.cpp 2010-02-17 13:43:56 EST (Wed, 17 Feb 2010)
@@ -235,7 +235,7 @@
     "}\n";
 
 const char *ok_data_12 =
- "{\" \\\" \\\\ \\0 \\b \\f \\n \\r \\t \" : \"multi\" \"-\" \"string\"}";
+ "{\" \\\" \\\\ \\b \\f \\n \\r \\t \" : \"multi\" \"-\" \"string\"}";
 
 const char *error_data_1 =
     ""; // No root object
@@ -342,7 +342,7 @@
     generic_parser_test_ok<Ptree, ReadFunc, WriteFunc>
     (
         ReadFunc(), WriteFunc(), ok_data_12, NULL,
- "testok12.json", NULL, "testok12out.json", 2, 12, 19
+ "testok12.json", NULL, "testok12out.json", 2, 12, 17
     );
 
     generic_parser_test_error<ptree, ReadFunc, WriteFunc, json_parser_error>


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk