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