Fixes a compilation error in Reader when using encoded stream.
Added unit test for parsing EncodedStream to Document.
This commit is contained in:
parent
a787e8a7f5
commit
b01093b16c
@ -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; }
|
||||
|
@ -491,7 +491,7 @@ private:
|
||||
InputStream& s(copy.s);
|
||||
|
||||
if (parseFlags & kParseInsituFlag) {
|
||||
Ch *head = s.PutBegin();
|
||||
typename InputStream::Ch *head = s.PutBegin();
|
||||
ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s);
|
||||
if (HasParseError())
|
||||
return;
|
||||
|
@ -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 <sstream>
|
||||
|
||||
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<UTF8<>, FileReadStream> eis(bis);
|
||||
|
||||
GenericDocument<UTF16<> > 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<UTF16<> >& 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<UTF8<>, StringBuffer> OutputStream;
|
||||
OutputStream eos(bos, false); // Not writing BOM
|
||||
Writer<OutputStream, UTF16<>, 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<StringBuffer> 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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user