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 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(); }