Instantly share code, notes, and snippets.

What would you like to do?
Write some JSON using a rapidjson library
#ifdef SHELL
g++ -Wall -Werror -g -I../../cclib/rapidjson/include $0 && ./a.out
exit 0
// Output is:
// {"project":"rapidjson","stars":11}
// {"Name":"XYZ","Rollnumer":2,"array":["hello","world"],"Marks":{"Math":"50","Science":"70","English":"50","Social Science":"70"}}
// {"FromEmail":"","FromName":"Sender's name","Subject":"My subject","Recipients":[{"Email":""}],"Text-part":"this is my text"}
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
void sample_basic() {
// 1. Parse a JSON string into DOM.
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
// 2. Modify it by DOM.
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);
// 3. Stringify the DOM
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
// Output {"project":"rapidjson","stars":11}
std::cout << buffer.GetString() << std::endl;
void sample_other() {
// document is the root of a json message
rapidjson::Document document;
// define the document as an object rather than an array
// create a rapidjson array type with similar syntax to std::vector
rapidjson::Value array(rapidjson::kArrayType);
// must pass an allocator when the object may need to allocate memory
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
// chain methods as rapidjson provides a fluent interface when modifying its objects
array.PushBack("hello", allocator).PushBack("world", allocator);//"array":["hello","world"]
document.AddMember("Name", "XYZ", allocator);
document.AddMember("Rollnumer", 2, allocator);
document.AddMember("array", array, allocator);
// create a rapidjson object type
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("Math", "50", allocator);
object.AddMember("Science", "70", allocator);
object.AddMember("English", "50", allocator);
object.AddMember("Social Science", "70", allocator);
document.AddMember("Marks", object, allocator);
// fromScratch["object"]["hello"] = "Yourname";
StringBuffer strbuf;
Writer<StringBuffer> writer(strbuf);
std::cout << strbuf.GetString() << std::endl;
void sample_sendapi() {
Document d;
Document::AllocatorType& alloc = d.GetAllocator();
std::string mytext = "this is my text";
d.AddMember("FromEmail", "", alloc);
d.AddMember("FromName", "Sender's name", alloc);
d.AddMember("Subject", "My subject", alloc);
Value recipients(kArrayType);
Value recipient(kObjectType);
recipient.AddMember("Email", "", alloc);
recipients.PushBack(recipient, alloc);
d.AddMember("Recipients", recipients, alloc);
Value textPart;
textPart.SetString(mytext.c_str(), alloc);
d.AddMember("Text-part", textPart, alloc);
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
std::cout << buffer.GetString() << std::endl;
int main() {
return 0;

This comment has been minimized.

Copy link

aditya-singh-26 commented May 24, 2017

@fclairamb Hey, I had a query that how can we deallocate the memory properly for the above code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment