rapidjson: working code for reading document from file?

morde picture morde · Aug 7, 2013 · Viewed 21.2k times · Source

I need a working c++ code for reading document from file using rapidjson: https://code.google.com/p/rapidjson/

In the wiki it's yet undocumented, the examples unserialize only from std::string, I haven't a deep knowledge of templates.

I serialized my document into a text file and this is the code I wrote, but it doesn't compile:

#include "rapidjson/prettywriter.h" // for stringify JSON
#include "rapidjson/writer.h"   // for stringify JSON
#include "rapidjson/filestream.h"   // wrapper of C stream for prettywriter as output
[...]
std::ifstream myfile ("c:\\statdata.txt");
rapidjson::Document document;
document.ParseStream<0>(myfile);

the compilation error state: error: 'Document' is not a member of 'rapidjson'

I'm using Qt 4.8.1 with mingw and rapidjson v 0.1 (I already try with upgraded v 0.11 but the error remain)

Answer

Drew Noakes picture Drew Noakes · Oct 27, 2013

The FileStream in @Raanan's answer is apparently deprecated. There's a comment in the source code that says to use FileReadStream instead.

#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>

using namespace rapidjson;

// ...

FILE* pFile = fopen(fileName.c_str(), "rb");
char buffer[65536];
FileReadStream is(pFile, buffer, sizeof(buffer));
Document document;
document.ParseStream<0, UTF8<>, FileReadStream>(is);