diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 403daa52..6aba86b0 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -11,10 +11,10 @@ namespace rapidjson { \tparam Encoding Encoding of both source strings and output. \tparam Allocator Type of allocator for allocating memory of stack. */ -template, typename Allocator = MemoryPoolAllocator<> > -class PrettyWriter : public Writer { +template, typename TargetEncoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> > +class PrettyWriter : public Writer { public: - typedef Writer Base; + typedef Writer Base; typedef typename Base::Ch Ch; //! Constructor diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 08fb81e7..7ff9944b 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -23,10 +23,10 @@ namespace rapidjson { \tparam Encoding Encoding of both source strings and output. \implements Handler */ -template, typename Allocator = MemoryPoolAllocator<> > +template, typename TargetEncoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> > class Writer { public: - typedef typename Encoding::Ch Ch; + typedef typename SourceEncoding::Ch Ch; Writer(Stream& stream, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : stream_(stream), level_stack_(allocator, levelDepth * sizeof(Level)) {} @@ -166,7 +166,7 @@ protected: } void WriteString(const Ch* str, SizeType length) { - static const char hexDigits[] = "0123456789ABCDEF"; + static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; static const char escape[256] = { #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 //0 1 2 3 4 5 6 7 8 9 A B C D E F @@ -180,19 +180,22 @@ protected: }; stream_.Put('\"'); - for (const Ch* p = str; p != str + length; ++p) { - if ((sizeof(Ch) == 1 || *p < 256) && escape[(unsigned char)*p]) { + GenericStringStream is(str); + while (is.Tell() < length) { + const Ch c = is.Peek(); + if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) { + is.Take(); stream_.Put('\\'); - stream_.Put(escape[(unsigned char)*p]); - if (escape[(unsigned char)*p] == 'u') { + stream_.Put(escape[(unsigned char)c]); + if (escape[(unsigned char)c] == 'u') { stream_.Put('0'); stream_.Put('0'); - stream_.Put(hexDigits[(*p) >> 4]); - stream_.Put(hexDigits[(*p) & 0xF]); + stream_.Put(hexDigits[(unsigned char)c >> 4]); + stream_.Put(hexDigits[(unsigned char)c & 0xF]); } } else - stream_.Put(*p); + Transcoder::Transcode(is, stream_); } stream_.Put('\"'); } diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index ed8d7644..909587fa 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -54,3 +54,13 @@ TEST(Writer, String) { TEST_ROUNDTRIP("[\"Hello\\u0000World\"]"); TEST_ROUNDTRIP("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]"); } + +TEST(Writer, Transcode) { + // UTF8 -> UTF16 -> UTF8 + StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3], \"dollar\":\"\x24\", \"cents\":\"\xC2\xA2\", \"euro\":\"\xE2\x82\xAC\", \"gclef\":\"\xF0\x9D\x84\x9E\" } "); + StringBuffer buffer; + Writer, UTF8<> > writer(buffer); + GenericReader, UTF16<> > reader; + reader.Parse<0>(s, writer); + EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}", buffer.GetString()); +} \ No newline at end of file