Write an JSON content
Table of Contents
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
ejson::Document doc;
Write an JSON file
Write an json tree is done like:
Write an JSON Stream
Writing a stream is done like this:
std::string streamOut;
bool retGenerate = doc.generate(streamOut);
Operation on Tree
Add String:
Add Null:
Add Number:
Add Boolean:
Add Array with values:
Add Object with values:
Remove a Value in an Object:
doc.remove("F");
Remove a Value in an Object:
array.remove(2);
Object concept
The ejson concept is to abstract the implementation of the internal system. All the element are mapped on shared memory. Then if you asign an element to an other, it is the same. You need to clone it if you want to have new standalone element.
All example file
#include <test-debug/debug.hpp>
#include <ejson/ejson.hpp>
#include "write.hpp"
static void writeToFile() {
ejson::Document doc;
TEST_INFO("store");
TEST_INFO("parse ret = " << retGenerate);
TEST_INFO("Debug display of the tree:");
doc.display();
}
static void writeToString() {
ejson::Document doc;
TEST_INFO("generate");
std::string streamOut;
bool retGenerate = doc.generate(streamOut);
TEST_INFO("parse ret = " << retGenerate);
TEST_INFO("Debug display of the tree:");
doc.display();
}
static void writeAll() {
ejson::Document doc;
ejson::Array array;
array.add(ejson::Number(22));
array.add(ejson::Null());
doc.add("E", array);
ejson::Object object;
// remove the object named "F"
doc.remove("F");
// Remove element 2 in the array
array.remove(2);
doc.display();
}
void appl::write() {
writeToFile();
writeToString();
writeAll();
}