Advance Thanks for the help.
I have a requirement to create array of json records of 10KB each. I am using rapidjson::writer and rapidjson::stringbuffer. I am getting Assertion error (at writer.h #488, !hasRoot_ error) for the below mentioned code piece:
rapidjson::StringBuffer s;
rapidjson::Writerrapidjson::StringBuffer writer(s);
writer.StartArray();
for (auto itr =myMap.begin(); itr != myMap.end(); itr++) {
writer.StartObject();
writer.Key(itr->first.c_str());
writer.String(itr->second.c_str());
writer.EndObject();
if (cond_matches) {
writer.EndArray();
Process( s.GetString() );
s.Clear();
writer.Reset(s); //(Without Resetting writer, next line seems to cause Assertion error.)
writer.StartArray(); //start another array of records
}
}
Error that I am getting:
../include/rapidjson/writer.h:488: void rapidjson::Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags>::Prefix(rapidjson::Type) [with OutputStream = rapidjson::GenericStringBuffer<rapidjson::UTF8<> >; SourceEncoding = rapidjson::UTF8<>; TargetEncoding = rapidjson::UTF8<>; StackAllocator = rapidjson::CrtAllocator; unsigned int writeFlags = 0]: Assertion `!hasRoot_' failed.
What am I doing wrong here? Any workaround for this use case? Thanks.
Expectation is that the entire entries in **myMap ** map should be converted to series of 10KB json records.
Edited: After adding writer.Reset(s) post s.clear() seems to have resolved the issue (As Peter suggested). One additional question: Is this code piece is bug free with respect to the way in which RapidJson Writer & Buffer is being used & JSON is being created?