BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
SniperJSON.h
Go to the documentation of this file.
1#include <string>
2#include <vector>
3#include <map>
4#include <sstream>
5#include <typeinfo>
6#include <exception>
7
9{
10public:
11 typedef std::vector<SniperJSON>::const_iterator vec_iterator;
12 typedef std::map<std::string, SniperJSON>::const_iterator map_iterator;
13
14 // default constructor
15 SniperJSON();
16
17 // construct from a json string
18 SniperJSON(const std::string &jstr);
19
20 // whether this is a scalar or string
21 bool isScalar() const { return m_type > 2; }
22
23 // whether this is a vector
24 bool isVector() const { return m_type == 2; }
25
26 // whether this is a map
27 bool isMap() const { return m_type == 1; }
28
29 /* get a C++ object from this element, T can be
30 * type[1]: C++ scalar and std::string
31 * type[2]: std::vector< type[1,2,3] >
32 * type[3]: std::map< type[1], type[1,2,3] >
33 */
34 template <typename T>
35 T get() const;
36
37 // push_back an element in case of a vector
38 bool push_back(const SniperJSON &var);
39
40 // insert a key/value pair in case of a map
41 bool insert(const std::string &key, const SniperJSON &val);
42
43 // clear all the contents of this element
44 void reset();
45
46 // get the array/vector or object/map size
47 int size() const;
48
49 // get the vector begin iterator
50 vec_iterator vec_begin() const { return m_jvec.begin(); }
51 // get the vector end iterator
52 vec_iterator vec_end() const { return m_jvec.end(); }
53
54 // get the object in vector via index
55 SniperJSON &operator[](int index) { return m_jvec.at(index); }
56 // get the object in vector via index
57 const SniperJSON &operator[](int index) const { return m_jvec.at(index); }
58
59 // get the map begin iterator
60 map_iterator map_begin() const { return m_jmap.begin(); }
61 // get the map end iterator
62 map_iterator map_end() const { return m_jmap.end(); }
63 // find the iterator via key
64 map_iterator find(const std::string &key) const { return m_jmap.find(key); }
65
66 // get the object in map via key
67 SniperJSON &operator[](const std::string &key);
68 // get the object in map via key
69 const SniperJSON &operator[](const std::string &key) const;
70
71 // load json from an input stream
72 static SniperJSON load(std::istream &is);
73
74 // load json from a string
75 static SniperJSON loads(const std::string &jstr);
76
77private:
78 typedef std::string::size_type StrCursor;
79
80 // 0:invalid, 1:object/map, 2:array/vector, 3:string, 9:other scalars
81 int m_type;
82 // scalar or std::string element holder
83 std::string m_jvar;
84 // array/vector element holder
85 std::vector<SniperJSON> m_jvec;
86 // object/map element holder
87 std::map<std::string, SniperJSON> m_jmap;
88
89 // control string for space charactors
90 static const std::string SPACES;
91 // control string for delimit charactors
92 static const std::string DELIMITS;
93
94 // construct a sub-element from a json string
95 SniperJSON(const std::string &jstr, StrCursor &cursor);
96
97 // initialize the json element from a string
98 void init(const std::string &jstr, StrCursor &cursor);
99
100 // get next valid charactor
101 char getValidChar(const std::string &jstr, StrCursor &cursor);
102
103 // read the content of a json object/map to m_jmap;
104 void readObjectMap(const std::string &jstr, StrCursor &cursor);
105
106 // read the content of a json array/vector to m_jvec
107 void readArrayVec(const std::string &jstr, StrCursor &cursor);
108
109 // read the content of a json string to m_jvar
110 void readStringStr(const std::string &jstr, StrCursor &cursor);
111
112 // read the content of other scalars to m_jvar
113 void readScalarStr(const std::string &jstr, StrCursor &cursor);
114
115 // function template helps to access type[1]
116 template <typename T>
117 inline void toCppVar(T &var) const;
118
119 // function template helps to access type[2]
120 template <typename T>
121 inline void toCppVar(std::vector<T> &var) const;
122
123 // function template helps to access type[3]
124 template <typename K, typename V>
125 inline void toCppVar(std::map<K, V> &var) const;
126
127 // an inner class for any exception while json parsing
128 class Exception : public std::exception
129 {
130 public:
131 Exception(const std::string &msg);
132 Exception(const std::string &jstr, int cursor);
133 virtual ~Exception() throw();
134 const char *what() const throw();
135
136 private:
137 std::string m_msg;
138 };
139};
140
141template <typename T>
142T SniperJSON::get() const
143{
144 T v;
145 toCppVar(v);
146 return v;
147}
148
149template <typename T>
150inline void SniperJSON::toCppVar(T &var) const
151{
152 if (m_type == 9)
153 {
154 std::stringstream ss;
155 ss << m_jvar;
156 ss >> var;
157
158 if (ss.eof())
159 {
160 return;
161 }
162 }
163
164 throw Exception(
165 std::string("cannot set <") + typeid(T).name() + "> with '" + m_jvar + "'");
166}
167
168template <>
169inline void SniperJSON::toCppVar<bool>(bool &var) const
170{
171 if (m_type == 9)
172 {
173 if (m_jvar == "true")
174 {
175 var = true;
176 return;
177 }
178 else if (m_jvar == "false")
179 {
180 var = false;
181 return;
182 }
183 }
184
185 throw Exception(std::string("cannot set <bool> with '") + m_jvar + "'");
186}
187
188template <>
189inline void SniperJSON::toCppVar<std::string>(std::string &var) const
190{
191 if (m_type == 3)
192 {
193 var = m_jvar.substr(1, m_jvar.size() - 2);
194 return;
195 }
196
197 throw Exception(std::string("cannot set <std::string> with '") + m_jvar + "'");
198}
199
200template <typename T>
201inline void SniperJSON::toCppVar(std::vector<T> &var) const
202{
203 if (m_type == 2)
204 {
205 for (vec_iterator it = m_jvec.begin(); it != m_jvec.end(); ++it)
206 {
207 var.push_back(it->get<T>());
208 }
209 return;
210 }
211
212 throw Exception(std::string("not a valid vector"));
213}
214
215//This implementaiton should be replaced by "std::is_same" when c++11 is valid
216namespace PreCXX11 {
217 struct true_value {
218 static bool value;
219 };
220
221 struct false_value {
222 static bool value;
223 };
224
225 template<typename, typename>
226 struct is_same : public false_value {};
227
228 template<typename T>
229 struct is_same<T, T> : public true_value {};
230}
231
232template <typename K, typename V>
233inline void SniperJSON::toCppVar(std::map<K, V> &var) const
234{
235 if (m_type == 1)
236 {
237 for (map_iterator it = m_jmap.begin(); it != m_jmap.end(); ++it)
238 {
239 var.insert(std::make_pair(
241 PreCXX11::is_same<K, std::string>::value ? it->first : it->first.substr(1, it->first.size() - 2))
242 .get<K>(),
243 it->second.get<V>()));
244 }
245 return;
246 }
247
248 throw Exception(std::string("not a valid map"));
249}
250
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition: KarLud.h:35
#define private
*************DOUBLE PRECISION m_pi *DOUBLE PRECISION m_HvecTau2 DOUBLE PRECISION m_HvClone2 DOUBLE PRECISION m_gamma1 DOUBLE PRECISION m_gamma2 DOUBLE PRECISION m_thet1 DOUBLE PRECISION m_thet2 INTEGER m_IFPHOT *COMMON c_Taupair $ !Spin Polarimeter vector first Tau $ !Spin Polarimeter vector second Tau $ !Clone Spin Polarimeter vector first Tau $ !Clone Spin Polarimeter vector second Tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning nd tau $ !Random Euler angle for cloning nd tau $ !Random Euler angle for cloning nd tau $ !phi of HvecTau1 $ !theta of HvecTau1 $ !phi of HvecTau2 $ !theta of HvecTau2 $ !super key
Definition: Taupair.h:42
bool insert(const std::string &key, const SniperJSON &val)
Definition: SniperJSON.cxx:44
T get() const
Definition: SniperJSON.h:142
bool isVector() const
Definition: SniperJSON.h:24
std::vector< SniperJSON >::const_iterator vec_iterator
Definition: SniperJSON.h:11
static SniperJSON loads(const std::string &jstr)
Definition: SniperJSON.cxx:96
void reset()
Definition: SniperJSON.cxx:56
map_iterator find(const std::string &key) const
Definition: SniperJSON.h:64
int size() const
Definition: SniperJSON.cxx:64
std::map< std::string, SniperJSON >::const_iterator map_iterator
Definition: SniperJSON.h:12
map_iterator map_begin() const
Definition: SniperJSON.h:60
bool push_back(const SniperJSON &var)
Definition: SniperJSON.cxx:33
static SniperJSON load(std::istream &is)
Definition: SniperJSON.cxx:88
SniperJSON & operator[](int index)
Definition: SniperJSON.h:55
vec_iterator vec_end() const
Definition: SniperJSON.h:52
map_iterator map_end() const
Definition: SniperJSON.h:62
bool isMap() const
Definition: SniperJSON.h:27
vec_iterator vec_begin() const
Definition: SniperJSON.h:50
const SniperJSON & operator[](int index) const
Definition: SniperJSON.h:57
bool isScalar() const
Definition: SniperJSON.h:21
static bool value
Definition: SniperJSON.h:218