Jpp  debug
the software that should make you happy
JSonSupportkit.hh
Go to the documentation of this file.
1 #ifndef __JDB_JSONSUPPORTKIT__
2 #define __JDB_JSONSUPPORTKIT__
3 
4 #ifndef __ROOTCLING__
5 
6 #include <sstream>
7 #include <vector>
8 #include <typeinfo>
9 #include <memory>
10 
11 #include "TDictionary.h"
12 
13 #include "JROOT/JRootClass.hh"
14 
15 #include "JLang/JException.hh"
16 #include "JLang/JTypeList.hh"
17 #include "JLang/JPrimitiveTypes.hh"
18 #include "JLang/JSingleton.hh"
19 
20 #include "JSon/JSon.hh"
21 
22 #include "JDB/JDBString.hh"
23 
24 /**
25  * \author mdejong
26  */
27 namespace JDATABASE {}
28 namespace JPP { using namespace JDATABASE; }
29 
30 namespace JDATABASE {
31 
32  using JSON::json;
35  using JLANG::JException;
37  using JLANG::JSingleton;
38 
39 
40  /**
41  * Convert database string to JSon.
42  *
43  * \param js json
44  * \param object database string
45  */
46  inline void to_json(json& js, const JDBString& object)
47  {
48  js = json(static_cast<const std::string&>(object));
49  }
50 
51 
52  /**
53  * Convert JSon to database string.
54  *
55  * \param js json
56  * \param object database string
57  */
58  inline void from_json(const json& js, JDBString& object)
59  {
60  static_cast<std::string&>(object) = js.get<std::string>();
61  }
62 
63 
64  /**
65  * Assigment interface.
66  */
67  struct JSonIO {
68 
69  virtual ~JSonIO()
70  {}
71 
72  virtual void to_json(json& js, const void* p) const = 0;
73  virtual void from_json(const json& js, void* p) const = 0;
74  };
75 
76 
77  /**
78  * Assigment implementation.
79  */
80  template<class T>
81  struct JSonIO_t :
82  public JSonIO
83  {
84  virtual void to_json(json& js, const void* p) const override
85  {
86  js = * (const T*) p;
87  }
88 
89  virtual void from_json(const json& js, void* p) const override
90  {
91  * (T*) p = js;
92  }
93  };
94 
95 
96  /**
97  * Assignment.
98  */
99  struct JSonDictionary :
100  public std::map< std::string, std::shared_ptr<JSonIO> >,
101  public JSingleton<JSonDictionary>
102  {
103  /**
104  * Default constructor.
105  *
106  * Removed types:
107  * - <tt>long double</tt> (not supported by ROOT TDictionary)
108  * - <tt>char</tt> (not supported by JSon)
109  *
110  * Added STL type definitions:
111  * - <tt>std::string</tt>
112  * - <tt>size_t</tt>
113  * - <tt>std::size_t</tt>
114  * - <tt>JDATABASE::JDBString</tt>
115  */
117  {
118  using namespace JPP;
119 
120  for_each<JRemove<JPrimitiveTypes_t, JTypeList<char, long double> >::typelist>(*this);
121 
122  (*this)(JType<std::string>());
123  (*this)(JType<size_t>());
124  (*this)(JType<std::size_t>());
125  (*this)(JType<JDATABASE::JDBString>());
126  }
127 
128 
129  /**
130  * Get typename for given template class.
131  *
132  * This method uses the TDictionary class to get the name of the given class.
133  *
134  * \return type name
135  */
136  template<class T>
137  static const char* getTypename()
138  {
139  const TDictionary* pDictionary = TDictionary::GetDictionary(typeid(T));
140 
141  if (pDictionary != NULL)
142  return pDictionary->GetName();
143  else
144  THROW(JException, "Data type not implemented.");
145  }
146 
147  /**
148  * Add data type.
149  *
150  * \param type data type
151  */
152  template<class T>
153  void operator()(const JLANG::JType<T>& type)
154  {
155  (*this)[getTypename<T>()] = std::make_shared< JSonIO_t<T> >();
156  }
157  };
158 
159 
160  /**
161  * Auxiliary base class for JSon I/O based on ROOT dictionary.
162  */
163  template<class T>
164  struct JSonHelper {
165  /**
166  * Convert object to JSon.
167  *
168  * \param js json
169  * \param object object
170  */
171  friend inline void to_json(json& js, const T& object)
172  {
173  using namespace std;
174  using namespace JPP;
175 
176  JRootWritableClass cls(object);
177 
178  for (unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) {
179 
180  if (!JRootClass::is_static(*p)) {
181 
182  JRootWritableClass abc = cls.get(*p);
183 
184  JSonDictionary::getInstance()[abc.getTypename()]->to_json(js[p->GetName()], abc.getAddress());
185  }
186  }
187  }
188 
189 
190  /**
191  * Convert JSon to object/.
192  *
193  * \param js json
194  * \param object object
195  */
196  friend inline void from_json(const json& js, T& object)
197  {
198  using namespace std;
199  using namespace JPP;
200 
201  JRootReadableClass cls(object);
202 
203  for (unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) {
204 
205  if (!JRootClass::is_static(*p)) {
206 
207  JRootReadableClass abc = cls.get(*p);
208 
209  if (js.contains(p->GetName()))
210  JSonDictionary::getInstance()[abc.getTypename()]->from_json(js[p->GetName()], abc.getAddress());
211  else
212  THROW(JParserException, "No key word <" << p->GetName() << "> in " << js);
213  }
214  }
215  }
216  };
217 }
218 
219 #else
220 
221 namespace JDATABASE {}
222 namespace JPP { using namespace JDATABASE; }
223 
224 namespace JDATABASE {
225 
226  template<class T>
227  struct JSonHelper {};
228 }
229 #endif
230 
231 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Type list of primitive data types.
nlohmann::json json
General exception.
Definition: JException.hh:24
Exception when parsing a value.
Definition: JException.hh:558
Auxiliary classes and methods for database I/O.
Definition: JAHRS.hh:14
void from_json(const json &js, JDBString &object)
Convert JSon to database string.
void to_json(json &js, const JDBString &object)
Convert database string to JSon.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Wrapper class to read string until end-of-line.
Definition: JDBString.hh:22
void operator()(const JLANG::JType< T > &type)
Add data type.
JSonDictionary()
Default constructor.
static const char * getTypename()
Get typename for given template class.
Auxiliary base class for JSon I/O based on ROOT dictionary.
friend void to_json(json &js, const T &object)
Convert object to JSon.
friend void from_json(const json &js, T &object)
Convert JSon to object/.
Assigment implementation.
virtual void to_json(json &js, const void *p) const override
virtual void from_json(const json &js, void *p) const override
Assigment interface.
virtual void to_json(json &js, const void *p) const =0
virtual void from_json(const json &js, void *p) const =0
Simple singleton class.
Definition: JSingleton.hh:18
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
Auxiliary class for a type holder.
Definition: JType.hh:19
pointer_type getAddress() const
Get address.
Definition: JRootClass.hh:460
JRootAddressableClass get(const TDataMember &data_member) const
Get addressable class of given data member.
Definition: JRootClass.hh:495
static bool is_static(const TDataMember &data_member)
Check if data member is static.
Definition: JRootClass.hh:128
TClass * getClass() const
Get class.
Definition: JRootClass.hh:194
const char * getTypename() const
Get type name.
Definition: JRootClass.hh:205
ROOT class for reading object.
Definition: JRootClass.hh:544
ROOT class for writing object.
Definition: JRootClass.hh:604