Read an JSON content

The first thing to do when reading or writing a JSON file/stream, is to declare the Document interface

Include ejson

#include <ejson/ejson.hpp>

Declare document interface

Read an JSON file

File to read: "read.json"

{
"object A": "bonjour",
"object B": null,
"object C": true,
"object D": 123854,
"object E": [
1,2,3,54,false
],
"object F": {
"a": 1,
"b": 2
}
}

Reading a file is done like this:

bool retParse = doc.load("DATA:read.json");

The file naming is manage by etk::FSNode that provide "DATA:" start string for internal application asset. You can use external path like "./plop/file.json" too.

Read an JSON Stream

Reading a stream is done like this:

std::string stream = "{"
" \"object A\":\"bonjour\","
" \"object B\":null,"
" \"object C\":true,"
" \"object D\":123854.215,"
" \"object E\":["
" 1,2,3,54,false"
" ],"
" \"object F\":{"
" \"a\":1,"
" \"b\":2"
" }"
"}";
bool retParse = doc.parse(stream);

In C and C++ it is very hard to read string with the \" then to simplify parsing of json the parser engine support the use of simple **'** interface:

std::string stream = "{"
" objectA:'bonjour',"
" objectB:null,"
" objectC:true,"
" objectD:123854.215,"
" objectE:["
" #simple comment one Line"
" 1,2,3,54,false"
" ],"
" objectF:{"
" a:1,"
" b:2"
" }"
"}";
bool retParse = doc.parse(stream);

Access at all Element datas

In an ejson::Object (or ejson::Document) the sub-nodes are accessible threw an abstraction class stores in an element name Value

Get a value with its name:

ejson::Value element = doc["object C"];

Access to a string in an Object:

The generic Way to access to an element is to convert it in the corect type:

ejson::String elem = doc["object A"].toString();
// Get the value:
std::string value = elem.get();

Commonly you might use like this:

std::string value = doc["object A"].toString().get();

Access to a boolean in an Object:

bool value = doc["object C"].toBoolean().get();

Access to a Number in an Object:

double value = doc["object D"].toNumber().get();

Access to a Array in an Object:

Get the Array

ejson::Array array = doc["object E"].toArray();

Move threw all element:

for (const auto itArray: array) {
TEST_INFO(" " << itArray);
}

Move threw all element in C mode:

for (size_t iii=0; iii<array.size(); ++iii) {
TEST_INFO(" " << array[iii]);
}

Access to a Object in an Object:

Get the Object

ejson::Object obj = doc["object F"].toObject();

Move threw all element:

for (const auto itObj: obj) {
TEST_INFO(" " << itObj);
}

Move threw all element in C mode:

for (size_t iii=0; iii<obj.size(); ++iii) {
TEST_INFO(" " << obj[iii]);
}

Now we can copy the Object we want

ejson manage reference object, then it is possible that 2 part of the software access at the same object/element ...

You can copy object to separate or duplicate section.

This is named cloning:

ejson::Value tmpClone = doc["object F"].clone();

All example file

#include <test-debug/debug.hpp>
#include <ejson/ejson.hpp>
#include "read.hpp"
static void readFromFile() {
bool retParse = doc.load("DATA:read.json");
TEST_INFO("parse ret = " << retParse);
TEST_INFO("Debug display of the tree:");
doc.display();
}
static void readFromString1() {
TEST_INFO("parse");
std::string stream = "{"
" \"object A\":\"bonjour\","
" \"object B\":null,"
" \"object C\":true,"
" \"object D\":123854.215,"
" \"object E\":["
" 1,2,3,54,false"
" ],"
" \"object F\":{"
" \"a\":1,"
" \"b\":2"
" }"
"}";
bool retParse = doc.parse(stream);
TEST_INFO("parse ret = " << retParse);
TEST_INFO("Debug display of the tree:");
doc.display();
}
static void readFromString2() {
TEST_INFO("parse");
std::string stream = "{"
" objectA:'bonjour',"
" objectB:null,"
" objectC:true,"
" objectD:123854.215,"
" objectE:["
" #simple comment one Line"
" 1,2,3,54,false"
" ],"
" objectF:{"
" a:1,"
" b:2"
" }"
"}";
bool retParse = doc.parse(stream);
TEST_INFO("parse ret = " << retParse);
TEST_INFO("Debug display of the tree:");
doc.display();
}
static void readFull() {
TEST_INFO("parse");
bool retParse = doc.load("DATA:read.json");
TEST_INFO("parse ret = " << retParse);
TEST_INFO("Debug display of the tree:");
doc.display();
TEST_INFO("list of Object:");
for (const auto it: doc) {
TEST_INFO(" " << it);
if (it.isObject() == true) {
ejson::Object obj = it.toObject();
// check if the convertion in ejson::Object has been done corectly
if (obj.exist() == false) {
continue;
}
TEST_INFO(" list of object:");
for (const auto itObj: obj) {
TEST_INFO(" " << itObj);
}
} else if (it.isArray() == true) {
ejson::Array array = it.toArray();
// check if the convertion in ejson::Array has been done corectly
if (array.exist() == false) {
continue;
}
TEST_INFO(" list of object:");
for (const auto itArray: array) {
TEST_INFO(" " << itArray);
}
} else if (it.isBoolean() == true) {
ejson::Boolean boolean = it.toBoolean();
// check if the convertion in ejson::Boolean has been done corectly
if (boolean.exist() == false) {
continue;
}
TEST_INFO(" boolean Value:" << boolean.get());
} else if (it.isString() == true) {
ejson::String str = it.toString();
// check if the convertion in ejson::String has been done corectly
if (str.exist() == false) {
continue;
}
TEST_INFO(" String Value:" << str.get());
} else if (it.isNumber() == true) {
ejson::Number num = it.toNumber();
// check if the convertion in ejson::Number has been done corectly
if (num.exist() == false) {
continue;
}
TEST_INFO(" Number Value:" << num.get());
}
}
for (size_t iii=0; iii<doc.size(); ++iii) {
switch (doc[iii].getType()) {
TEST_INFO(" Get an Object:" << doc.getKey(iii) );
break;
TEST_INFO(" Get an Array:" << doc.getKey(iii) );
break;
TEST_INFO(" Get an Boolean:" << doc.getKey(iii) );
break;
TEST_INFO(" Get an Null:" << doc.getKey(iii) );
break;
TEST_INFO(" Get an Number:" << doc.getKey(iii) );
break;
TEST_INFO(" Get an String:" << doc.getKey(iii) );
break;
default:
TEST_INFO(" Inknow element:" << doc.getKey(iii) );
break;
}
}
TEST_INFO(" Direct get node exml:");
ejson::Value element = doc["object C"];
if (element.exist() == false) {
TEST_ERROR("The element does not exist");
}
TEST_INFO("Read String:");
{
// you can simply change the type of the exml value if you know what it is:
ejson::String elem = doc["object A"].toString();
// Get the value:
std::string value = elem.get();
TEST_INFO(" String Value:" << value);
}
// or simply:
{
// Get the value:
std::string value = doc["object A"].toString().get();
TEST_INFO(" String Value:" << value);
}
TEST_INFO("Read Boolean:");
// Get a Boolean value:
{
// Get the value:
bool value = doc["object C"].toBoolean().get();
TEST_INFO(" Boolean Value:" << value);
}
TEST_INFO("Read Number:");
// Get a number value:
{
// Get the value:
double value = doc["object D"].toNumber().get();
TEST_INFO(" Number Value:" << value);
}
TEST_INFO("Read Array:");
{
ejson::Array array = doc["object E"].toArray();
// check if the convertion in ejson::Array has been done corectly
if (array.exist() == true) {
TEST_INFO(" list of Element in Array:");
for (const auto itArray: array) {
TEST_INFO(" " << itArray);
}
TEST_INFO(" list of Element in Array (C mode):");
for (size_t iii=0; iii<array.size(); ++iii) {
TEST_INFO(" " << array[iii]);
}
}
}
TEST_INFO("Read Object:");
{
ejson::Object obj = doc["object F"].toObject();
// check if the convertion in ejson::Object has been done corectly
if (obj.exist() == true) {
TEST_INFO(" list of Element in Object:");
for (const auto itObj: obj) {
TEST_INFO(" " << itObj);
}
TEST_INFO(" list of Element in Object (C mode):");
for (size_t iii=0; iii<obj.size(); ++iii) {
TEST_INFO(" " << obj[iii]);
}
}
}
TEST_INFO("Clone Object:");
// in the local system, All element get are not copied but referenced. the to have a copy of an object, you need to clone it :
{
// Clone object:
ejson::Value tmpClone = doc["object F"].clone();
ejson::Object obj = tmpClone.toObject();
// check if the clone and convertion in ejson::Object has been done corectly
if (obj.exist() == true) {
TEST_INFO(" list of Element in Object:");
for (const auto itObj: obj) {
TEST_INFO(" " << itObj);
}
}
}
}
void appl::read() {
readFromFile();
readFromString1();
readFromString1();
readFull();
}