This example demonstrates how to use the basic functionality of SG++ JSON API.
The goal of this API is to facilitate generation and parsing of complex configuration files. Also it permits easy but limited object serialization functionality.
int main(
int argc,
char **argv) {
try {
int main()
Definition densityMultiplication.cpp:22
We first create an empty JSON object
Then we introduce the following JSON structure:
{
parent:{
"t1":"v1";
"t2":"v2";
"list1":["tv1",96.0,"tv2"]
};
"textAttr1":"text1";
"numVal1":36.0
}
As you can see in the code below, methods to append new entries support chaining for convenience.
Node & addDictAttr(const std::string &name) override
Definition DictNode.cpp:287
Node & addTextAttr(const std::string &name, const std::string &value) override
Definition DictNode.cpp:232
virtual Node & addTextAttr(const std::string &name, const std::string &value)
Definition Node.cpp:81
virtual Node & addIdValue(const std::string &value)
Definition Node.cpp:197
virtual Node & addIDAttr(const std::string &name, const std::string &value)
Definition Node.cpp:86
virtual Node & addListAttr(const std::string &name)
Definition Node.cpp:121
virtual Node & addTextValue(const std::string &value)
Definition Node.cpp:192
You can conveniently get and set values inside the JSON object using Key/Value notation.
This call gets the string representation of the second entry (96.0
) in list1
of dictionary parent
.
std::cout <<
"value: " << configuration[
"parent"][
"list1"][1].
get() << std::endl;
virtual std::string & get()
Definition Node.cpp:30
This call sets the second entry (96.0
) in list1
of dictionary parent
to the double value 7
and then prints its string representation.
configuration[
"parent"][
"list1"][1].
setDouble(7);
std::cout <<
"value: " << configuration[
"parent"][
"list1"][1].
get() << std::endl;
virtual void setDouble(double doubleValue)
Definition Node.cpp:42
You can also try to obtain a typed representation of an entry. Conversion is performed automatically and may result in an exception if it can't be converted.
std::cout <<
"value: " << configuration[
"parent"][
"list1"][1].
getDouble() << std::endl;
virtual double getDouble()
Definition Node.cpp:38
Next we demonstrate erasing values.
Here we erase numVal1
from the top level of the configuration
object.
configuration[
"numVal1"].
erase();
std::unique_ptr< Node > erase(Node &node) override
Definition DictNode.cpp:415
Now we erase the dictionary named parent
from the the configuration
object. Note that the object that is erased is returned to us by the erase operation. All Objects inside a JSON object are represented internally as an object of type Node
"
std::unique_ptr<json::Node> parentNode = configuration[
"parent"].
erase();
virtual void addAttribute(const std::string &name, std::unique_ptr< Node > node)
Definition Node.cpp:68
Serialization (i.e. writing of a standard compatible json file) is also possible.
void serialize(const std::string &outFileName)
Definition JSON.cpp:191
We can also read a JSON file by passing it's path to the constructor of a JSON object
std::cout << e.
what() << std::endl;
}
return 0;
}
Definition json_exception.hpp:15
const char * what() const noexcept override
Definition json_exception.cpp:29