-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-plus.h
409 lines (368 loc) · 11.5 KB
/
json-plus.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
//
// json-plus.h
//
// Author:
// Brian Sullender
// SULLE WAREHOUSE LLC
//
// Description:
// The header file for the json_plus namespace.
// https://github.com/sullewarehouse/json-plus
//
#ifndef JSON_PLUS_H
#define JSON_PLUS_H
#include <cstdlib>
namespace json_plus
{
// UTF8 functions
namespace UTF8_Encoding
{
// Get the number of 'char' units for a UTF8 encoded Unicode character
unsigned char GetCharacterUnits(char Code);
// Encode a Unicode code point (character) as UTF8
unsigned char Encode(char* pBuffer, size_t Length, unsigned long CodePoint);
// Encode a Unicode code point (character) as UTF8 (unsafe version)
unsigned char EncodeUnsafe(char* pBuffer, unsigned long CodePoint);
// Get a Unicode code point (character) from a UTF8 encoded string
unsigned long Decode(unsigned char Units, const char* String);
// Get the number of 'char' units for a UTF8 encoded string
size_t GetStringUnits(const char* String);
// Copy a UTF8 encoded string
size_t StringCopy(char* Destination, size_t Length, const char* Source);
// Compare 2 UTF8 encoded strings
long CompareStrings(const char* String1, const char* String2);
// Compare 2 UTF8 encoded strings (insensitive)
long CompareStringsInsensitive(const char* String1, const char* String2);
}
// JSON element types
enum class JSON_TYPE
{
OBJECT,
ARRAY,
STRING,
NUMBER,
BOOLEAN,
NULL_TYPE
};
// JSON node, for the parsed tree
typedef struct _JSON_NODE JSON_NODE;
struct _JSON_NODE
{
// Next node in the linked list
JSON_NODE* next;
// Key for the node
char* key;
// Value for the node
void* value;
// JSON node type
JSON_TYPE type;
// Format override for the node
const char* format;
// Get value as a char* string
const char* String();
// Get value as a double
double Double();
// Get value as a int
int Int();
// Get value as a long
long Long();
// Get value as a long long (64-bit int)
long long Int64();
// Get value as a boolean
bool Boolean();
};
// JSON error codes
typedef enum class _JSON_ERROR_CODE {
NONE,
// general errors:
INVALID_PARAMETER,
OUT_OF_MEMORY,
// parse errors:
UNRECOGNIZED_TOKEN,
UNEXPECTED_START_TOKEN,
// parse object errors:
OBJECT_SYNTAX_ERROR_EXPECTED_COLON,
OBJECT_SYNTAX_ERROR_KEY_ALREADY_DEFINED,
OBJECT_SYNTAX_ERROR_KEY_NOT_DEFINED,
UNEXPECTED_CLOSING_SQUARE_BRACKET,
EXPECTED_CURLY_BRACKET_ENCOUNTERED_JSON_END,
EXPECTED_PAIR_ENCOUNTERED_OBJECT_END,
// parse array errors:
UNEXPECTED_ARRAY_VALUE,
UNEXPECTED_PAIR_COLON_TOKEN,
UNEXPECTED_CLOSING_CURLY_BRACKET,
EXPECTED_SQUARE_BRACKET_ENCOUNTERED_JSON_END,
EXPECTED_ARRAY_VALUE,
// parse string errors:
STRING_CHARACTERS_MUST_BE_ESCAPED,
STRING_FORCED_STRICT_ESCAPING,
STRING_UNUSED_ESCAPE_CHARACTER,
EXPECTED_DOUBLE_QUOTES_ENCOUNTERED_JSON_END,
// parse literal name errors:
INVALID_LITERAL_NAME,
} JSON_ERROR_CODE;
// JSON parsing context
class JSON_PARSER_CONTEXT
{
public:
// Default initializer
JSON_PARSER_CONTEXT();
// Error after JSON_Parse call
JSON_ERROR_CODE errorCode;
// Error description after JSON_Parse call
const char* errorDescription;
// Force strict string escaping for code editors
bool visualEscapeOnly;
// Number of characters parsed
unsigned long charNumber;
// Number of lines parsed
unsigned long lineNumber;
// Begin index of the error
unsigned long beginIndex;
// Number of characters from beginIndex
unsigned long errorLength;
};
// Create JSON string from node tree
char* JSON_Generate(JSON_NODE* json_root, const char* format);
// Parse a JSON string and create a node tree
JSON_NODE* JSON_Parse(const char* json, JSON_PARSER_CONTEXT* context);
// Free a JSON node tree
void JSON_Free(JSON_NODE* json_root);
// Get a JSON object from an object
JSON_NODE* JSON_GetObject(JSON_NODE* object, const char* key);
// Get a JSON array from an object
JSON_NODE* JSON_GetArray(JSON_NODE* object, const char* key);
// Get a JSON string from an object
char* JSON_GetString(JSON_NODE* object, const char* key);
// Get a JSON number from an object
char* JSON_GetNumber(JSON_NODE* object, const char* key);
// Get a JSON bool value from an object
bool JSON_GetBoolean(JSON_NODE* object, const char* key);
// Create a JSON node
JSON_NODE* JSON_CreateNode(JSON_TYPE type, const char* key, void* value);
// Forward declaration of JSON_OBJECT
class JSON_OBJECT;
// Forward declaration of JSON_ARRAY
class JSON_ARRAY;
// JSON object
class JSON_OBJECT
{
private:
// Object root node
JSON_NODE* json_root;
public:
// Default initializer
JSON_OBJECT();
// Standard initializer
JSON_OBJECT(JSON_NODE* root);
// Parse a JSON string and create a node tree
JSON_OBJECT(const char* json, JSON_PARSER_CONTEXT* context);
// Assignment operator overload
JSON_OBJECT& operator=(const JSON_OBJECT& other);
// Assigment operator overload
operator JSON_NODE* () const;
// Create a root object node and assign it to this object
JSON_NODE* MakeRoot();
// Free JSON node tree
void Free();
// Check if the root object exists
bool Empty();
// Get the number of items in the object
unsigned long Count();
// Get the first node item in the object
JSON_NODE* First();
// Get an object from the object using a key
JSON_OBJECT Object(const char* key);
// Get an array from the object using a key
JSON_ARRAY Array(const char* key);
// Get a string from the object using a key
const char* String(const char* key);
// Get a boolean from the object using a key
bool Boolean(const char* key);
// Nested Number class
class Number
{
private:
// Parent reference
JSON_OBJECT& parent;
public:
// Constructor for the Number class, which is a nested class inside the JSON_OBJECT class.
Number(JSON_OBJECT& parent);
// Get a double from the object using a key
double Double(const char* key);
// Get a int from the object using a key
int Int(const char* key);
// Get a long from the object using a key
long Long(const char* key);
// Get a 64-bit int from the object using a key
long long Int64(const char* key);
// Get a number from the object as a string using a key
const char* String(const char* key);
};
// Get a number from the object
Number Number{ *this };
// Nested Insert class
class Insert
{
private:
// Parent reference
JSON_OBJECT& parent;
public:
// Constructor for the Insert class, which is a nested class inside the JSON_OBJECT class.
Insert(JSON_OBJECT& parent);
// Insert an object with a key
JSON_OBJECT Object(const char* key);
// Insert an array with a key
JSON_ARRAY Array(const char* key);
// Insert a string with a key
JSON_NODE* String(const char* key, const char* value);
// Insert a boolean with a key
JSON_NODE* Boolean(const char* key, bool value);
// Nested Number class for Insert
class Number
{
private:
// Parent reference
Insert& parent;
public:
// Constructor for the Number class, which is a nested class inside the JSON_OBJECT::Insert class.
Number(Insert& parent);
// Insert a double with a key
JSON_NODE* Double(const char* key, double value);
// Insert a int with a key
JSON_NODE* Int(const char* key, int value);
// Insert a long with a key
JSON_NODE* Long(const char* key, long value);
// Insert a 64-bit int with a key
JSON_NODE* Int64(const char* key, long long value);
// Insert a number as a string with a key
JSON_NODE* String(const char* key, const char* value);
};
// Insert a number into the object
Number Number{ *this };
};
// Insert a JSON object, array or key-value pair
Insert Insert{ *this };
// Delete a key-value pair using a key
bool Delete(const char* key);
// Delete a key-value pair using a reference
bool Delete(JSON_NODE* reference);
// Create JSON from object
char* Generate(const char* format);
// Format for the object, this will override the format parameter passed to JSON_Generate
bool Format(const char* format);
// Parse a JSON string and create a node tree
JSON_NODE* Parse(const char* json, JSON_PARSER_CONTEXT* context);
};
// JSON array
class JSON_ARRAY
{
private:
// Array root node
JSON_NODE* json_root;
public:
// Default initializer
JSON_ARRAY();
// Standard initializer
JSON_ARRAY(JSON_NODE* root);
// Parse a JSON string and create a node tree
JSON_ARRAY(const char* json, JSON_PARSER_CONTEXT* context);
// Assignment operator overload
JSON_ARRAY& operator=(const JSON_ARRAY& other);
// Assignment operator overload
operator JSON_NODE* () const;
// Create a root array node and assign it to this array
JSON_NODE* MakeRoot();
// Free JSON node tree
void Free();
// Check if the root array exists
bool Empty();
// Get the number of items in the array
unsigned long Count();
// Get the first node item in the array
JSON_NODE* First();
// Get an object from the array using an index
JSON_OBJECT Object(unsigned long i);
// Get an array from the array using an index
JSON_ARRAY Array(unsigned long i);
// Get a string from the array using an index
const char* String(unsigned long i);
// Get a boolean from the array using an index
bool Boolean(unsigned long i);
// Nested Number class
class Number
{
private:
// Parent reference
JSON_ARRAY& parent;
public:
// Constructor for the Number class, which is a nested class inside the JSON_ARRAY class.
Number(JSON_ARRAY& parent);
// Get a double from the array using an index
double Double(unsigned long i);
// Get a int from the array using an index
int Int(unsigned long i);
// Get a long from the array using an index
long Long(unsigned long i);
// Get a 64-bit int from the array using an index
long long Int64(unsigned long i);
// Get a double from the array using an index
const char* String(unsigned long i);
};
// Get a number from the array
Number Number{ *this };
// Nested Insert class
class Insert
{
private:
// Parent reference
JSON_ARRAY& parent;
public:
// Constructor for the Insert class, which is a nested class inside the JSON_ARRAY class.
Insert(JSON_ARRAY& parent);
// Insert an object
JSON_OBJECT Object();
// Insert an array
JSON_ARRAY Array();
// Insert a string
JSON_NODE* String(const char* value);
// Insert a boolean
JSON_NODE* Boolean(bool value);
// Nested Number class for Insert
class Number
{
private:
// Parent reference
Insert& parent;
public:
// Constructor for the Number class, which is a nested class inside the JSON_ARRAY::Insert class.
Number(Insert& parent);
// Insert a double
JSON_NODE* Double(double value);
// Insert a int
JSON_NODE* Int(int value);
// Insert a long
JSON_NODE* Long(long value);
// Insert a 64-bit int
JSON_NODE* Int64(long long value);
// Insert a number as a string
JSON_NODE* String(const char* value);
};
// Insert a number into the array
Number Number{ *this };
};
// Insert a JSON object, array or key-value pair
Insert Insert{ *this };
// Delete a key-value pair using a index
bool Delete(unsigned long i);
// Delete a key-value pair using a reference
bool Delete(JSON_NODE* reference);
// Create JSON from array
char* Generate(const char* format);
// Format for the array, this will override the format parameter passed to JSON_Generate
bool Format(const char* format);
// Parse a JSON string and create a node tree
JSON_NODE* Parse(const char* json, JSON_PARSER_CONTEXT* context);
};
}
#endif // !JSON_PLUS_H