36 #include "../../../dependencies/jsoncpp/include/json.h" 61 virtual Json::Value
toJson()
const = 0;
68 virtual void fromJson(Json::Value
const& root) = 0;
82 std::ofstream outStream;
83 outStream.open(fileName);
84 outStream << this->
toJson();
94 std::string jsonstring;
95 std::ifstream inStream;
96 inStream.open(fileName);
105 if (reader.parse(inStream, root)) {
108 throw std::runtime_error(
"Cannot Parse Json File");
155 : errorType_(errorType) {
156 nodename_.push_back(nodename);
165 : errorType_(src.errorType_), nodename_(src.nodename_) {
166 nodename_.push_back(nodename);
174 : errorType_(src.errorType_), nodename_(src.nodename_) {}
192 virtual const char*
what()
const throw() {
194 switch (errorType_) {
195 case JsonErrorType::JsonMissingNode:
196 message =
"Json Structure Error: Missing Node";
199 case JsonErrorType::JsonTypeError:
200 message =
"Json Structure Error: Type Error";
203 case JsonErrorType::JsonValueError:
204 message =
"Json Structure Error: Value Error";
208 message =
"Json unknown error type";
212 for (
auto& node : nodename_) message +=
" > " + node;
214 return message.c_str();
236 template <
typename T>
239 root.resize(static_cast<Json::ArrayIndex>(n));
240 for (
int i = 0; i < n; i++) {
253 template <
typename T>
254 void json2array(Json::Value
const& root, T* a,
unsigned int n) {
257 if (root.size() != n)
260 for (
auto it : root) {
266 void json2array(Json::Value
const& root,
float* a,
unsigned int n);
268 void json2array(Json::Value
const& root,
double* a,
unsigned int n);
270 void json2array(Json::Value
const& root,
bool* a,
unsigned int n);
272 void json2array(Json::Value
const& root, std::string* a,
unsigned int n);
280 template <
typename T>
283 root.resize(static_cast<Json::ArrayIndex>(a.size()));
284 for (
int i = 0; i < a.size(); i++) {
298 template <
typename T>
299 void json2vector(Json::Value
const& root, std::vector<T>& a,
unsigned int n) {
302 if (root.size() != n)
305 for (
auto it : root) {
311 void json2vector(Json::Value
const& root, std::vector<float>& a,
314 void json2vector(Json::Value
const& root, std::vector<double>& a,
317 void json2vector(Json::Value
const& root, std::vector<bool>& a,
unsigned int n);
319 void json2vector(Json::Value
const& root, std::vector<std::string>& a,
void json2array(Json::Value const &root, T *a, unsigned int n)
Reads a C-style array from a Json Value.
Definition: xmmJson.hpp:254
virtual ~Writable()
Definition: xmmJson.hpp:52
JsonErrorType errorType_
Type of Json Parsing Error.
Definition: xmmJson.hpp:221
std::string __str__() const
"print" method for python => returns the results of write method
Definition: xmmJson.hpp:117
Json::Value vector2json(std::vector< T > const &a)
Writes a vector to a Json Value.
Definition: xmmJson.hpp:281
void json2vector(Json::Value const &root, std::vector< T > &a, unsigned int n)
Reads a vector from a Json Value.
Definition: xmmJson.hpp:299
The current node has an inadmissible value.
virtual Json::Value toJson() const =0
Write the object to a JSON Structure.
JsonException & operator=(JsonException const &src)
Assigment.
Definition: xmmJson.hpp:180
JsonException(JsonException const &src)
Copy Constructor.
Definition: xmmJson.hpp:173
Abstract class for handling JSON + File I/O.
Definition: xmmJson.hpp:50
virtual const char * what() const
Get exception message.
Definition: xmmJson.hpp:192
std::vector< std::string > nodename_
Name of the Json Node presenting an error.
Definition: xmmJson.hpp:226
Exception class for handling JSON parsing errors.
Definition: xmmJson.hpp:127
void writeFile(char *fileName) const
write method for python wrapping ('write' keyword forbidden, name has to be different) ...
Definition: xmmJson.hpp:81
Definition: xmmAttribute.hpp:42
JsonException(JsonErrorType errorType, std::string nodename="")
Default Constructor.
Definition: xmmJson.hpp:154
JsonException(JsonException const &src, std::string nodename)
Constructor From exception message.
Definition: xmmJson.hpp:164
The current node has wrong data type.
void readFile(char *fileName)
read method for python wrapping ('read' keyword forbidden, name has to be different) ...
Definition: xmmJson.hpp:93
virtual void fromJson(Json::Value const &root)=0
Read the object from a JSON Structure.
Json::Value array2json(T const *a, unsigned int n)
Writes a C-style array to a Json Value.
Definition: xmmJson.hpp:237
JsonErrorType
Type of Json parsing errors.
Definition: xmmJson.hpp:132