-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSON.h
400 lines (301 loc) · 9.22 KB
/
JSON.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#ifndef __JASONS_JSON
#define __JASONS_JSON
#include <unordered_map>
#include <deque>
#include <vector>
#include <string>
#include <algorithm>
#define JSON_VERSION "0.1"
namespace json
{
class JSON
{
public:
typedef enum {
JSON_BOOL, // bool
JSON_NUMBER, // double
JSON_STRING, // std::string
JSON_ARRAY, // std::deque
JSON_MAP, // std::unordered_map
JSON_NONE // NULL
} Type;
// Only use a typedef for the more complicated
// types (i.e. Array and Map). There is no need to replace std::string
// with String (for now).
typedef std::deque<JSON> Array;
typedef std::unordered_map<std::string, JSON> Map;
private:
Type value_type;
void* value_ptr;
void assign(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
Type get_type(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
std::pair<void*, Type> parse(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
void* parse_none(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
void* parse_bool(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
void* parse_number(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
void* parse_string(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
void* parse_array(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
void* parse_map(const std::string::const_iterator &m_begin,
const std::string::const_iterator &m_end);
double string_to_number(const std::string &m_str) const;
void duplicate(const JSON& m_rhs)
{
clear();
value_type = m_rhs.value_type;
switch(value_type){
case JSON_BOOL:
value_ptr = new bool;
if(value_ptr == NULL){
throw __FILE__ ":JSON::duplicate: Unable to allocate bool";
}
*( (bool*) value_ptr ) = *( (bool*)m_rhs.value_ptr );
break;
case JSON_NUMBER:
value_ptr = new double;
if(value_ptr == NULL){
throw __FILE__ ":JSON::duplicate: Unable to allocate double";
}
*( (double*) value_ptr ) = *( (double*) m_rhs.value_ptr );
break;
case JSON_STRING:
value_ptr = new std::string;
if(value_ptr == NULL){
throw __FILE__ ":JSON::duplicate: Unable to allocate string";
}
*( (std::string*) value_ptr ) = *( (std::string*) m_rhs.value_ptr );
break;
case JSON_ARRAY:
value_ptr = new std::deque<JSON>;
if(value_ptr == NULL){
throw __FILE__ ":JSON::duplicate: Unable to allocate array";
}
*( (Array*) value_ptr ) = *( (Array*)m_rhs.value_ptr );
break;
case JSON_MAP:
value_ptr = new std::unordered_map<std::string, JSON>;
if(value_ptr == NULL){
throw __FILE__ ":JSON::duplicate: Unable to allocate map";
}
*( (Map*) value_ptr ) =
*( (Map*)m_rhs.value_ptr );
break;
case JSON_NONE:
// Do nothing
break;
default:
throw __FILE__ ":JSON::duplicate: Unknown type";
};
};
public:
JSON() :
value_type(JSON_NONE), value_ptr(NULL)
{
};
JSON(const std::string &m_rhs) :
value_type(JSON_NONE), value_ptr(NULL)
{
assign( m_rhs.begin(), m_rhs.end() );
};
JSON(const JSON& m_rhs) :
value_type(JSON_NONE), value_ptr(NULL)
{
duplicate(m_rhs);
};
~JSON()
{
clear();
};
JSON& operator=(const std::string &m_rhs)
{
assign( m_rhs.begin(), m_rhs.end() );
return *this;
};
JSON& operator=(const JSON &m_rhs)
{
JSON tmp;
// Copy before clearing, since we may be copying from ourselves!
tmp.duplicate(m_rhs);
clear();
// Move the memory in tmp to the current object
value_type = tmp.value_type;
value_ptr = tmp.value_ptr;
// Make tmp safe to delete
tmp.value_type = JSON_NONE;
tmp.value_ptr = NULL;
return *this;
};
void clear()
{
switch(value_type){
case JSON_BOOL:
if(value_ptr == NULL){
throw __FILE__ ":JSON: Attempting to delete NULL bool ptr";
};
delete ( (bool*) value_ptr );
break;
case JSON_NUMBER:
if(value_ptr == NULL){
throw __FILE__ ":JSON: Attempting to delete NULL number ptr";
};
delete ( (double*) value_ptr );
break;
case JSON_STRING:
if(value_ptr == NULL){
throw __FILE__ ":JSON: Attempting to delete NULL string ptr";
};
delete ( (std::string*) value_ptr );
break;
case JSON_ARRAY:
if(value_ptr == NULL){
throw __FILE__ ":JSON: Attempting to delete NULL array ptr";
};
delete ( (Array*) value_ptr );
break;
case JSON_MAP:
if(value_ptr == NULL){
throw __FILE__ ":JSON: Attempting to delete NULL map ptr";
};
delete ( (Map*) value_ptr );
break;
case JSON_NONE:
// Do nothing
break;
default:
throw __FILE__ ":JSON: Unknown type";
};
value_ptr = NULL;
value_type = JSON_NONE;
};
inline Type get_type() const
{
return value_type;
};
inline size_t size() const
{
if(value_ptr == NULL){
throw __FILE__ ":JSON::size: NULL value_ptr";
}
if(value_type == JSON_MAP){
return ( (Map*)value_ptr )->size();
}
if(value_type == JSON_ARRAY){
return ( (Array*)value_ptr )->size();
}
throw __FILE__ ":JSON::size: Illegal call to non-map/non-array object";
return 0;
};
inline bool get_bool(bool m_force_conversion = false) const
{
if(value_ptr == NULL){
throw __FILE__ ":JSON::get_bool: NULL value_ptr";
}
if(value_type == JSON_BOOL){
return *( (bool*)value_ptr );
}
// If the user wants to take a chance, we can convert number
// to bool
if(m_force_conversion){
if(value_type == JSON_NUMBER){
return ( *( (double*)value_ptr ) != 0 );
}
}
throw __FILE__ ":JSON::get_bool: Non-bool value_ptr!";
return false;
};
inline double get_number(bool m_force_conversion = false) const
{
if(value_ptr == NULL){
throw __FILE__ ":JSON::get_number: NULL value_ptr";
}
if(value_type == JSON_NUMBER){
return *( (double*)value_ptr );
}
// If the user wants to take a chance, we can convert string and bool
// to number
if(m_force_conversion){
if(value_type == JSON_BOOL){
return double( *( (bool*)value_ptr ) );
}
if(value_type == JSON_STRING){
return string_to_number( *( (std::string*)value_ptr ) );
}
}
throw __FILE__ ":JSON::get_number: Non-number value_ptr!";
return 0.0;
};
inline std::string get_string() const
{
if(value_type == JSON_STRING){
if(value_ptr == NULL){
throw __FILE__ ":JSON::get_string: NULL value_ptr";
}
return *( (std::string*)value_ptr );
}
throw __FILE__ ":JSON::get_string: Non-string value_ptr!";
return std::string();
};
// Test for map element existance
inline bool has_key(const std::string &m_key) const
{
if(value_type == JSON_MAP){
Map::const_iterator iter = ( (Map*)value_ptr )->find(m_key);
return ( iter != ( (Map*)value_ptr )->end() );
}
throw __FILE__ ":has_key: Attempted to dereference non-map object";
return false; // Keep the compiler happy
};
inline std::vector<std::string> keys() const
{
if(value_ptr == NULL){
throw __FILE__ ":JSON::keys: NULL value_ptr";
}
if(value_type == JSON_MAP){
std::deque<std::string> local;
for(Map::const_iterator i = ( (Map*)value_ptr )->begin();
i != ( (Map*)value_ptr )->end();++i){
local.push_back(i->first);
}
// Each Map key is unique, so there is no need to
// use std::unique on the extracted keys
std::sort( local.begin(), local.end() );
return std::vector<std::string>( local.begin(), local.end() );
}
throw __FILE__ ":JSON::keys: Illegal call to non-map object";
return std::vector<std::string>();
};
// Access a map element
const JSON& operator[](const std::string &m_key) const
{
if(value_type == JSON_MAP){
Map::const_iterator iter = ( (Map*)value_ptr )->find(m_key);
if( iter == ( (Map*)value_ptr )->end() ){
throw __FILE__ ":JSON[map]: Did not find requested key in map";
}
return iter->second;
}
throw __FILE__ ":JSON[map]: Attempted to dereference non-map object";
return *this; // Keep the compiler happy
};
// Access an array element
const JSON& operator[](const size_t &m_index) const
{
if(value_type == JSON_ARRAY){
if( m_index >= ( (Array*)value_ptr )->size() ){
throw __FILE__ ":JSON[array]: Index out of bounds";
}
return (* ( (Array*)value_ptr ) )[m_index];
}
throw __FILE__ ":JSON[array]: Attempted to dereference non-array object";
return *this; // Keep the compiler happy
};
};
}
#endif // __JASONS_JSON