The first thing to do when reading or writing a JSON file/stream, is to declare the Document interface
Include ejson
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:
Access to a string in an Object:
The generic Way to access to an element is to convert it in the corect type:
Commonly you might use like this:
Access to a boolean in an Object:
Access to a Number in an Object:
Access to a Array in an Object:
Get the Array
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
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:
All example file
#include <test-debug/debug.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:");
}
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:");
}
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:");
}
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:");
TEST_INFO("list of Object:");
for (const auto it: doc) {
TEST_INFO(" " << it);
if (it.isObject() == true) {
if (obj.
exist() ==
false) {
continue;
}
TEST_INFO(" list of object:");
for (const auto itObj: obj) {
TEST_INFO(" " << itObj);
}
} else if (it.isArray() == true) {
if (array.
exist() ==
false) {
continue;
}
TEST_INFO(" list of object:");
for (const auto itArray: array) {
TEST_INFO(" " << itArray);
}
} else if (it.isBoolean() == true) {
if (boolean.exist() == false) {
continue;
}
TEST_INFO(" boolean Value:" << boolean.get());
} else if (it.isString() == true) {
if (str.
exist() ==
false) {
continue;
}
TEST_INFO(
" String Value:" << str.
get());
} else if (it.isNumber() == true) {
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:");
if (element.
exist() ==
false) {
TEST_ERROR("The element does not exist");
}
TEST_INFO("Read String:");
{
std::string value = elem.
get();
TEST_INFO(" String Value:" << value);
}
{
std::string value = doc["object A"].toString().get();
TEST_INFO(" String Value:" << value);
}
TEST_INFO("Read Boolean:");
{
bool value = doc["object C"].toBoolean().get();
TEST_INFO(" Boolean Value:" << value);
}
TEST_INFO("Read Number:");
{
double value = doc["object D"].toNumber().get();
TEST_INFO(" Number Value:" << value);
}
TEST_INFO("Read Array:");
{
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:");
{
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:");
{
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();
}