-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.cpp
More file actions
669 lines (589 loc) · 13.8 KB
/
JSON.cpp
File metadata and controls
669 lines (589 loc) · 13.8 KB
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// JSON library
// source file
// Author: @someperson75
// Date: 08/09/2024
// Version: 3.0
// Description: json class for c++ with performs input and output on I/O stream
#include "JSON.h"
#include <iostream>
#include <string>
#include <stdexcept>
#include <unordered_map>
#include <vector>
#ifndef JSON_lib_functions
#define JSON_lib_functions
////////////////////////////
// //
// Json_error functions //
// //
////////////////////////////
Json_error::Json_error(const char *msg)
{
this->msg = msg;
}
Json_error::Json_error(const std::string msg)
{
this->msg = msg;
}
const char *Json_error::what() const noexcept
{
return msg.c_str();
}
////////////////////////////
// //
// Object functions //
// //
////////////////////////////
void Object::clear()
{
for (auto it = object.begin(); it != object.end(); it++)
it->second.clear();
}
JSON &Object::operator[](const std::string key)
{
if (object.find(key) == object.end())
object[key] = JSON();
return object[key];
}
JSON &Object::operator[](const char *key)
{
if (object.find(key) == object.end())
object[key] = JSON();
return object[key];
}
Object &Object::operator=(const Object &other)
{
clear();
object = other.object;
return *this;
}
std::unordered_map<std::string, JSON> &Object::values()
{
return object;
}
Object::Object()
{
clear();
}
std::unordered_map<const std::string, JSON>::const_iterator Object::begin() const
{
return object.begin();
}
std::unordered_map<const std::string, JSON>::const_iterator Object::end() const
{
return object.end();
}
///////////////////////////
// //
// Array functions //
// //
///////////////////////////
void Array::clear()
{
for (auto it = array.begin(); it != array.end(); it++)
it->clear();
}
JSON &Array::operator[](const long long id)
{
if (id < 0)
throw Json_error("Out of bound error : index less than 0.\n");
if (id >= (long long)array.size())
array.resize(id + 1, JSON());
return array[id];
}
Array &Array::operator=(const Array &other)
{
clear();
array = other.array;
return *this;
}
Array &Array::push_back(const JSON &val)
{
array.push_back(val);
return *this;
}
Array &Array::operator+=(const JSON &val)
{
array.push_back(val);
return *this;
}
std::vector<JSON> &Array::values()
{
return array;
}
Array::Array()
{
clear();
}
std::vector<JSON>::const_iterator Array::begin() const
{
return array.begin();
}
std::vector<JSON>::const_iterator Array::end() const
{
return array.end();
}
////////////////////////////
// //
// JSON functions //
// //
////////////////////////////
void JSON::clear()
{
object.clear();
array.clear();
num = 0;
d = 0.;
str = "";
b = false;
type = Type::null;
}
JSON::JSON()
{
clear();
}
JSON::JSON(const Object object)
{
clear();
this->object = object;
type = Type::obj;
}
JSON::JSON(const Array array)
{
clear();
this->array = array;
type = Type::arr;
}
JSON::JSON(const int num)
{
clear();
this->num = num;
type = Type::integer;
}
JSON::JSON(const double num)
{
clear();
this->d = d;
type = Type::floatNum;
}
JSON::JSON(const std::string str)
{
clear();
this->str = str;
type = Type::string;
}
JSON::JSON(const char *str)
{
clear();
this->str = str;
type = Type::string;
}
JSON::JSON(const bool b)
{
clear();
this->b = b;
type = Type::boolean;
}
JSON &JSON::operator[](const int id)
{
if (type == Type::arr)
return array[id];
else
throw Json_error("Type error : not an Array.\n");
}
JSON &JSON::operator[](const std::string &key)
{
if (type == Type::obj)
return object[key];
else
throw Json_error("Type error : not an Object.\n");
}
JSON &JSON::operator=(const JSON val)
{
clear();
type = val.type;
switch (type)
{
case Type::obj:
object = val.object;
break;
case Type::arr:
array = val.array;
break;
case Type::integer:
num = val.num;
break;
case Type::floatNum:
d = val.d;
break;
case Type::string:
str = val.str;
break;
case Type::boolean:
b = val.b;
break;
default:
break;
}
return *this;
}
JSON &JSON::operator=(const Object val)
{
clear();
object = val;
type = Type::obj;
return *this;
}
JSON &JSON::operator=(const Array val)
{
clear();
array = val;
type = Type::arr;
return *this;
}
JSON &JSON::operator=(const int val)
{
clear();
num = val;
type = Type::integer;
return *this;
}
JSON &JSON::operator=(const double val)
{
clear();
d = val;
type = Type::floatNum;
return *this;
}
JSON &JSON::operator=(const std::string val)
{
clear();
str = val;
type = Type::string;
return *this;
}
JSON &JSON::operator=(const char *val)
{
clear();
str = val;
type = Type::string;
return *this;
}
JSON &JSON::operator=(const bool val)
{
clear();
b = val;
type = Type::boolean;
return *this;
}
template <typename T>
T &JSON::get(T n)
{
throw Json_error((std::string) "Error : type " + typeid(T).name() + " is not allowed here.\n");
return T();
}
template <>
Object &JSON::get<Object>(Object n)
{
if (type == Type::obj)
return object;
else
throw Json_error("Type error : not an Object.\n");
}
template <>
Array &JSON::get<Array>(Array n)
{
if (type == Type::arr)
return array;
else
throw Json_error("Type error : not an Array.\n");
}
template <>
int &JSON::get<int>(int n)
{
if (type == Type::integer)
return num;
else
throw Json_error("Type error : not an Integer.\n");
}
template <>
double &JSON::get<double>(double n)
{
if (type == Type::floatNum)
return d;
else
throw Json_error("Type error : not an Double.\n");
}
template <>
bool &JSON::get<bool>(bool n)
{
if (type == Type::boolean)
return b;
else
throw Json_error("Type error : not a Boolean.\n");
}
template <>
std::string &JSON::get<std::string>(std::string n)
{
if (type == Type::string)
return str;
else
throw Json_error("Type error : not a String.\n");
}
bool JSON::isNull() const
{
return type == Type::null;
}
bool JSON::isObject() const
{
return type == Type::obj;
}
bool JSON::isArray() const
{
return type == Type::arr;
}
bool JSON::isInteger() const
{
return type == Type::integer;
}
bool JSON::isDouble() const
{
return type == Type::floatNum;
}
bool JSON::isString() const
{
return type == Type::string;
}
bool JSON::isBoolean() const
{
return type == Type::boolean;
}
///////////////////////////
// //
// I/O functions //
// //
///////////////////////////
std::string JSON::strigify(bool preaty, int level) const
{
std::string line = " ";
if (preaty)
{
line = "\n";
for (int i = 0; i <= level; i++)
line += "\t";
}
std::string out = "";
switch (type)
{
case Type::null:
out = "null";
break;
case Type::obj:
out += '{';
out += line;
for (auto iter = object.begin(); iter != object.end();)
{
out += "\"" + iter->first + "\": " + iter->second.strigify(preaty, level + 1);
if (++iter != object.end())
out += ',';
out += line;
}
if (preaty)
out.pop_back();
out += '}';
break;
case Type::arr:
out += '[';
out += line;
for (auto iter = array.begin(); iter != array.end();)
{
out += iter->strigify(preaty, level + 1);
if (++iter != array.end())
out += ',';
out += line;
}
if (preaty)
out.pop_back();
out += ']';
break;
case Type::integer:
out = std::to_string(num);
break;
case Type::floatNum:
out = std::to_string(d);
break;
case Type::string:
out = "\"" + str + "\"";
break;
case Type::boolean:
out = b ? "true" : "false";
break;
default:
break;
}
return out;
}
std::ostream &operator<<(std::ostream &os, const JSON &json)
{
os << json.strigify(true);
return os;
}
void ExtractNotInterestingChar(std::istream &is)
{
char next = is.eof() ? ' ' : is.peek();
while (next == ' ' || next == '\n' || next == '\t')
is.get(), next = is.peek();
}
JSON readJSON(std::istream &is)
{
ExtractNotInterestingChar(is);
char c = is.get();
if (c == '\"')
{
// string
std::string str = "";
char last = '"';
char current = is.get();
while (current != '"' || last == '\\')
{
if (last == '\\')
{
switch (current)
{
case '\\':
str += '\\';
break;
case 'n':
str += '\n';
break;
case 't':
str += '\t';
break;
case '"':
str += '"';
break;
default:
break;
}
current = '\0';
}
if (current != '\\' && current != '\0')
str += current;
last = current, current = is.get();
}
return JSON(str);
}
else if (c == '-' || c == '+' || (c >= '0' && c <= '9'))
{
// integer
int num = 0;
int divide = 0;
bool signe = (c == '-');
char current = c < '0' ? is.get() : c;
while ((current >= '0' && current <= '9') || current == '.')
{
num *= 10;
divide *= 10;
if (current == '.' && divide != 0)
throw Json_error("Syntax error: there can't be two or more decimal point.\n");
if (current == '.')
divide = 1;
num += (current - '0');
current = is.get();
}
is.unget();
if (divide != 0)
return JSON((double)(num * (signe ? -1 : 1)) / (double)divide);
return JSON(num * (signe ? -1 : 1));
}
else if (c == 't' || c == 'f')
{
// boolean
bool result = (c == 't');
char null[5] = "\0\0\0\0";
if (result)
is.read(null, 3);
else
is.read(null, 4);
if ((std::string)null != (std::string) "alse" && (std::string)null != (std::string) "rue")
throw Json_error((std::string) "Syntax error : Unknow symbol '" + c + null + "'.\n");
return JSON(result);
}
else if (c == '{')
{
// Object
Object o;
while (true)
{
ExtractNotInterestingChar(is);
std::string str = "";
char last = is.get();
char current = is.get();
while (current != '"' || last == '\\')
{
if (last == '\\')
{
switch (current)
{
case '\\':
str += '\\';
break;
case 'n':
str += '\n';
break;
case 't':
str += '\t';
break;
case '"':
str += '"';
break;
default:
break;
}
current = '\0';
}
if (current != '\\' && current != '\0')
str += current;
last = current, current = is.get();
}
ExtractNotInterestingChar(is);
char c = is.get();
if (c != ':')
throw Json_error((std::string) "Syntax error : Unknow symbol '" + c + "'.\n");
JSON obj = readJSON(is);
o[str] = obj;
ExtractNotInterestingChar(is);
char end = is.get();
if (end == '}')
break;
else if (end != ',')
throw Json_error((std::string) "Syntax error : Unknow symbol '" + end + "'.\n");
}
return JSON(o);
}
else if (c == '[')
{
// Array
Array a;
while (true)
{
JSON obj = readJSON(is);
a += obj;
ExtractNotInterestingChar(is);
char end = is.get();
if (end == ']')
break;
else if (end != ',')
throw Json_error((std::string) "Syntax error : Unknow symbol '" + end + "'.\n");
}
return JSON(a);
}
else
throw Json_error((std::string) "Syntax error : Unknow symbol '" + c + "'.\n");
}
std::istream &operator>>(std::istream &is, JSON &json)
{
json = readJSON(is);
return is;
}
#endif