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)