diff --git a/include/rapidjson/encodedstream.h b/include/rapidjson/encodedstream.h index f33b14e..3be89f9 100644 --- a/include/rapidjson/encodedstream.h +++ b/include/rapidjson/encodedstream.h @@ -30,7 +30,7 @@ public: size_t Tell() const { return is_.Tell(); } // Not implemented - void Put(Ch c) { RAPIDJSON_ASSERT(false); } + void Put(Ch) { RAPIDJSON_ASSERT(false); } void Flush() { RAPIDJSON_ASSERT(false); } Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 9010aec..1a4be6f 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -491,7 +491,7 @@ private: InputStream& s(copy.s); if (parseFlags & kParseInsituFlag) { - Ch *head = s.PutBegin(); + typename InputStream::Ch *head = s.PutBegin(); ParseStringToStream(s, s); if (HasParseError()) return; diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index ac7d623..0253439 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -1,6 +1,9 @@ #include "unittest.h" #include "rapidjson/document.h" #include "rapidjson/writer.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/encodedstream.h" +#include "rapidjson/stringbuffer.h" #include using namespace rapidjson; @@ -47,6 +50,57 @@ TEST(Document, Parse) { EXPECT_EQ(i + 1, a[i].GetUint()); } +static FILE* OpenEncodedFile(const char* filename) { + char buffer[1024]; + sprintf(buffer, "encodings/%s", filename); + FILE *fp = fopen(buffer, "rb"); + if (!fp) { + sprintf(buffer, "../../bin/encodings/%s", filename); + fp = fopen(buffer, "rb"); + } + return fp; +} + +TEST(Document, ParseStream_EncodedInputStream) { + // UTF8 -> UTF16 + FILE* fp = OpenEncodedFile("utf8.json"); + char buffer[256]; + FileReadStream bis(fp, buffer, sizeof(buffer)); + EncodedInputStream, FileReadStream> eis(bis); + + GenericDocument > d; + d.ParseStream<0, UTF8<> >(eis); + EXPECT_FALSE(d.HasParseError()); + + fclose(fp); + + wchar_t expected[] = L"I can eat glass and it doesn't hurt me."; + GenericValue >& v = d[L"en"]; + EXPECT_TRUE(v.IsString()); + EXPECT_EQ(sizeof(expected) / sizeof(wchar_t) - 1, v.GetStringLength()); + EXPECT_EQ(0, StrCmp(expected, v.GetString())); + + // UTF16 -> UTF8 in memory + StringBuffer bos; + typedef EncodedOutputStream, StringBuffer> OutputStream; + OutputStream eos(bos, false); // Not writing BOM + Writer, UTF8<> > writer(eos); + d.Accept(writer); + + { + // Condense the original file and compare. + FILE *fp = OpenEncodedFile("utf8.json"); + FileReadStream is(fp, buffer, sizeof(buffer)); + Reader reader; + StringBuffer bos2; + Writer writer(bos2); + reader.Parse(is, writer); + + EXPECT_EQ(bos.GetSize(), bos2.GetSize()); + EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize())); + } +} + TEST(Document, Swap) { Document d1; Document::AllocatorType& a = d1.GetAllocator();