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
530
531
532
533
534
535
|
#ifndef JSON_H
#define JSON_H
#include <string>
#include <vector>
#include <map>
#include <deque>
#include <stack>
#if __GNUC__ >= 3
# define maybe_expect(expr,value) __builtin_expect ((expr),(value))
# define is_const(expr) __builtin_constant_p ((expr))
#else
# define maybe_expect(expr,value) (expr)
# define is_const(expr) 0
#endif
#define expect_false(expr) maybe_expect ((expr) != 0, 0)
#define expect_true(expr) maybe_expect ((expr) != 0, 1)
static void unreachable_internal (char const *file, int line, char const *function);
static void throw_unless_internal (char const *file, int line, char const *function, char const *condition);
static void throw_msg_unless_internal (char const *file, int line, char const *function, char const *message);
#define throw_unreachable unreachable_internal (__FILE__, __LINE__, __PRETTY_FUNCTION__)
#define throw_unless(condition) if (!expect_false (condition)) throw_unless_internal (__FILE__, __LINE__, __PRETTY_FUNCTION__, #condition)
#define throw_msg_unless(condition, message) if (!expect_false (condition)) throw_msg_unless_internal (__FILE__, __LINE__, __PRETTY_FUNCTION__, message)
namespace json
{
class ValueIterator;
class ValueConstIterator;
enum ValueType
{
nullValue = 0,
intValue,
uintValue,
realValue,
stringValue,
booleanValue,
arrayValue,
objectValue
};
class StaticString
{
public:
explicit StaticString (char const *czstring)
: str_ (czstring)
{
}
operator char const *() const
{
return str_;
}
char const *c_str() const
{
return str_;
}
private:
char const *str_;
};
class Value
{
friend class ValueIteratorBase;
public:
typedef std::vector<std::string> Members;
typedef ValueIterator iterator;
typedef ValueConstIterator const_iterator;
typedef unsigned ArrayIndex;
static const Value null;
static const int minInt;
static const int maxInt;
static const unsigned maxUInt;
private:
class CZString
{
public:
enum DuplicationPolicy
{
noDuplication = 0,
duplicate,
duplicateOnCopy
};
CZString (int index);
CZString (char const *cstr, DuplicationPolicy allocate);
CZString (const CZString &other);
~CZString ();
CZString &operator = (const CZString &other);
bool operator < (const CZString &other) const;
bool operator == (const CZString &other) const;
int index () const;
char const *c_str () const;
bool isStaticString () const;
private:
void swap (CZString &other);
char const *cstr_;
int index_;
};
public:
typedef std::map<CZString, Value> ObjectValues;
public:
Value (ValueType type = nullValue);
Value (int value);
Value (unsigned value);
Value (double value);
Value (char const *value);
Value (const StaticString &value);
Value (const std::string &value);
Value (bool value);
Value (const Value &other);
~Value ();
Value &operator = (const Value &other);
void swap (Value &other);
ValueType type () const;
bool operator < (const Value &other) const;
bool operator <= (const Value &other) const;
bool operator >= (const Value &other) const;
bool operator > (const Value &other) const;
bool operator == (const Value &other) const;
bool operator != (const Value &other) const;
operator char const * () const;
operator std::string () const;
operator int () const;
operator unsigned () const;
operator double () const;
operator bool () const;
bool isNull () const;
bool isBool () const;
bool isInt () const;
bool isUInt () const;
bool isIntegral () const;
bool isDouble () const;
bool isNumeric () const;
bool isString () const;
bool isArray () const;
bool isObject () const;
bool isConvertibleTo (ValueType other) const;
unsigned size () const;
bool empty () const;
bool operator ! () const;
void clear ();
void resize (unsigned size);
Value &operator [] (int index);
Value &operator [] (unsigned index);
const Value &operator [] (int index) const;
const Value &operator [] (unsigned index) const;
Value get (int index, const Value &defaultValue) const;
Value get (unsigned index, const Value &defaultValue) const;
bool isValidIndex (int index) const;
bool isValidIndex (unsigned index) const;
Value &append (const Value &value);
Value &operator [] (char const *key);
const Value &operator [] (char const *key) const;
Value &operator [] (const std::string &key);
const Value &operator [] (const std::string &key) const;
Value &operator [] (const StaticString &key);
Value get (char const *key, const Value &defaultValue) const;
Value get (const std::string &key, const Value &defaultValue) const;
Value removeMember (char const* key);
Value removeMember (const std::string &key);
bool isMember (char const *key) const;
bool isMember (const std::string &key) const;
Members getMemberNames () const;
const_iterator begin () const;
const_iterator end () const;
iterator begin ();
iterator end ();
private:
Value &resolveReference (char const *key, bool isStatic);
struct MemberNamesTransform
{
typedef char const *result_type;
char const *operator () (const CZString &name) const
{
return name.c_str ();
}
};
union ValueHolder
{
int int_;
unsigned uint_;
double real_;
bool bool_;
char *string_;
ObjectValues *map_;
} value_;
ValueType type_ : 8;
int allocated_ : 1;
};
class ValueAllocator
{
public:
enum { unknown = (unsigned)-1 };
virtual ~ValueAllocator ();
virtual char *makeMemberName (char const *memberName) = 0;
virtual void releaseMemberName (char *memberName) = 0;
virtual char *duplicateStringValue (char const *value,
unsigned length = unknown) = 0;
virtual void releaseStringValue (char *value) = 0;
};
class ValueIteratorBase
{
public:
typedef unsigned size_t;
typedef int difference_type;
typedef ValueIteratorBase SelfType;
ValueIteratorBase ();
explicit ValueIteratorBase (const Value::ObjectValues::iterator ¤t);
bool operator == (const SelfType &other) const
{
return isEqual (other);
}
bool operator != (const SelfType &other) const
{
return !isEqual (other);
}
difference_type operator - (const SelfType &other) const
{
return computeDistance (other);
}
Value key () const;
unsigned index () const;
char const *memberName () const;
protected:
Value &deref () const;
void increment ();
void decrement ();
difference_type computeDistance (const SelfType &other) const;
bool isEqual (const SelfType &other) const;
void copy (const SelfType &other);
private:
Value::ObjectValues::iterator current_;
};
class ValueConstIterator : public ValueIteratorBase
{
friend class Value;
public:
typedef unsigned size_t;
typedef int difference_type;
typedef const Value &reference;
typedef const Value *pointer;
typedef ValueConstIterator SelfType;
ValueConstIterator ();
private:
explicit ValueConstIterator (const Value::ObjectValues::iterator ¤t);
public:
SelfType &operator = (const ValueIteratorBase &other);
SelfType operator ++ (int)
{
SelfType temp (*this);
++*this;
return temp;
}
SelfType operator -- (int)
{
SelfType temp (*this);
--*this;
return temp;
}
SelfType &operator -- ()
{
decrement ();
return *this;
}
SelfType &operator ++ ()
{
increment ();
return *this;
}
reference operator * () const
{
return deref ();
}
};
class ValueIterator : public ValueIteratorBase
{
friend class Value;
public:
typedef unsigned size_t;
typedef int difference_type;
typedef Value &reference;
typedef Value *pointer;
typedef ValueIterator SelfType;
ValueIterator ();
ValueIterator (const ValueConstIterator &other);
ValueIterator (const ValueIterator &other);
private:
explicit ValueIterator (const Value::ObjectValues::iterator ¤t);
public:
SelfType &operator = (const SelfType &other);
SelfType operator ++ (int)
{
SelfType temp (*this);
++*this;
return temp;
}
SelfType operator -- (int)
{
SelfType temp (*this);
--*this;
return temp;
}
SelfType &operator -- ()
{
decrement ();
return *this;
}
SelfType &operator ++ ()
{
increment ();
return *this;
}
reference operator * () const
{
return deref ();
}
};
} // namespace json
namespace json
{
class Value;
class Reader
{
public:
typedef char const *Location;
Reader ();
bool parse (const std::string &document, Value &root);
bool parse (char const *beginDoc, char const *endDOc, Value &root);
bool parse (std::istream &, Value &root);
std::string error_msgs () const;
private:
enum TokenType
{
tokenEndOfStream = 0,
tokenObjectBegin,
tokenObjectEnd,
tokenArrayBegin,
tokenArrayEnd,
tokenString,
tokenNumber,
tokenTrue,
tokenFalse,
tokenNull,
tokenArraySeparator,
tokenMemberSeparator,
tokenComment,
tokenError
};
class Token
{
public:
TokenType type_;
Location start_;
Location end_;
};
class ErrorInfo
{
public:
Token token_;
std::string message_;
Location extra_;
};
typedef std::deque<ErrorInfo> Errors;
bool expectToken (TokenType type, Token &token, char const *message);
bool readToken (Token &token);
void skipSpaces ();
bool match (Location pattern,
int patternLength);
bool readString ();
void readNumber ();
bool readValue ();
bool readObject ();
bool readArray ();
bool decodeNumber (Token &token);
bool decodeString (Token &token);
bool decodeString (Token &token, std::string &decoded);
bool decodeDouble (Token &token);
bool decodeUnicodeEscapeSequence (Token &token,
Location ¤t,
Location end,
unsigned &unicode);
bool addError (const std::string &message,
Token &token,
Location extra = 0);
bool recoverFromError (TokenType skipUntilToken);
bool addErrorAndRecover (const std::string &message,
Token &token,
TokenType skipUntilToken);
void skipUntilSpace ();
Value ¤tValue ();
char getNextChar ();
void getLocationLineAndColumn (Location location,
int &line,
int &column) const;
std::string getLocationLineAndColumn (Location location) const;
typedef std::stack<Value *> Nodes;
Nodes nodes_;
Errors errors_;
std::string document_;
Location begin_;
Location end_;
Location current_;
Location lastValueEnd_;
Value *lastValue_;
};
std::istream& operator >> (std::istream&, Value&);
} // namespace json
namespace json
{
class Value;
class Writer
{
public:
Writer () { }
~Writer () { }
public:
std::string write (const Value &root);
private:
void writeValue (const Value &value);
std::string document_;
};
std::string valueToString (int value);
std::string valueToString (unsigned value);
std::string valueToString (double value);
std::string valueToString (bool value);
std::string valueToQuotedString (char const *value);
} // namespace json
/*
* JSON-RPC implementation in C++
*/
namespace json
{
namespace rpc
{
typedef void (*method) (HTTPRequest *http, Value &request, Value &response);
void init (void);
void add_method (char *name, method mth);
void service (HTTPRequest *http, Value &request, Value &response);
void process (HTTPRequest *http, std::string &response, char const *request);
}
}
#endif // JSON_H
|