Skip to content

Instantly share code, notes, and snippets.

@pskpatil
Forked from fclairamb/rapidjson_gen_json.cpp
Created January 2, 2022 00:29
Show Gist options
  • Save pskpatil/35e17f6d78d678c1f3827b815dd79e9c to your computer and use it in GitHub Desktop.
Save pskpatil/35e17f6d78d678c1f3827b815dd79e9c to your computer and use it in GitHub Desktop.

Revisions

  1. @fclairamb fclairamb created this gist Jan 5, 2016.
    121 changes: 121 additions & 0 deletions rapidjson_gen_json.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    #ifdef SHELL
    g++ -Wall -Werror -g -I../../cclib/rapidjson/include $0 && ./a.out
    exit 0
    #endif

    // Output is:
    // {"project":"rapidjson","stars":11}
    // {"Name":"XYZ","Rollnumer":2,"array":["hello","world"],"Marks":{"Math":"50","Science":"70","English":"50","Social Science":"70"}}
    // {"FromEmail":"[email protected]","FromName":"Sender's name","Subject":"My subject","Recipients":[{"Email":"[email protected]"}],"Text-part":"this is my text"}


    #include "rapidjson/document.h"
    #include "rapidjson/writer.h"
    #include "rapidjson/stringbuffer.h"
    #include <iostream>

    using namespace rapidjson;

    // https://github.com/miloyip/rapidjson#usage-at-a-glance
    void sample_basic() {
    // 1. Parse a JSON string into DOM.
    const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
    Document d;
    d.Parse(json);

    // 2. Modify it by DOM.
    Value& s = d["stars"];
    s.SetInt(s.GetInt() + 1);

    // 3. Stringify the DOM
    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);

    // 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
    document.SetObject();

    // 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);
    document.Accept(writer);

    std::cout << strbuf.GetString() << std::endl;
    }

    void sample_sendapi() {
    Document d;
    Document::AllocatorType& alloc = d.GetAllocator();

    std::string mytext = "this is my text";

    d.SetObject();

    d.AddMember("FromEmail", "[email protected]", alloc);
    d.AddMember("FromName", "Sender's name", alloc);
    d.AddMember("Subject", "My subject", alloc);


    {
    Value recipients(kArrayType);

    {
    Value recipient(kObjectType);
    recipient.AddMember("Email", "[email protected]", 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);
    d.Accept(writer);

    std::cout << buffer.GetString() << std::endl;
    }

    int main() {
    sample_basic();

    sample_other();

    sample_sendapi();

    return 0;
    }