Example #1
0
TEST(Writer, Transcode) {
    const char json[] = "{\"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\"}";

    // UTF8 -> UTF16 -> UTF8
    {
        StringStream s(json);
        StringBuffer buffer;
        Writer<StringBuffer, UTF16<>, UTF8<> > writer(buffer);
        GenericReader<UTF8<>, UTF16<> > reader;
        reader.Parse(s, writer);
        EXPECT_STREQ(json, buffer.GetString());
    }

    // UTF8 -> UTF8 -> ASCII -> UTF8 -> UTF8
    {
        StringStream s(json);
        StringBuffer buffer;
        Writer<StringBuffer, UTF8<>, ASCII<> > writer(buffer);
        Reader reader;
        reader.Parse(s, writer);

        StringBuffer buffer2;
        Writer<StringBuffer> writer2(buffer2);
        GenericReader<ASCII<>, UTF8<> > reader2;
        StringStream s2(buffer.GetString());
        reader2.Parse(s2, writer2);

        EXPECT_STREQ(json, buffer2.GetString());
    }
}
Example #2
0
TEST(Reader, ParseString_Transcoding) {
    const char* x = "\"Hello\"";
    const wchar_t* e = L"Hello";
    GenericStringStream<UTF8<> > is(x);
    GenericReader<UTF8<>, UTF16<> > reader;
    ParseStringHandler<UTF16<> > h;
    reader.Parse(is, h);
    EXPECT_EQ(0, StrCmp<UTF16<>::Ch>(e, h.str_));
    EXPECT_EQ(StrLen(e), h.length_);
}
Example #3
0
int main(int, char*[]) {
#ifdef _WIN32
    // Prevent Windows converting between CR+LF and LF
    _setmode(_fileno(stdin), _O_BINARY);    // NEW
    _setmode(_fileno(stdout), _O_BINARY);   // NEW
#endif

    // Prepare reader and input stream.
    //Reader reader;
    GenericReader<AutoUTF<unsigned>, UTF8<> > reader;       // CHANGED
    char readBuffer[65536];
    FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
    AutoUTFInputStream<unsigned, FileReadStream> eis(is);   // NEW

    // Prepare writer and output stream.
    char writeBuffer[65536];
    FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));

#if 1
    // Use the same Encoding of the input. Also use BOM according to input.
    typedef AutoUTFOutputStream<unsigned, FileWriteStream> OutputStream;    // NEW
    OutputStream eos(os, eis.GetType(), eis.HasBOM());                      // NEW
    PrettyWriter<OutputStream, UTF8<>, AutoUTF<unsigned> > writer(eos);     // CHANGED
#else
    // You may also use static bound encoding type, such as output to UTF-16LE with BOM
    typedef EncodedOutputStream<UTF16LE<>,FileWriteStream> OutputStream;    // NEW
    OutputStream eos(os, true);                                             // NEW
    PrettyWriter<OutputStream, UTF8<>, UTF16LE<> > writer(eos);             // CHANGED
#endif

    // JSON reader parse from the input stream and let writer generate the output.
    //if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) {
    if (!reader.Parse<kParseValidateEncodingFlag>(eis, writer)) {   // CHANGED
        fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
        return 1;
    }

    return 0;
}
static inline bool successful_whole_read (GenericReader& r, void* const buf, size_t const bufbytesize) {
    bool const readprecice(r.read(buf, bufbytesize));
    DASSERT(readprecice);
    return readprecice;
}
static inline bool successful_whole_read (GenericReader& r, T& obj) {
    bool const readprecice(r.read(&obj, sizeof(T)));
    DASSERT(readprecice);
    return readprecice;
}