Jpp  17.3.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSonSupportkit.hh
Go to the documentation of this file.
1 #ifndef __JDB_JSONSUPPORTKIT__
2 #define __JDB_JSONSUPPORTKIT__
3 
4 #include <sstream>
5 #include <vector>
6 #include <typeinfo>
7 #include <memory>
8 
9 #include "TDictionary.h"
10 
11 #include "JROOT/JRootClass.hh"
12 
13 #include "JLang/JException.hh"
14 #include "JLang/JTypeList.hh"
15 #include "JLang/JPrimitiveTypes.hh"
16 #include "JLang/JSingleton.hh"
17 
18 #include "JSon/JSon.hh"
19 
20 /**
21  * \author mdejong
22  */
23 namespace JDATABASE {}
24 namespace JPP { using namespace JDATABASE; }
25 
26 namespace JDATABASE {
27 
28  using JSON::json;
31  using JLANG::JException;
32  using JLANG::JSingleton;
33 
34  /**
35  * Assigment interface.
36  */
37  struct JSonIO {
38 
39  virtual ~JSonIO()
40  {}
41 
42  virtual void to_json(json& js, const void* p) const = 0;
43  virtual void from_json(const json& js, void* p) const = 0;
44  };
45 
46 
47  /**
48  * Assigment implementation.
49  */
50  template<class T>
51  struct JSonIO_t :
52  public JSonIO
53  {
54  virtual void to_json(json& js, const void* p) const override
55  {
56  js = * (const T*) p;
57  }
58 
59  virtual void from_json(const json& js, void* p) const override
60  {
61  * (T*) p = js;
62  }
63  };
64 
65 
66  /**
67  * Assignment.
68  */
69  struct JSonDictionary :
70  public std::map< std::string, std::shared_ptr<JSonIO> >,
71  public JSingleton<JSonDictionary>
72  {
73  /**
74  * Default constructor.
75  *
76  * Removed types:
77  * - <tt>long double</tt> (not supported by ROOT TDictionary)
78  * - <tt>char</tt> (not supported by JSon)
79  *
80  * Added STL type definitions:
81  * - <tt>std::string</tt>
82  * - <tt>size_t</tt>
83  * - <tt>std::size_t</tt>
84  */
86  {
87  using namespace JPP;
88 
89  for_each<JRemove<JPrimitiveTypes_t, JTypeList<char, long double> >::typelist>(*this);
90 
91  (*this)(JType<std::string>());
92  (*this)(JType<size_t>());
93  (*this)(JType<std::size_t>());
94  }
95 
96 
97  /**
98  * Get typename for given template class.
99  *
100  * This method uses the TDictionary class to get the name of the given class.
101  *
102  * \return type name
103  */
104  template<class T>
105  static const char* getTypename()
106  {
107  const TDictionary* pDictionary = TDictionary::GetDictionary(typeid(T));
108 
109  if (pDictionary != NULL)
110  return pDictionary->GetName();
111  else
112  THROW(JException, "Data type not implemented.");
113  }
114 
115  /**
116  * Add data type.
117  *
118  * \param type data type
119  */
120  template<class T>
121  void operator()(const JLANG::JType<T>& type)
122  {
123  (*this)[getTypename<T>()] = std::make_shared< JSonIO_t<T> >();
124  }
125  };
126 
127 
128  /**
129  * Auxiliary base class for JSon I/O based on ROOT dictionary.
130  */
131  template<class T>
132  struct JSonHelper {
133  /**
134  * Convert object to JSon.
135  *
136  * \param js json
137  * \param object object
138  */
139  friend inline void to_json(json& js, const T& object)
140  {
141  using namespace std;
142  using namespace JPP;
143 
144  JRootWritableClass cls(object);
145 
146  for (unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) {
147 
148  if (!JRootClass::is_static(*p)) {
149 
150  JRootWritableClass abc = cls.get(*p);
151 
152  JSonDictionary::getInstance()[abc.getTypename()]->to_json(js[p->GetName()], abc.getAddress());
153  }
154  }
155  }
156 
157 
158  /**
159  * Convert JSon to object/.
160  *
161  * \param js json
162  * \param object object
163  */
164  friend inline void from_json(const json& js, T& object)
165  {
166  using namespace std;
167  using namespace JPP;
168 
169  JRootReadableClass cls(object);
170 
171  for (unique_ptr<TIterator> i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) {
172 
173  if (!JRootClass::is_static(*p)) {
174 
175  JRootReadableClass abc = cls.get(*p);
176 
177  JSonDictionary::getInstance()[abc.getTypename()]->from_json(js[p->GetName()], abc.getAddress());
178  }
179  }
180  }
181  };
182 }
183 
184 #endif
static const char * getTypename()
Get typename for given template class.
virtual void to_json(json &js, const void *p) const override
General exception.
Definition: JException.hh:23
Exceptions.
Simple singleton class.
Definition: JSingleton.hh:18
ROOT class for reading object.
Definition: JRootClass.hh:533
JSonDictionary()
Default constructor.
friend void to_json(json &js, const T &object)
Convert object to JSon.
virtual void from_json(const json &js, void *p) const override
friend void from_json(const json &js, T &object)
Convert JSon to object/.
const char * getTypename() const
Get type name.
Definition: JRootClass.hh:196
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
Auxiliary class for a type holder.
Definition: JType.hh:19
Assigment implementation.
virtual void from_json(const json &js, void *p) const =0
Auxiliary base class for JSon I/O based on ROOT dictionary.
Assigment interface.
pointer_type getAddress() const
Get address.
Definition: JRootClass.hh:451
do set_variable OUTPUT_DIRECTORY $WORKDIR T
void operator()(const JLANG::JType< T > &type)
Add data type.
JRootAddressableClass get(const TDataMember &data_member) const
Get addressable class of given data member.
Definition: JRootClass.hh:486
Type list of primitive data types.
TClass * getClass() const
Get class.
Definition: JRootClass.hh:185
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
nlohmann::json json
virtual void to_json(json &js, const void *p) const =0
static bool is_static(const TDataMember &data_member)
Check if data member is static.
Definition: JRootClass.hh:128
ROOT class for writing object.
Definition: JRootClass.hh:593