-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonxx.h
529 lines (462 loc) · 12.5 KB
/
jsonxx.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// -*- mode: c++; c-basic-offset: 4; -*-
// Author: Hong Jiang <hong@hjiang.net>
// Contributors:
// Sean Middleditch <sean@middleditch.us>
// rlyeh <https://github.com/r-lyeh>
#pragma once
#include <cstddef>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <sstream>
// jsonxx versioning: major.minor-extra where
// major = { number }
// minor = { number }
// extra = { 'a':alpha, 'b':beta, 'rc': release candidate, 'r': release, 's':stable }
#define JSONXX_MAJOR "0"
#define JSONXX_MINOR "22"
#define JSONXX_EXTRA "a"
#define JSONXX_VERSION JSONXX_MAJOR "." JSONXX_MINOR "-" JSONXX_EXTRA
#define JSONXX_XML_TAG "<!-- generated by jsonxx " JSONXX_VERSION " -->"
#if __cplusplus > 199711L
#define JSONXX_COMPILER_HAS_CXX11 1
#elif defined(_MSC_VER) && _MSC_VER > 1700
#define JSONXX_COMPILER_HAS_CXX11 1
#else
#define JSONXX_COMPILER_HAS_CXX11 0
#endif
#ifdef _MSC_VER
// disable the C4127 warning if using VC, see http://stackoverflow.com/a/12042515
#define JSONXX_ASSERT(...) \
do { \
__pragma(warning(push)) __pragma(warning(disable:4127)) \
if( jsonxx::Assertions ) \
__pragma(warning(pop)) \
jsonxx::assertion(__FILE__,__LINE__,#__VA_ARGS__,bool(__VA_ARGS__)); \
__pragma(warning(push)) __pragma(warning(disable:4127)) \
} while(0) \
__pragma(warning(pop))
#else
#define JSONXX_ASSERT(...) do { if( jsonxx::Assertions ) \
jsonxx::assertion(__FILE__,__LINE__,#__VA_ARGS__,bool(__VA_ARGS__)); } while(0)
#endif
namespace jsonxx {
// FIXME(hjiang): Those should really be dynamic.
// Settings
enum Settings {
// constants
Enabled = true,
Disabled = false,
Permissive = true,
Strict = false,
// values
Parser = Strict, // permissive or strict parsing
UnquotedKeys = Disabled, // support of unquoted keys
Assertions = Disabled // enabled or disabled assertions (these asserts work both in DEBUG and RELEASE builds)
};
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4127)
#endif
inline bool parser_is_strict() { return Parser == Strict; }
inline bool parser_is_permissive() { return Parser == Permissive; }
inline bool unquoted_keys_are_enabled() { return UnquotedKeys == Enabled; }
#ifdef _MSC_VER
#pragma warning(pop)
#endif
// Constants for .write() and .xml() methods
enum Format {
JSON = 0, // JSON output
JSONx = 1, // XML output, JSONx format. see http://goo.gl/I3cxs
JXML = 2, // XML output, JXML format. see https://github.com/r-lyeh/JXML
JXMLex = 3, // XML output, JXMLex format. see https://github.com/r-lyeh/JXMLex
TaggedXML = 4 // XML output, tagged XML format. see https://github.com/hjiang/jsonxx/issues/12
};
// Types
typedef long double Number;
typedef bool Boolean;
typedef std::string String;
struct Null {};
class Value;
class Object;
class Array;
// Identity meta-function
template <typename T>
struct identity {
typedef T type;
};
// Tools
bool validate( const std::string &input );
bool validate( std::istream &input );
std::string reformat( const std::string &input );
std::string reformat( std::istream &input );
std::string xml( const std::string &input, unsigned format = JSONx );
std::string xml( std::istream &input, unsigned format = JSONx );
// Detail
void assertion( const char *file, int line, const char *expression, bool result );
// A JSON Object
class Object {
public:
Object();
~Object();
template <typename T>
bool has(const std::string& key) const;
// Always call has<>() first. If the key doesn't exist, consider
// the behavior undefined.
template <typename T>
T& get(const std::string& key);
template <typename T>
const T& get(const std::string& key) const;
template <typename T>
const T& get(const std::string& key, const typename identity<T>::type& default_value) const;
size_t size() const;
bool empty() const;
const std::map<std::string, Value*>& kv_map() const;
std::string json() const;
std::string xml( unsigned format = JSONx, const std::string &header = std::string(), const std::string &attrib = std::string() ) const;
std::string write( unsigned format ) const;
void reset();
bool parse(std::istream &input);
bool parse(const std::string &input);
typedef std::map<std::string, Value*> container;
void import( const Object &other );
void import( const std::string &key, const Value &value );
Object &operator<<(const Value &value);
Object &operator<<(const Object &value);
Object &operator=(const Object &value);
Object(const Object &other);
Object(const std::string &key, const Value &value);
template<size_t N>
Object(const char (&key)[N], const Value &value) {
import(key,value);
}
template<typename T>
Object &operator<<(const T &value);
protected:
static bool parse(std::istream& input, Object& object);
container value_map_;
std::string odd;
};
class Array {
public:
Array();
~Array();
size_t size() const;
bool empty() const;
template <typename T>
bool has(unsigned int i) const;
template <typename T>
T& get(unsigned int i);
template <typename T>
const T& get(unsigned int i) const;
template <typename T>
const T& get(unsigned int i, const typename identity<T>::type& default_value) const;
const std::vector<Value*>& values() const {
return values_;
}
std::string json() const;
std::string xml( unsigned format = JSONx, const std::string &header = std::string(), const std::string &attrib = std::string() ) const;
std::string write( unsigned format ) const { return format == JSON ? json() : xml(format); }
void reset();
bool parse(std::istream &input);
bool parse(const std::string &input);
typedef std::vector<Value*> container;
void append(const Array &other);
void append(const Value &value) { import(value); }
void import(const Array &other);
void import(const Value &value);
Array &operator<<(const Array &other);
Array &operator<<(const Value &value);
Array &operator=(const Array &other);
Array &operator=(const Value &value);
Array(const Array &other);
Array(const Value &value);
protected:
static bool parse(std::istream& input, Array& array);
container values_;
};
// A value could be a number, an array, a string, an object, a
// boolean, or null
class Value {
public:
Value();
~Value() { reset(); }
void reset();
template<typename T>
void import( const T & ) {
reset();
type_ = INVALID_;
// debug
// std::cout << "[WARN] No support for " << typeid(t).name() << std::endl;
}
void import( const bool &b ) {
reset();
type_ = BOOL_;
bool_value_ = b;
}
#define $number(TYPE) \
void import( const TYPE &n ) { \
reset(); \
type_ = NUMBER_; \
number_value_ = static_cast<long double>(n); \
}
$number( char )
$number( int )
$number( long )
$number( long long )
$number( unsigned char )
$number( unsigned int )
$number( unsigned long )
$number( unsigned long long )
$number( float )
$number( double )
$number( long double )
#undef $number
#if JSONXX_COMPILER_HAS_CXX11 > 0
void import( const std::nullptr_t & ) {
reset();
type_ = NULL_;
}
#endif
void import( const Null & ) {
reset();
type_ = NULL_;
}
void import( const String &s ) {
reset();
type_ = STRING_;
*( string_value_ = new String() ) = s;
}
void import( const Array &a ) {
reset();
type_ = ARRAY_;
*( array_value_ = new Array() ) = a;
}
void import( const Object &o ) {
reset();
type_ = OBJECT_;
*( object_value_ = new Object() ) = o;
}
void import( const Value &other ) {
if (this != &other)
switch (other.type_) {
case NULL_:
import( Null() );
break;
case BOOL_:
import( other.bool_value_ );
break;
case NUMBER_:
import( other.number_value_ );
break;
case STRING_:
import( *other.string_value_ );
break;
case ARRAY_:
import( *other.array_value_ );
break;
case OBJECT_:
import( *other.object_value_ );
break;
case INVALID_:
type_ = INVALID_;
break;
default:
JSONXX_ASSERT( !"not implemented" );
}
}
template<typename T>
Value &operator <<( const T &t ) {
import(t);
return *this;
}
template<typename T>
Value &operator =( const T &t ) {
reset();
import(t);
return *this;
}
Value(const Value &other);
template<typename T>
Value( const T&t ) : type_(INVALID_) { import(t); }
template<size_t N>
Value( const char (&t)[N] ) : type_(INVALID_) { import( std::string(t) ); }
bool parse(std::istream &input);
bool parse(const std::string &input);
template<typename T>
bool is() const;
template<typename T>
T& get();
template<typename T>
const T& get() const;
bool empty() const;
public:
enum {
NUMBER_,
STRING_,
BOOL_,
NULL_,
ARRAY_,
OBJECT_,
INVALID_
} type_;
union {
Number number_value_;
String* string_value_;
Boolean bool_value_;
Array* array_value_;
Object* object_value_;
};
protected:
static bool parse(std::istream& input, Value& value);
};
template <typename T>
bool Array::has(unsigned int i) const {
if (i >= size()) {
return false;
} else {
Value* v = values_.at(i);
return v->is<T>();
}
}
template <typename T>
T& Array::get(unsigned int i) {
JSONXX_ASSERT(i < size());
Value* v = values_.at(i);
return v->get<T>();
}
template <typename T>
const T& Array::get(unsigned int i) const {
JSONXX_ASSERT(i < size());
const Value* v = values_.at(i);
return v->get<T>();
}
template <typename T>
const T& Array::get(unsigned int i, const typename identity<T>::type& default_value) const {
if(has<T>(i)) {
const Value* v = values_.at(i);
return v->get<T>();
} else {
return default_value;
}
}
template <typename T>
bool Object::has(const std::string& key) const {
container::const_iterator it(value_map_.find(key));
return it != value_map_.end() && it->second->is<T>();
}
template <typename T>
T& Object::get(const std::string& key) {
JSONXX_ASSERT(has<T>(key));
return value_map_.find(key)->second->get<T>();
}
template <typename T>
const T& Object::get(const std::string& key) const {
JSONXX_ASSERT(has<T>(key));
return value_map_.find(key)->second->get<T>();
}
template <typename T>
const T& Object::get(const std::string& key, const typename identity<T>::type& default_value) const {
if (has<T>(key)) {
return value_map_.find(key)->second->get<T>();
} else {
return default_value;
}
}
template<>
inline bool Value::is<Value>() const {
return true;
}
template<>
inline bool Value::is<Null>() const {
return type_ == NULL_;
}
template<>
inline bool Value::is<Boolean>() const {
return type_ == BOOL_;
}
template<>
inline bool Value::is<String>() const {
return type_ == STRING_;
}
template<>
inline bool Value::is<Number>() const {
return type_ == NUMBER_;
}
template<>
inline bool Value::is<Array>() const {
return type_ == ARRAY_;
}
template<>
inline bool Value::is<Object>() const {
return type_ == OBJECT_;
}
template<>
inline Value& Value::get<Value>() {
return *this;
}
template<>
inline const Value& Value::get<Value>() const {
return *this;
}
template<>
inline bool& Value::get<Boolean>() {
JSONXX_ASSERT(is<Boolean>());
return bool_value_;
}
template<>
inline std::string& Value::get<String>() {
JSONXX_ASSERT(is<String>());
return *string_value_;
}
template<>
inline Number& Value::get<Number>() {
JSONXX_ASSERT(is<Number>());
return number_value_;
}
template<>
inline Array& Value::get<Array>() {
JSONXX_ASSERT(is<Array>());
return *array_value_;
}
template<>
inline Object& Value::get<Object>() {
JSONXX_ASSERT(is<Object>());
return *object_value_;
}
template<>
inline const Boolean& Value::get<Boolean>() const {
JSONXX_ASSERT(is<Boolean>());
return bool_value_;
}
template<>
inline const String& Value::get<String>() const {
JSONXX_ASSERT(is<String>());
return *string_value_;
}
template<>
inline const Number& Value::get<Number>() const {
JSONXX_ASSERT(is<Number>());
return number_value_;
}
template<>
inline const Array& Value::get<Array>() const {
JSONXX_ASSERT(is<Array>());
return *array_value_;
}
template<>
inline const Object& Value::get<Object>() const {
JSONXX_ASSERT(is<Object>());
return *object_value_;
}
template<typename T>
inline Object &Object::operator<<(const T &value) {
*this << Value(value);
return *this;
}
} // namespace jsonxx
std::ostream& operator<<(std::ostream& stream, const jsonxx::Value& v);
std::ostream& operator<<(std::ostream& stream, const jsonxx::Object& v);
std::ostream& operator<<(std::ostream& stream, const jsonxx::Array& v);