XMM - Probabilistic Models for Motion Recognition and Mapping

xmmJson.hpp
Go to the documentation of this file.
1 /*
2  * xmmJson.hpp
3  *
4  * Set of utility functions for JSON I/O
5  *
6  * Contact:
7  * - Jules Francoise <jules.francoise@ircam.fr>
8  *
9  * This code has been initially authored by Jules Francoise
10  * <http://julesfrancoise.com> during his PhD thesis, supervised by Frederic
11  * Bevilacqua <href="http://frederic-bevilacqua.net>, in the Sound Music
12  * Movement Interaction team <http://ismm.ircam.fr> of the
13  * STMS Lab - IRCAM, CNRS, UPMC (2011-2015).
14  *
15  * Copyright (C) 2015 UPMC, Ircam-Centre Pompidou.
16  *
17  * This File is part of XMM.
18  *
19  * XMM is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * XMM is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with XMM. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 #ifndef xmmJson_h
34 #define xmmJson_h
35 
36 #include "../../../dependencies/jsoncpp/include/json.h"
37 #include <fstream>
38 #include <vector>
39 
40 namespace xmm {
50 class Writable {
51  public:
52  virtual ~Writable() {}
53 
55 
61  virtual Json::Value toJson() const = 0;
62 
68  virtual void fromJson(Json::Value const& root) = 0;
69 
71 
72 #ifdef SWIGPYTHON
73 
74 
81  void writeFile(char* fileName) const {
82  std::ofstream outStream;
83  outStream.open(fileName);
84  outStream << this->toJson();
85  outStream.close();
86  }
87 
93  void readFile(char* fileName) {
94  std::string jsonstring;
95  std::ifstream inStream;
96  inStream.open(fileName);
97  // inStream.seekg(0, std::ios::end);
98  // jsonstring.reserve(inStream.tellg());
99  // inStream.seekg(0, std::ios::beg);
100  //
101  // jsonstring.assign((std::istreambuf_iterator<char>(inStream)),
102  // std::istreambuf_iterator<char>());
103  Json::Value root;
104  Json::Reader reader;
105  if (reader.parse(inStream, root)) {
106  this->fromJson(root);
107  } else {
108  throw std::runtime_error("Cannot Parse Json File");
109  }
110  inStream.close();
111  }
112 
117  std::string __str__() const { return this->toJson().toStyledString(); }
118 
120 #endif
121 };
122 
127 class JsonException : public std::exception {
128  public:
132  enum class JsonErrorType {
136  JsonMissingNode,
137 
141  JsonTypeError,
142 
146  JsonValueError
147  };
148 
154  JsonException(JsonErrorType errorType, std::string nodename = "")
155  : errorType_(errorType) {
156  nodename_.push_back(nodename);
157  }
158 
164  explicit JsonException(JsonException const& src, std::string nodename)
165  : errorType_(src.errorType_), nodename_(src.nodename_) {
166  nodename_.push_back(nodename);
167  }
168 
174  : errorType_(src.errorType_), nodename_(src.nodename_) {}
175 
181  if (this != &src) {
182  errorType_ = src.errorType_;
183  nodename_ = src.nodename_;
184  }
185  return *this;
186  }
187 
192  virtual const char* what() const throw() {
193  std::string message;
194  switch (errorType_) {
195  case JsonErrorType::JsonMissingNode:
196  message = "Json Structure Error: Missing Node";
197  break;
198 
199  case JsonErrorType::JsonTypeError:
200  message = "Json Structure Error: Type Error";
201  break;
202 
203  case JsonErrorType::JsonValueError:
204  message = "Json Structure Error: Value Error";
205  break;
206 
207  default:
208  message = "Json unknown error type";
209  break;
210  }
211  message += " (root";
212  for (auto& node : nodename_) message += " > " + node;
213  message += ")";
214  return message.c_str();
215  }
216 
217  private:
222 
226  std::vector<std::string> nodename_;
227 };
228 
236 template <typename T>
237 Json::Value array2json(T const* a, unsigned int n) {
238  Json::Value root;
239  root.resize(static_cast<Json::ArrayIndex>(n));
240  for (int i = 0; i < n; i++) {
241  root[i] = a[i];
242  }
243  return root;
244 }
245 
253 template <typename T>
254 void json2array(Json::Value const& root, T* a, unsigned int n) {
255  if (!root.isArray())
257  if (root.size() != n)
259  unsigned int i = 0;
260  for (auto it : root) {
261  a[i++] = it.asInt();
262  }
263 }
264 
265 template <>
266 void json2array(Json::Value const& root, float* a, unsigned int n);
267 template <>
268 void json2array(Json::Value const& root, double* a, unsigned int n);
269 template <>
270 void json2array(Json::Value const& root, bool* a, unsigned int n);
271 template <>
272 void json2array(Json::Value const& root, std::string* a, unsigned int n);
273 
280 template <typename T>
281 Json::Value vector2json(std::vector<T> const& a) {
282  Json::Value root;
283  root.resize(static_cast<Json::ArrayIndex>(a.size()));
284  for (int i = 0; i < a.size(); i++) {
285  root[i] = a[i];
286  }
287  return root;
288 }
289 
298 template <typename T>
299 void json2vector(Json::Value const& root, std::vector<T>& a, unsigned int n) {
300  if (!root.isArray())
302  if (root.size() != n)
304  unsigned int i = 0;
305  for (auto it : root) {
306  a[i++] = it.asInt();
307  }
308 }
309 
310 template <>
311 void json2vector(Json::Value const& root, std::vector<float>& a,
312  unsigned int n);
313 template <>
314 void json2vector(Json::Value const& root, std::vector<double>& a,
315  unsigned int n);
316 template <>
317 void json2vector(Json::Value const& root, std::vector<bool>& a, unsigned int n);
318 template <>
319 void json2vector(Json::Value const& root, std::vector<std::string>& a,
320  unsigned int n);
321 }
322 
323 #endif
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 (&#39;write&#39; 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 (&#39;read&#39; 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