BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
SniperJSON.cxx
Go to the documentation of this file.
2
5
6const std::string SniperJSON::SPACES(" \n\t\r");
7const std::string SniperJSON::DELIMITS(", \n]}\t\r");
8
10 : m_type(0)
11{
12}
13
14SniperJSON::SniperJSON(const std::string &jstr)
15 : m_type(0)
16{
17 StrCursor cursor = 0;
18 init(jstr, cursor);
19
20 cursor = jstr.find_first_not_of(SniperJSON::SPACES, cursor);
21 if (cursor != std::string::npos)
22 {
23 throw Exception(jstr, cursor);
24 }
25}
26
27SniperJSON::SniperJSON(const std::string &jstr, StrCursor &cursor)
28 : m_type(0)
29{
30 init(jstr, cursor);
31}
32
34{
35 if (m_type == 2 || m_type == 0)
36 {
37 m_jvec.push_back(var);
38 m_type = 2;
39 return true;
40 }
41 return false;
42}
43
44bool SniperJSON::insert(const std::string &key, const SniperJSON &val)
45{
46 if (m_type == 1 || m_type == 0)
47 {
48 std::string _key = '"' + key + '"';
49 m_jmap.insert(std::make_pair(_key, val));
50 m_type = 1;
51 return true;
52 }
53 return false;
54}
55
57{
58 m_type = 0;
59 m_jvar.clear();
60 m_jvec.clear();
61 m_jmap.clear();
62}
63
65{
66 if (m_type == 1)
67 {
68 return m_jmap.size();
69 }
70 else if (m_type == 2)
71 {
72 return m_jvec.size();
73 }
74
75 return -1;
76}
77
79{
80 return m_jmap.at('"' + key + '"');
81}
82
83const SniperJSON &SniperJSON::operator[](const std::string &key) const
84{
85 return m_jmap.at('"' + key + '"');
86}
87
89{
90 std::ostringstream oss;
91 oss << is.rdbuf();
92
93 return loads(oss.str());
94}
95
96SniperJSON SniperJSON::loads(const std::string &jstr)
97{
98 return SniperJSON(jstr);
99}
100
101void SniperJSON::init(const std::string &jstr, StrCursor &cursor)
102{
103 switch (getValidChar(jstr, cursor))
104 {
105 case '{': //object
106 readObjectMap(jstr, cursor);
107 m_type = 1;
108 break;
109 case '[': //array
110 readArrayVec(jstr, cursor);
111 m_type = 2;
112 break;
113 case '"': //string
114 readStringStr(jstr, cursor);
115 m_type = 3;
116 break;
117 default: //number, true, false or null
118 readScalarStr(jstr, cursor);
119 m_type = 9;
120 }
121}
122
123char SniperJSON::getValidChar(const std::string &jstr, StrCursor &cursor)
124{
125 cursor = jstr.find_first_not_of(SPACES, cursor);
126 if (cursor != std::string::npos)
127 {
128 return jstr.at(cursor);
129 }
130 throw Exception(jstr, cursor);
131}
132
133void SniperJSON::readObjectMap(const std::string &jstr, StrCursor &cursor)
134{
135 bool status = true;
136
137 if (getValidChar(jstr, ++cursor) != '}')
138 {
139 --cursor; //reset the cursor just before the first key
140 do
141 {
142 readStringStr(jstr, ++cursor);
143 const std::string &key = m_jvar;
144 if (key.size() == 2 || getValidChar(jstr, cursor) != ':')
145 {
146 status = false;
147 break;
148 }
149 m_jmap.insert(std::make_pair(key, SniperJSON(jstr, ++cursor)));
150 } while (getValidChar(jstr, cursor) == ',');
151 }
152
153 m_jvar.clear();
154
155 if (status && getValidChar(jstr, cursor) == '}')
156 {
157 ++cursor;
158 return;
159 }
160
161 throw Exception(jstr, cursor);
162}
163
164void SniperJSON::readArrayVec(const std::string &jstr, StrCursor &cursor)
165{
166 if (getValidChar(jstr, ++cursor) != ']')
167 {
168 --cursor; //reset the cursor just before the first variable
169 do
170 {
171 m_jvec.push_back(SniperJSON(jstr, ++cursor));
172 } while (getValidChar(jstr, cursor) == ',');
173 }
174
175 if (getValidChar(jstr, cursor) == ']')
176 {
177 ++cursor;
178 return;
179 }
180
181 throw Exception(jstr, cursor);
182}
183
184void SniperJSON::readStringStr(const std::string &jstr, StrCursor &cursor)
185{
186 if (getValidChar(jstr, cursor) == '"')
187 {
188 StrCursor pstart = cursor;
189 cursor = jstr.find('"', pstart + 1);
190 if (cursor != std::string::npos)
191 {
192 ++cursor;
193 m_jvar = jstr.substr(pstart, cursor - pstart);
194 return;
195 }
196 }
197
198 throw Exception(jstr, cursor);
199}
200
201void SniperJSON::readScalarStr(const std::string &jstr, StrCursor &cursor)
202{
203 StrCursor pstart = cursor;
204 cursor = jstr.find_first_of(DELIMITS, pstart + 1);
205 if (cursor != std::string::npos || pstart == 0)
206 {
207 m_jvar = jstr.substr(pstart, cursor - pstart);
208 return;
209 }
210
211 throw Exception(jstr, cursor);
212}
213
214SniperJSON::Exception::Exception(const std::string &msg)
215 : m_msg("json error: ")
216{
217 m_msg += msg;
218}
219
220SniperJSON::Exception::Exception(const std::string &jstr, int cursor)
221 : m_msg("invalid json:\n")
222{
223 m_msg += jstr.substr(0, cursor + 1);
224 m_msg += " <<< parsing error";
225}
226
227SniperJSON::Exception::~Exception() throw()
228{
229}
230
231const char *SniperJSON::Exception::what() const throw()
232{
233 return m_msg.c_str();
234}
235
236
*************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
static SniperJSON loads(const std::string &jstr)
Definition: SniperJSON.cxx:96
void reset()
Definition: SniperJSON.cxx:56
int size() const
Definition: SniperJSON.cxx:64
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
static bool value
Definition: SniperJSON.h:218