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