Standalone C++ program to create the JSON for one XML output using rapidjson.

Rapidjson is a C++ JSON parser and generator. In other words, it helps a C++ program to read JSON data and write JSON data.
rapidjson supports SAX (Simple API for XML) style and DOM (Document Object Model) for parsing.

Installation
(rapidjson is a header-only library. That means, the only thing to be done is to copy rapidjson/include/rapidjson and its sub-directories to your project or other include paths.)
the steps mentioned below are for the Mint distribution-

The rapidjson package can be downloaded from here – http://code.google.com/p/rapidjson/downloads/list . It can then be unpacked.
(considering that the folder containing the installed rapidjson is ‘rapidjson’)

before proceeding, certain libraries need to be installed, which can be done by-
$sudo apt-get install gcc-multilib g++-multilib

now, run GNU make in build/gmake,(inside ‘rapidjson’) for example-
$make -f test.make config=release32
$make -f example.make config=debug32

On success, the executable are generated at rapidjson/bin

inside the ‘include’ sub-folder (in rapidjson), a file (let’s say, prog1.cpp) containing the following c++ code is to be added –

#include "rapidjson/document.h"
#include

int main() {
const char json[] = “{ \”hello\” : \”world\” }”;

rapidjson::Document d;
d.Parse(json);

printf(“%s\n”, d[“hello”].GetString());

return 0;
}
(the document.h header file can be found inside ‘rapidjson/include/rapidjson’)

In order to compile this code, on the terminal type the command-
$g++ test1.cpp
Once the code has been successfully compiled without any errors, the executables for the same can be created using-
$./a.out
which gives the output –
world

This example parses a JSON text string into a DOM tree. Then it obtains the “hello” member of the root JSON object. Finally it obtains the string of that value, which is “world”.

Advertisements

4 thoughts on “Standalone C++ program to create the JSON for one XML output using rapidjson.

    • Hi Anshul, my concern while writing this blog was to simply parse a JSON text string. As far as your query is concerned, I could not find a potential way to do so. You can look up for yourself regarding the same. Hopefully you will find an online convertor sort of thing but if you do manage to find a valid solution to this , do let me know.
      Thanks!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s