TEST_F(RapidJson, SIMD_SUFFIX(DocumentParse_CrtAllocator)) { for (size_t i = 0; i < kTrialCount; i++) { memcpy(temp_, json_, length_ + 1); GenericDocument<UTF8<>, CrtAllocator> doc; doc.Parse(temp_); ASSERT_TRUE(doc.IsObject()); } }
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()); }
// Issue 44: SetStringRaw doesn't work with wchar_t TEST(Document, UTF16_Document) { GenericDocument< UTF16<> > json; json.Parse<kParseValidateEncodingFlag>(L"[{\"created_at\":\"Wed Oct 30 17:13:20 +0000 2012\"}]"); ASSERT_TRUE(json.IsArray()); GenericValue< UTF16<> >& v = json[0]; ASSERT_TRUE(v.IsObject()); GenericValue< UTF16<> >& s = v[L"created_at"]; ASSERT_TRUE(s.IsString()); EXPECT_EQ(0, wcscmp(L"Wed Oct 30 17:13:20 +0000 2012", s.GetString())); }
void PitchmarkParameters::load(const boost::filesystem::wpath& path) { boost::filesystem::wifstream ifs(path); wstring json((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>()); GenericStringStream< UTF16<> > buffer(json.c_str()); GenericDocument< UTF16<> > doc; doc.ParseStream(buffer); filename = doc[L"filename"].GetString(); sub_fade_start = doc[L"sub_fade_start"].IsInt()?doc[L"sub_fade_start"].GetInt():0; base_pitch = doc[L"base_pitch"].IsInt() ? doc[L"base_pitch"].GetInt() : 0; { const GenericValue< UTF16<> >& tmp_val = doc[L"base_vowel_wav"]; if (tmp_val.IsObject()) { if (tmp_val[L"filename"].IsString()) { base_vowel_wav_filename = tmp_val[L"filename"].GetString(); } if (tmp_val[L"from"].IsInt()) { base_vowel_wav_from = tmp_val[L"from"].GetInt(); } if (tmp_val[L"to"].IsInt()) { base_vowel_wav_from = tmp_val[L"to"].GetInt(); } } } { const GenericValue< UTF16<> >& tmp_val = doc[L"prefix_vowel_wav"]; if (tmp_val.IsObject()) { if (tmp_val[L"filename"].IsString()) { prefix_vowel_wav_filename = tmp_val[L"filename"].GetString(); } if (tmp_val[L"from"].IsInt()) { prefix_vowel_wav_from = tmp_val[L"from"].GetInt(); } if (tmp_val[L"to"].IsInt()) { prefix_vowel_wav_from = tmp_val[L"to"].GetInt(); } } } { const GenericValue< UTF16<> >& tmp_val = doc[L"pitchmark_points"]; if (tmp_val.IsArray()) { pitchmark_points.resize(tmp_val.Size(),0); for (SizeType i = 0; i < tmp_val.Size(); i++) { pitchmark_points[i] = tmp_val[i].IsInt()?tmp_val[i].GetInt():0; } } } }
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())); }
HANDLE WINAPI EXP_NAME(OpenFilePlugin)(LPCTSTR name, LPCBYTE data, int dataSize #ifdef UNICODE , int OpMode #endif ) { /*if(!SETTINGS_GET(sBrowseXMLFiles, 1)) return INVALID_HANDLE_VALUE;*/ /* int sz = dataSize; while(sz) { if(strchr(" \t\r\n", *data)) { data++; sz--; if(*data != '{' && } break; }*/ { MemoryStream ms((LPCSTR)data, dataSize); AutoUTFInputStream<unsigned, MemoryStream> is(ms); GenericDocument<DocType> d; d.ParseStream(is); auto err = d.GetParseError(); auto ofs= d.GetErrorOffset(); if(err != kParseErrorNone && ofs < (unsigned)dataSize-4) return INVALID_HANDLE_VALUE; } auto plugin = new JsonPlugin(name, data); if(!plugin->IsValidDir()) { delete plugin; return INVALID_HANDLE_VALUE; } return plugin; }
void PitchmarkParameters::save(const boost::filesystem::wpath& path) { GenericDocument< UTF16<> > doc; Document::AllocatorType& allocator = doc.GetAllocator(); doc.SetObject(); doc.AddMember(L"filename", StringRef(filename.c_str()), allocator); doc.AddMember(L"sub_fade_start", sub_fade_start, allocator); doc.AddMember(L"base_pitch", base_pitch, allocator); { GenericValue< UTF16<> > base_vowel_wav(kObjectType); base_vowel_wav.AddMember(L"filename", StringRef(base_vowel_wav_filename.c_str()), allocator); base_vowel_wav.AddMember(L"from", base_vowel_wav_from, allocator); base_vowel_wav.AddMember(L"to", base_vowel_wav_to, allocator); doc.AddMember(L"base_vowel_wav", base_vowel_wav, allocator); } { GenericValue< UTF16<> > prefix_vowel_wav(kObjectType); prefix_vowel_wav.AddMember(L"filename", StringRef(prefix_vowel_wav_filename.c_str()), allocator); prefix_vowel_wav.AddMember(L"from", prefix_vowel_wav_from, allocator); prefix_vowel_wav.AddMember(L"to", prefix_vowel_wav_to, allocator); doc.AddMember(L"prefix_vowel_wav", prefix_vowel_wav, allocator); } { GenericValue< UTF16<> > tmp_val(kArrayType); for (size_t i=0; i<pitchmark_points.size(); i++) { tmp_val.PushBack(pitchmark_points[i], allocator); } doc.AddMember(L"pitchmark_points", tmp_val, allocator); } GenericStringBuffer< UTF16<> > buffer; PrettyWriter<GenericStringBuffer< UTF16<> >, UTF16<>, ASCII<> > writer(buffer); doc.Accept(writer); boost::filesystem::wofstream ofs(path, ios_base::trunc); ofs << buffer.GetString(); }
void ScoreNAK::load() { clearNotes(); wcout << L"nak: " << getScorePath() << endl; boost::filesystem::wifstream ifs(path_score); wstring json((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>()); GenericStringStream< UTF16<> > buffer(json.c_str()); GenericDocument< UTF16<> > doc; doc.ParseStream(buffer); if (!doc.HasMember(L"Score")) { cerr << "[ScoreNAK::load]" << getScorePath() << " is not nak score." << endl; return; } const GenericValue< UTF16<> >& tmp_score = doc[L"Score"]; if (!tmp_score.HasMember(L"Notes")) { cerr << "[ScoreNAK::load]" << getScorePath() << " does not have Notes." << endl; return; } const GenericValue< UTF16<> >& tmp_notes = tmp_score[L"Notes"]; if (tmp_notes.IsArray()) { for (SizeType i = 0; i < tmp_notes.Size(); i++) { if (tmp_notes[i][L"id"].IsInt()) { Note tmp_note(this, tmp_notes[i][L"id"].GetInt()); if (tmp_notes[i].HasMember(L"alias") && tmp_notes[i][L"alias"].IsString()) { tmp_note.setPronAlias(tmp_notes[i][L"alias"].GetString()); } if (tmp_notes[i].HasMember(L"start") && tmp_notes[i][L"start"].IsInt()) { tmp_note.setStart(tmp_notes[i][L"start"].GetInt()); } if (tmp_notes[i].HasMember(L"end") && tmp_notes[i][L"end"].IsInt()) { tmp_note.setEnd(tmp_notes[i][L"end"].GetInt()); } if (tmp_notes[i].HasMember(L"front_margin") && tmp_notes[i][L"front_margin"].IsInt() && tmp_notes[i].HasMember(L"back_margin") && tmp_notes[i][L"back_margin"].IsInt()) { tmp_note.setMargin(tmp_notes[i][L"front_margin"].GetInt(), tmp_notes[i][L"back_margin"].GetInt()); } if (tmp_notes[i].HasMember(L"front_padding") && tmp_notes[i][L"front_padding"].IsInt() && tmp_notes[i].HasMember(L"back_padding") && tmp_notes[i][L"back_padding"].IsInt()) { tmp_note.setPadding(tmp_notes[i][L"front_padding"].GetInt(), tmp_notes[i][L"back_padding"].GetInt()); } if (tmp_notes[i].HasMember(L"vel") && tmp_notes[i][L"vel"].IsInt()) { tmp_note.setBaseVelocity(tmp_notes[i][L"vel"].GetInt()); } if (tmp_notes[i].HasMember(L"pitch") && tmp_notes[i][L"pitch"].IsInt()) { tmp_note.setBasePitch(tmp_notes[i][L"pitch"].GetInt()); } if (tmp_notes[i].HasMember(L"vel_points") && tmp_notes[i][L"vel_points"].IsArray()) { const GenericValue< UTF16<> >& tmp_vel_points = tmp_notes[i][L"vel_points"]; for (SizeType i = 0; i < tmp_vel_points.Size(); i++) { if (tmp_vel_points[L"vel_points"].IsArray()) { const GenericValue< UTF16<> >& tmp_vel_point = tmp_vel_points[i][L"vel_points"]; tmp_note.addVelocityPoint(tmp_vel_point[SizeType(0)].IsInt()?tmp_vel_point[SizeType(0)].GetInt():0, tmp_vel_point[SizeType(1)].IsInt()?tmp_vel_point[SizeType(1)].GetInt():0); } } } addNote(tmp_note); } } } reloadPitches(); }
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; } }