-
Notifications
You must be signed in to change notification settings - Fork 5
/
json.h
410 lines (336 loc) · 8.75 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
401
402
403
404
405
406
407
408
409
410
#ifndef JSON_INCLUDED
#define JSON_INCLUDED
#ifdef _WIN32
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef long long int int64_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long int uint64_t;
#else
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#endif
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <limits>
#include <algorithm>
using namespace std;
class JValue
{
public:
enum TYPE
{
E_NULL = 0,
E_INT,
E_BOOL,
E_FLOAT,
E_ARRAY,
E_OBJECT,
E_STRING,
E_DATE,
E_DATA,
};
public:
JValue(TYPE type = E_NULL);
JValue(int val);
JValue(bool val);
JValue(double val);
JValue(int64_t val);
JValue(const char *val);
JValue(const string &val);
JValue(const JValue &other);
JValue(const char *val, size_t len);
~JValue();
public:
int asInt() const;
bool asBool() const;
double asFloat() const;
int64_t asInt64() const;
string asString() const;
const char *asCString() const;
time_t asDate() const;
string asData() const;
void assignData(const char *val, size_t size);
void assignDate(time_t val);
void assignDateString(time_t val);
TYPE type() const;
size_t size() const;
void clear();
JValue &at(int index);
JValue &at(size_t index);
JValue &at(const char *key);
bool has(const char *key) const;
int index(const char *ele) const;
bool keys(vector<string> &arrKeys) const;
bool join(JValue &jv);
bool append(JValue &jv);
bool remove(int index);
bool remove(size_t index);
bool remove(const char *key);
JValue &back();
JValue &front();
bool push_back(int val);
bool push_back(bool val);
bool push_back(double val);
bool push_back(int64_t val);
bool push_back(const char *val);
bool push_back(const string &val);
bool push_back(const JValue &jval);
bool push_back(const char *val, size_t len);
bool isInt() const;
bool isNull() const;
bool isBool() const;
bool isFloat() const;
bool isArray() const;
bool isObject() const;
bool isString() const;
bool isEmpty() const;
bool isData() const;
bool isDate() const;
bool isDataString() const;
bool isDateString() const;
operator int() const;
operator bool() const;
operator double() const;
operator int64_t() const;
operator string() const;
operator const char *() const;
JValue &operator=(const JValue &other);
JValue &operator[](int index);
const JValue &operator[](int index) const;
JValue &operator[](size_t index);
const JValue &operator[](size_t index) const;
JValue &operator[](int64_t index);
const JValue &operator[](int64_t index) const;
JValue &operator[](const char *key);
const JValue &operator[](const char *key) const;
JValue &operator[](const string &key);
const JValue &operator[](const string &key) const;
friend bool operator==(const JValue &jv, const char *psz)
{
return (0 == strcmp(jv.asCString(), psz));
}
friend bool operator==(const char *psz, const JValue &jv)
{
return (0 == strcmp(jv.asCString(), psz));
}
friend bool operator!=(const JValue &jv, const char *psz)
{
return (0 != strcmp(jv.asCString(), psz));
}
friend bool operator!=(const char *psz, const JValue &jv)
{
return (0 != strcmp(jv.asCString(), psz));
}
private:
void Free();
char *NewString(const char *cstr);
void CopyValue(const JValue &src);
bool WriteDataToFile(const char *file, const char *data, size_t len);
public:
static const JValue null;
static const string nullData;
private:
union HOLD {
bool vBool;
double vFloat;
int64_t vInt64;
char *vString;
vector<JValue> *vArray;
map<string, JValue> *vObject;
time_t vDate;
string *vData;
wchar_t *vUnicode;
} m_Value;
TYPE m_eType;
public:
string write() const;
const char *write(string &strDoc) const;
string styleWrite() const;
const char *styleWrite(string &strDoc) const;
bool read(const char *pdoc, string *pstrerr = NULL);
bool read(const string &strdoc, string *pstrerr = NULL);
string writePList() const;
const char *writePList(string &strDoc) const;
bool readPList(const string &strdoc, string *pstrerr = NULL);
bool readPList(const char *pdoc, size_t len = 0, string *pstrerr = NULL);
bool readFile(const char *file, string *pstrerr = NULL);
bool readPListFile(const char *file, string *pstrerr = NULL);
bool writeFile(const char *file);
bool writePListFile(const char *file);
bool styleWriteFile(const char *file);
bool readPath(const char *path, ...);
bool readPListPath(const char *path, ...);
bool writePath(const char *path, ...);
bool writePListPath(const char *path, ...);
bool styleWritePath(const char *path, ...);
};
class JReader
{
public:
bool parse(const char *pdoc, JValue &root);
void error(string &strmsg) const;
private:
struct Token
{
enum TYPE
{
E_Error = 0,
E_End,
E_Null,
E_True,
E_False,
E_Number,
E_String,
E_ArrayBegin,
E_ArrayEnd,
E_ObjectBegin,
E_ObjectEnd,
E_ArraySeparator,
E_MemberSeparator
};
TYPE type;
const char *pbeg;
const char *pend;
};
void skipSpaces();
void skipComment();
bool match(const char *pattern, int patternLength);
bool readToken(Token &token);
bool readValue(JValue &jval);
bool readArray(JValue &jval);
void readNumber();
bool readString();
bool readObject(JValue &jval);
bool decodeNumber(Token &token, JValue &jval);
bool decodeString(Token &token, string &decoded);
bool decodeDouble(Token &token, JValue &jval);
char GetNextChar();
bool addError(const string &message, const char *ploc);
private:
const char *m_pBeg;
const char *m_pEnd;
const char *m_pCur;
const char *m_pErr;
string m_strErr;
};
class JWriter
{
public:
static void FastWrite(const JValue &jval, string &strDoc);
static void FastWriteValue(const JValue &jval, string &strDoc);
public:
const string &StyleWrite(const JValue &jval);
private:
void PushValue(const string &strval);
void StyleWriteValue(const JValue &jval);
void StyleWriteArrayValue(const JValue &jval);
bool isMultineArray(const JValue &jval);
public:
static string v2s(double val);
static string v2s(int64_t val);
static string v2s(const char *val);
static string vstring2s(const char *val);
static string d2s(time_t t);
private:
string m_strDoc;
string m_strTab;
bool m_bAddChild;
vector<string> m_childValues;
};
//////////////////////////////////////////////////////////////////////////
class PReader
{
public:
PReader();
public:
bool parse(const char *pdoc, size_t len, JValue &root);
void error(string &strmsg) const;
private:
struct Token
{
enum TYPE
{
E_Error = 0,
E_End,
E_Null,
E_True,
E_False,
E_Key,
E_Data,
E_Date,
E_Integer,
E_Real,
E_String,
E_ArrayBegin,
E_ArrayEnd,
E_ArrayNull,
E_DictionaryBegin,
E_DictionaryEnd,
E_DictionaryNull,
E_ArraySeparator,
E_MemberSeparator
};
Token()
{
pbeg = NULL;
pend = NULL;
type = E_Error;
}
TYPE type;
const char *pbeg;
const char *pend;
};
bool readToken(Token &token);
bool readLabel(string &label);
bool readValue(JValue &jval, Token &token);
bool readArray(JValue &jval);
bool readNumber();
bool readString();
bool readDictionary(JValue &jval);
void endLabel(Token &token, const char *szLabel);
bool decodeNumber(Token &token, JValue &jval);
bool decodeString(Token &token, string &decoded);
bool decodeDouble(Token &token, JValue &jval);
void skipSpaces();
bool addError(const string &message, const char *ploc);
public:
bool parseBinary(const char *pbdoc, size_t len, JValue &pv);
private:
uint32_t getUInt24FromBE(const char *v);
void byteConvert(uint8_t *v, size_t size);
uint64_t getUIntVal(const char *v, size_t size);
bool readUIntSize(const char *&pcur, size_t &size);
bool readBinaryValue(const char *&pcur, JValue &pv);
bool readUnicode(const char *pcur, size_t size, JValue &pv);
public:
static void XMLUnescape(string &strval);
private: //xml
const char *m_pBeg;
const char *m_pEnd;
const char *m_pCur;
const char *m_pErr;
string m_strErr;
private: //binary
const char *m_pTrailer;
uint64_t m_uObjects;
uint8_t m_uOffsetSize;
const char *m_pOffsetTable;
uint8_t m_uDictParamSize;
};
class PWriter
{
public:
static void FastWrite(const JValue &pval, string &strdoc);
static void FastWriteValue(const JValue &pval, string &strdoc, string &strindent);
public:
static void XMLEscape(string &strval);
static string &StringReplace(string &context, const string &from, const string &to);
};
#endif // JSON_INCLUDED