TEST(IStreamWrapper, wfstream) { wfstream fs; ASSERT_TRUE(Open(fs, "utf16bebom.json")); fs.imbue(std::locale(fs.getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>)); WIStreamWrapper isw(fs); GenericDocument<UTF16<> > d; d.ParseStream<kParseDefaultFlags, UTF16<>, WIStreamWrapper>(isw); EXPECT_TRUE(!d.HasParseError()); EXPECT_TRUE(d.IsObject()); EXPECT_EQ(5, d.MemberCount()); }
TEST(JsonChecker, Reader) { char filename[256]; // jsonchecker/failXX.json for (int i = 1; i <= 33; i++) { if (i == 18) // fail18.json is valid in rapidjson, which has no limitation on depth of nesting. continue; sprintf(filename, "jsonchecker/fail%d.json", i); size_t length; char* json = ReadFile(filename, length); if (!json) { sprintf(filename, "../../bin/jsonchecker/fail%d.json", i); json = ReadFile(filename, length); if (!json) { printf("jsonchecker file %s not found", filename); continue; } } GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) if (!document.Parse((const char*)json).HasParseError()) FAIL(); //printf("%s(%u):%s\n", filename, (unsigned)document.GetErrorOffset(), document.GetParseError()); free(json); } // passX.json for (int i = 1; i <= 3; i++) { sprintf(filename, "jsonchecker/pass%d.json", i); size_t length; char* json = ReadFile(filename, length); if (!json) { sprintf(filename, "../../bin/jsonchecker/pass%d.json", i); json = ReadFile(filename, length); if (!json) { printf("jsonchecker file %s not found", filename); continue; } } GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) document.Parse((const char*)json); EXPECT_TRUE(!document.HasParseError()); free(json); } }
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. fp = OpenEncodedFile("utf8.json"); FileReadStream is(fp, buffer, sizeof(buffer)); Reader reader; StringBuffer bos2; Writer<StringBuffer> writer2(bos2); reader.Parse(is, writer2); fclose(fp); EXPECT_EQ(bos.GetSize(), bos2.GetSize()); EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize())); }
void JSObject::Fill(Berkelium::WideString str) { using namespace rapidjson; children.clear(); /** {Test} Berkelium::WideString <-> std::string, std::wstring */ /* Berkelium::UTF8String utf8str = Berkelium::WideToUTF8(str); std::string cstr = Berkelium::WideToUTF8(str).data(); cstr.resize(str.length()); std::wstring wstr = str.data(); wstr.resize(str.length()); // convert back from ctr Berkelium::WideString w1 = Berkelium::UTF8ToWide( Berkelium::UTF8String::point_to(cstr) ); // convert back from utf8str Berkelium::WideString w2 = Berkelium::UTF8ToWide( utf8str ); // wstring -> WideString [this must be correct one] Berkelium::WideString w3 = Berkelium::WideString::point_to(wstr.c_str()); */ if(!str.length()) { value.str = L""; value.type = JSTypeString; return; } std::wstring wstr = str.data(); wstr.resize(str.length()); GenericDocument<UTF8<wchar_t>> d; d.Parse<0>(wstr.c_str()); if(d.HasParseError()) { value.str = wstr; value.type = JSTypeString; return; } switch(d.GetType()) { case kNullType: //!< null value = JSValue(); break; case kFalseType: //!< false value = JSValue(false); break; case kTrueType: //!< true value = JSValue(true); break; case kObjectType: //!< object value.type = JSTypeObject; for(GenericValue<UTF8<wchar_t>>::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr) { children.push_back(std::make_pair(itr->name.GetString(), JSObject())); children.back().second.FillRecursive(itr->value); } break; case kArrayType: //!< array value.type = JSTypeObject; children.resize(d.Size()); for(SizeType i = 0; i < d.Size(); i++) { children[i].second.FillRecursive(d[i]); } break; case kStringType: //!< string value.str = wstr; value.type = JSTypeString; break; case kNumberType: //!< number value = JSValue(d.GetDouble()); break; } }