A C++ program to generate json output

In this post, constructing and printing of JSON output using rapidjson library has been discussed.
For instance, we have a file (probe.json) containing the following json output –

{ MTConnectDevices:
{ attrs:
{ 'xmlns:m': 'urn:mtconnect.org:MTConnectDevices:1.2',
xmlns: 'urn:mtconnect.org:MTConnectDevices:1.2',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation': 'urn:mtconnect.org:MTConnectDevices:1.2 http://www.mtconnect.org/schemas/MTConnectDevices_1.2.xsd' },
Header:
[ { attrs:
{ creationTime: '2014-09-16T01:01:01Z',
sender: 'vimana',
instanceId: '001',
version: '1.2.0.23',
assetBufferSize: '1024',
assetCount: '0',
bufferSize: '524288' } } ],
Devices:
[ { Device:
[ { attrs: { id: 'DEVICE_01', name: 'Device 01', uuid: 'DEVICE-01' },
Description:
[ { charValue: 'DEVICE_01, DEVICE-01',
attrs: { manufacturer: 'Manufacturer-01' } } ] } ] } ] } }

A c++ code for this may be written in the following manner-

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

using namespace rapidjson;

int main()
{
const char* probe=”{ \”MTConnectDevices\”:\n{ \”attrs\”:\n{ \”xmlns:m\”: \”urn:mtconnect.org:MTConnectDevices:1.2\”,\”xmlns\”: \”urn:mtconnect. org:MTConnectDevices:1.2\”,\”xmlns:xsi\”: \”http://www.w3.org/2001/XMLSchema-instance\”,\”xsi:schemaLocation\”: \”urn:mtconnect.org:MTConnectDevice s:1.2 http://www.mtconnect.org/schemas/MTConnectDevices_1.2.xsd\” },\n\”Header\”:\n[ { \”attrs\”:\n{ \”creationTime\”: \”2014-09-16T01:01:01Z\”,\n\ “sender\”:\n \”vimana\”,\n\”instanceId\”: \”001\”,\n\”version\”: \”1.2.0.23\”,\n\”assetBufferSize\”:\”1024\”,\n\”assetCount\”: \”0\”,\n\”bufferSize \”: \”524288\” } } ],\n\”Devices\”:\n[ { \”Device\”:\n[ { \”attrs\”: { \”id\”: \”DEVICE_01\”, \”name\”: \”Device 01\”, \”uuid\”: \”DEVICE-01\” },\n \”Description\”:\n[ { \”charValue\”: \”DEVICE_01, DEVICE-01\”,\n\”attrs\”: { \”manufacturer\”: \”Manufacturer-01\” } } ] } ] } ] }}”;

Document d;
d.Parse(probe);
printf(“PROBE :\n %s \n”,probe);

}

Compilation of the code can be done by –
g++ probe.cpp
On success,type
./a.out

the output obtained is –

PROBE :
{ "MTConnectDevices":
{ "attrs":
{ "xmlns:m": "urn:mtconnect.org:MTConnectDevices:1.2","xmlns": "urn:mtconnect.org:MTConnectDevices:1.2","xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance","xsi:schemaLocation": "urn:mtconnect.org:MTConnectDevices:1.2 http://www.mtconnect.org/schemas/MTConnectDevices_1.2.xsd" },
"Header":
[ { "attrs":
{ "creationTime": "2014-09-16T01:01:01Z",
"sender":
"vimana",
"instanceId": "001",
"version": "1.2.0.23",
"assetBufferSize":"1024",
"assetCount": "0",
"bufferSize": "524288" } } ],
"Devices":
[ { "Device":
[ { "attrs": { "id": "DEVICE_01", "name": "Device 01", "uuid": "DEVICE-01" },
"Description":
[ { "charValue": "DEVICE_01, DEVICE-01",
"attrs": { "manufacturer": "Manufacturer-01" } } ] } ] } ] }}

(This .cpp file should be present in the sub-folder ../rapidjson/include . My previous post can be referred for information on rapidjson folder)

Advertisements

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”.