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