-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_value.h
424 lines (353 loc) · 9.87 KB
/
json_value.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (C) 2012 Anders Dalle Henning Dalvander
//
// Use, modification and distribution are subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(ADHD_JSON_VALUE_H)
#define ADHD_JSON_VALUE_H
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/static_assert.hpp>
#include <stdexcept>
#include <iosfwd>
#include <string>
#include <vector>
#include <map>
#include <assert.h>
#if !defined(ADHD_JSON_API)
# ifdef _WIN32
# if defined(ADHD_JSON_EXPORT)
# define ADHD_JSON_API __declspec(dllexport)
# elif defined(ADHD_JSON_IMPORT)
# define ADHD_JSON_API __declspec(dllimport)
# else
# define ADHD_JSON_API
# endif
# else
# define ADHD_JSON_API
# endif
#endif
namespace adhd
{
/// Exception thrown by json_parser (see json_parser.h) if parsing fails.
class ADHD_JSON_API json_parse_exception : public std::runtime_error
{
public:
explicit json_parse_exception(const char* message)
: std::runtime_error(message)
{
}
virtual ~json_parse_exception();
};
/// Represents a JSON value of type null.
struct json_null
{
};
/// Represents a JSON value of type string.
struct json_string
{
explicit json_string(const std::string& val) : val(val) {}
std::string val;
};
/// Represents a JSON value of type number.
struct json_number
{
explicit json_number(double val) : val(val) {}
double val;
};
/// Represents a JSON value of type bool.
struct json_bool
{
explicit json_bool(bool b) : val(b) {}
bool val;
};
/// Represents a JSON value of type array.
struct json_array
{
};
/// Represents a JSON value of type object.
struct json_object
{
};
/// JavaScript Object Notation (JSON) is a lightweight, text-based,
/// language-independent data interchange format. It was derived from
/// the ECMAScript Programming Language Standard. JSON defines a small
/// set of formatting rules for the portable representation of structured
/// data.
///
/// JSON can represent four primitive types (strings, numbers, booleans,
/// and null) and two structured types (objects and arrays).
///
/// Where:
/// - An object is an unordered collection of zero or more name/value
/// pairs, where a name is a string and a value is a string, number,
/// boolean, null, object, or array.
///
/// - An array is an ordered sequence of zero or more values.
///
/// See:
/// * http://www.json.org/
/// * http://www.ietf.org/rfc/rfc4627
///
/// This class could be seen as the result of the NIH syndrome. But when
/// searching the net an existing library wasn't found which had an
/// acceptable license model, acceptable number of bugs, acceptable
/// performance, acceptable code bloat and acceptable required features.
class ADHD_JSON_API json_value
{
public:
json_value()
: vt(variant_type_null)
, vd()
{
}
json_value(const json_null& /*val*/)
: vt(variant_type_null)
, vd()
{
}
json_value(const json_string& val)
: vt(variant_type_string)
, vd(val.val)
{
}
explicit json_value(const std::string& val)
: vt(variant_type_string)
, vd(val)
{
}
json_value(const json_number& val)
: vt(variant_type_number)
, vd(val.val)
{
}
explicit json_value(double val)
: vt(variant_type_number)
, vd(val)
{
}
template <class T>
explicit json_value(T, typename boost::enable_if< boost::is_same<T, bool> >::type* = 0)
{
// prevent direct initialization from bool, use the json_value(json_bool) constructor instead.
BOOST_STATIC_ASSERT(false);
}
json_value(const json_bool& val)
: vt(variant_type_bool)
, vd(val)
{
}
json_value(const json_array& val)
: vt(variant_type_array)
, vd(val)
{
}
json_value(const json_object& val)
: vt(variant_type_object)
, vd(val)
{
}
json_value(const json_value& rhs);
json_value& operator=(json_value rhs)
{
swap(rhs);
return *this;
}
~json_value();
void swap(json_value& rhs)
{
std::swap(vt, rhs.vt);
std::swap(vd, rhs.vd);
}
/// Recursively visits the visitor.
template <typename TVisitor>
void accept(TVisitor& visitor) const
{
switch (vt)
{
case variant_type_null:
visitor.null_value();
break;
case variant_type_string:
visitor.string_value(*vd.s);
break;
case variant_type_number:
visitor.number_value(vd.n);
break;
case variant_type_bool:
visitor.bool_value(vd.b);
break;
case variant_type_array:
visitor.begin_array();
for (variant_data::a_type::const_iterator i = vd.a->begin(), e = vd.a->end(); i != e; ++i)
{
visitor.begin_value();
i->accept(visitor);
visitor.end_value();
}
visitor.end_array();
break;
case variant_type_object:
visitor.begin_object();
for (variant_data::o_type::const_iterator i = vd.o->begin(), e = vd.o->end(); i != e; ++i)
{
visitor.begin_key();
visitor.string_value(i->first);
visitor.end_key();
visitor.begin_value();
i->second.accept(visitor);
visitor.end_value();
}
visitor.end_object();
break;
default:
break;
}
}
bool operator==(const json_value& rhs) const;
bool operator!=(const json_value& rhs) const
{
return !(*this == rhs);
}
bool operator<(const json_value& rhs) const;
bool operator>(const json_value& rhs) const
{
return rhs < *this;
}
bool operator<=(const json_value& rhs) const
{
return !(rhs > *this);
}
bool operator>=(const json_value& rhs) const
{
return !(*this < rhs);
}
typedef const std::string& (json_value::*unspecified_bool_type)() const;
operator unspecified_bool_type() const
{
return is_null() ? 0 : &json_value::get_string;
}
bool operator!() const
{
return is_null();
}
bool is_null() const
{
return vt == variant_type_null;
}
bool is_string() const
{
return vt == variant_type_string;
}
const std::string& get_string() const
{
assert(is_string());
return is_string() ? *vd.s : empty_string;
}
bool is_number() const
{
return vt == variant_type_number;
}
double get_number() const
{
assert(is_number());
return is_number() ? vd.n : 0;
}
bool is_bool() const
{
return vt == variant_type_bool;
}
bool get_bool() const
{
assert(is_bool());
return is_bool() ? vd.b : false;
}
bool is_array() const
{
return vt == variant_type_array;
}
const json_value& get_child(size_t i) const;
json_value& put_child(size_t i);
json_value& append_child()
{
return put_child(get_length());
}
size_t get_length() const
{
return is_array() ? vd.a->size() : 0;
}
void set_length(size_t length);
bool is_object() const
{
return vt == variant_type_object;
}
const json_value& get_child(const std::string& name) const;
json_value& put_child(const std::string& name);
bool has_child(const std::string& name) const;
bool erase_child(const std::string& name);
std::string to_pretty_string(size_t indent_size = 4) const;
std::ostream& pretty_print(std::ostream& os, size_t indent_size = 4) const;
std::string to_string() const;
/// Predicate to check if a character should be escaped.
struct need_escaping
{
bool operator()(char c) const
{
return (c >= 0 && c < 0x20) || c == 0x7f || c == '\\' || c == '\"';
}
};
/// Public static for representing a null JSON value.
static const json_value null;
private:
static const std::string empty_string;
enum variant_type
{
variant_type_null,
variant_type_string,
variant_type_number,
variant_type_bool,
variant_type_array,
variant_type_object,
};
union variant_data
{
variant_data()
: v()
{
}
variant_data(const std::string& val)
: s(new std::string(val))
{
}
variant_data(double val)
: n(val)
{
}
variant_data(const json_bool& val)
: b(val.val)
{
}
variant_data(const json_array& /*val*/)
: a(new a_type())
{
}
variant_data(const json_object& /*val*/)
: o(new o_type())
{
}
typedef std::vector<json_value> a_type;
typedef std::map<std::string, json_value> o_type;
void* v;
std::string* s;
double n;
bool b;
a_type* a;
o_type* o;
};
variant_type vt;
variant_data vd;
};
/// Outputs a JSON value to a stream in a compact way.
ADHD_JSON_API std::ostream& operator<<(std::ostream& os, const json_value& rhs);
}
#endif