Skip to content

Instantly share code, notes, and snippets.

@palashkulsh
Last active November 29, 2019 10:35
Show Gist options
  • Save palashkulsh/f5d4a1ed98002bde58ce9b3b08aa2b59 to your computer and use it in GitHub Desktop.
Save palashkulsh/f5d4a1ed98002bde58ce9b3b08aa2b59 to your computer and use it in GitHub Desktop.
I've been trying to get started with json in c++ and i think i've finally found out a way.
Following are the steps.
run the following command and it will download the library to a folder
git clone https://github.com/miloyip/rapidjson
This will clone the following repo which provides json usage in c++
now make a new folder where you will be writing your c++ code
now copy the rapidjson/include/rapidjson to your project folder you made in step 4
now your folder should have YOUR_FOLDER_NAME/rapidjson present in it.we will use this directory to include rapidjson library in our file
now copy the attached file in your project folder you made in step 4
compile the file using g++ testing.cpp
run the file using ./a.out
it should print the result.
now you'll have to go through its documentation to learn more about it. Documentation is here http://rapidjson.org/
// rapidjson/example/simpledom/simpledom.cpp
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
int main() {
// 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;
return 0;
}
// rapidjson/example/simpledom/simpledom.cpp
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
int main() {
// 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;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment