How to write JSON to cout in RapidJSON
RapidJSON does not provide a straightforward way of serializing JSON to cout
(= stdout), but you can use OStreamWrapper
to do that:
#include <rapidjson/writer.h>
#include <rapidjson/ostreamwrapper.h>
// ...
OStreamWrapper out(cout);
Writer<OStreamWrapper> writer(out);
doc.Accept(writer);
Full example:
#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/ostreamwrapper.h>
using namespace rapidjson;
using namespace std;
int main() {
// Generate document: {"text": "Hello JSON!"}
Document doc;
doc.SetObject(); // Make doc an object !
doc.AddMember("text", "Hello JSON!", doc.GetAllocator());
// Write to stdout
OStreamWrapper out(cout);
Writer<OStreamWrapper> writer(out);
doc.Accept(writer);
}