-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsttJson.hpp
759 lines (687 loc) · 19.5 KB
/
bsttJson.hpp
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#pragma once
#include <charconv>
#include <cstdint>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
inline std::string to_string(const std::string& s) { return s; }
#ifdef USE_BSTT_NAMESPACE
namespace bstt
{
#endif
static constexpr size_t MAX_JSON_DEPTH = 1024;
static const std::string& getTab(const std::string& tab, size_t tabCount)
{
static std::map<std::string, std::vector<std::string>> tabListMap;
static std::string emptyTab;
if (tab.empty()) return emptyTab;
auto it = tabListMap.find(tab);
if (it == tabListMap.end())
{
tabListMap.emplace(tab, std::vector<std::string>{"", tab});
it = tabListMap.find(tab);
it->second.reserve(MAX_JSON_DEPTH);
}
while (it->second.size() <= tabCount) it->second.push_back(it->second.back() + tab);
return it->second[tabCount];
}
using JsonObj = std::map<std::string, struct Json>;
using JsonArr = std::vector<struct Json>;
template <typename T> T from_string(const std::string& s);
template <> inline std::string from_string<std::string>(const std::string& s) { return s; }
template <> inline int from_string<int>(const std::string& s) { return std::stoi(s); }
template <> inline int64_t from_string<int64_t>(const std::string& s) { return std::stoll(s); }
template <> inline size_t from_string<size_t>(const std::string& s) { return std::stoull(s); }
template <> inline double from_string<double>(const std::string& s) { return std::stod(s); }
template <typename T> T fromJson(const Json&);
template <typename T> Json toJson(const T&);
void parseValue(const std::string_view& str, size_t& pos, Json& jsonValue, size_t depth);
struct Json
{
enum class Type
{
Null,
Bool,
Number,
String,
Array,
Object
};
static std::string typeToString(Type type)
{
static const std::vector<std::string> typeStringArr = {"Null", "Bool", "Number", "String", "Array", "Object"};
return typeStringArr[static_cast<size_t>(type)];
}
template <typename T> static Json::Type typeToType(const T&) { return Type::Null; }
static Type typeToType(const bool&) { return Type::Bool; }
static Type typeToType(const int&) { return Type::Number; }
static Type typeToType(const int64_t&) { return Type::Number; }
static Type typeToType(const size_t&) { return Type::Number; }
static Type typeToType(const double&) { return Type::Number; }
static Type typeToType(const std::string&) { return Type::String; }
static Type typeToType(const JsonObj&) { return Type::Object; }
static Type typeToType(const JsonArr&) { return Type::Array; }
static Json parse(const std::string_view& str)
{
Json json;
size_t pos = 0;
parseValue(str, pos, json, 0);
if (pos != str.size()) throw std::runtime_error("Extra characters at position " + std::to_string(pos));
return json;
}
static bool tryParse(const std::string_view& str, Json& json)
{
std::string error;
return tryParse(str, json, error);
}
static bool tryParse(const std::string_view& str, Json& json, std::string& error)
{
size_t pos = 0;
try
{
parseValue(str, pos, json, 0);
if (pos != str.size()) throw std::runtime_error("Extra characters at position " + std::to_string(pos));
return true;
}
catch (const std::exception& e)
{
error = e.what();
return false;
}
}
static Json parseFile(const std::string& fileName)
{
std::ifstream ifs(fileName);
std::ostringstream oss;
oss << ifs.rdbuf();
return parse(oss.str());
}
static bool tryParseFile(const std::string& fileName, Json& json)
{
std::string error;
return tryParseFile(fileName, json, error);
}
static bool tryParseFile(const std::string& fileName, Json& json, std::string& error)
{
std::ifstream ifs(fileName);
std::ostringstream oss;
oss << ifs.rdbuf();
return tryParse(oss.str(), json, error);
}
// Constructors
Json() : b(false) {} // default is null
Json(const Json& v) { *this = v; }
template <typename T> Json(const T& v) { *this = v; }
// Move constructor
Json(Json&& v) noexcept : type(v.type), b(false)
{
switch (v.type)
{
case Type::Null:
break;
case Type::Bool:
b = v.b;
break;
case Type::Number:
num = v.num;
break;
case Type::String:
new (&str) std::string(std::move(v.str));
break;
case Type::Object:
new (&obj) JsonObj(std::move(v.obj));
break;
case Type::Array:
new (&arr) JsonArr(std::move(v.arr));
break;
}
v.type = Type::Null;
}
// Destructor
~Json() {}
// Assignments
Json& operator=(const Json& rhs)
{
if (this == &rhs) return *this;
type = rhs.type;
switch (rhs.type)
{
case Type::Null:
b = false;
break;
case Type::Bool:
b = rhs.b;
break;
case Type::Number:
num = rhs.num;
break;
case Type::String:
*this = rhs.str;
break;
case Type::Object:
*this = rhs.obj;
break;
case Type::Array:
*this = rhs.arr;
break;
}
return *this;
}
Json& operator=(std::nullptr_t)
{
type = Type::Null;
return *this;
}
Json& operator=(bool b_)
{
type = Type::Bool;
b = b_;
return *this;
}
Json& operator=(int i)
{
type = Type::Number;
num = i;
return *this;
}
Json& operator=(int64_t i)
{
type = Type::Number;
num = static_cast<double>(i);
return *this;
}
Json& operator=(size_t i)
{
type = Type::Number;
num = static_cast<double>(i);
return *this;
}
Json& operator=(double d_)
{
type = Type::Number;
num = d_;
return *this;
}
Json& operator=(const char* s_)
{
type = Type::String;
new (&str) std::string(s_);
// replace tabs and newlines with escape sequences
size_t pos = 0;
while ((pos = str.find_first_of("\t\r\n", pos)) != std::string::npos)
{
if (str[pos] == '\t') str.replace(pos, 1, "\\t");
else if (str[pos] == '\r')
str.replace(pos, 1, "\\r");
else
str.replace(pos, 1, "\\n");
pos += 2;
}
return *this;
}
Json& operator=(const std::string& s_) { return *this = s_.c_str(); }
Json& operator=(const JsonObj& obj_)
{
type = Type::Object;
new (&obj) JsonObj(obj_);
return *this;
}
Json& operator=(const JsonArr& arr_)
{
type = Type::Array;
new (&arr) JsonArr(arr_);
return *this;
}
template <typename T> Json& operator=(const T& t)
{
*this = toJson<T>(t);
return *this;
}
template <typename T, typename U> Json& operator=(const std::map<T, U>& tuMap)
{
using namespace std;
type = Type::Object;
new (&obj) JsonObj();
for (const auto& [key, value] : tuMap) (*this)[to_string(key)] = value;
return *this;
}
template <typename T> Json& operator=(const std::vector<T>& tList)
{
type = Type::Array;
new (&arr) JsonArr();
arr.resize(tList.size());
for (size_t i = 0; i < tList.size(); i++) (*this)[i] = tList[i];
return *this;
}
// Move assignment
Json& operator=(Json&& rhs) noexcept
{
if (this == &rhs) return *this;
type = rhs.type;
switch (rhs.type)
{
case Type::Null:
break;
case Type::Bool:
b = rhs.b;
break;
case Type::Number:
num = rhs.num;
break;
case Type::String:
new (&str) std::string(std::move(rhs.str));
break;
case Type::Object:
new (&obj) JsonObj(std::move(rhs.obj));
break;
case Type::Array:
new (&arr) JsonArr(std::move(rhs.arr));
break;
}
rhs.type = Type::Null;
return *this;
}
// Conversions
operator const bool&() const { return b; }
operator int() const { return static_cast<int>(num); }
operator int64_t() const { return static_cast<int64_t>(num); }
operator size_t() const { return static_cast<size_t>(num); }
operator const double&() const { return num; }
operator std::string() const { return str; }
operator const char*() const { return str.c_str(); }
operator const JsonObj&() const { return obj; }
operator const JsonArr&() const { return arr; }
template <typename T> operator T() const { return fromJson<T>(*this); }
template <typename T, typename U> operator std::map<T, U>() const
{
std::map<T, U> tuMap;
for (const auto& [key_, val_] : obj) tuMap[key_] = val_;
return tuMap;
}
template <typename T> operator std::vector<T>() const
{
std::vector<T> tList;
for (const auto& value : arr) tList.push_back(fromJson<T>(value));
return tList;
}
// Get
template <typename T> void get(const std::string& key, T& value) const
{
const auto& child = (*this)[key];
child.checkKeyType(key, typeToType(value));
value = child;
}
void get(const std::string& key, std::string& value) const
{
const auto& child = (*this)[key];
child.checkKeyType(key, Type::String);
value = std::string(child.str);
}
template <typename T, typename U> void get(const std::string& key, std::map<T, U>& value) const
{
const auto& child = (*this)[key];
child.checkKeyType(key, Type::Object);
for (const auto& [key_, val_] : child.obj) value[from_string<T>(key_)] = val_;
}
template <typename T> void get(const std::string& key, std::vector<T>& value) const
{
const auto& child = (*this)[key];
child.checkKeyType(key, Type::Array);
value = child.operator std::vector<T, std::allocator<T>>();
}
template <typename T, typename... Args> void get(const std::string& key, T& value, Args&&... args) const
{
get(key, value);
get(args...);
}
// Set
template <typename T> void set(const std::string& key, const T& value) { (*this)[key] = value; }
template <typename T, typename... Args> void set(const std::string& key, const T& value, Args&&... args)
{
set(key, value);
set(args...);
}
// Try get
template <typename T> bool tryGet(const std::string& key, T& value) const
{
auto it = obj.find(key);
if (it != obj.end()) value = it->second;
return it != obj.end();
}
bool tryGet(const std::string& key, std::string& value) const
{
auto it = obj.find(key);
if (it != obj.end())
{
it->second.checkKeyType(key, Type::String);
value = std::string(it->second.str);
}
return it != obj.end();
}
template <typename T, typename U> bool tryGet(const std::string& key, std::map<T, U>& value) const
{
auto it = obj.find(key);
if (it != obj.end())
{
it->second.checkKeyType(key, Type::Object);
for (const auto& [k, val] : it->second.obj) value[from_string<T>(k)] = val;
}
return it != obj.end();
}
template <typename T> bool tryGet(const std::string& key, std::vector<T>& value) const
{
auto it = obj.find(key);
if (it != obj.end())
{
it->second.checkKeyType(key, Type::Array);
value = std::vector<T>(it->second);
}
return it != obj.end();
}
template <typename T, typename... Args> bool tryGet(const std::string& key, T& value, Args&&... args) const
{
bool allFound = tryGet(key, value);
return tryGet(args...) && allFound;
}
// Array functions
const Json& operator[](size_t index) const { return arr[index]; }
Json& operator[](size_t index)
{
if (type != Type::Array) *this = JsonArr();
if (arr.size() <= index) arr.resize(index + 1);
return arr[index];
}
void resize(size_t size) { arr.resize(size); }
// Object functions
const Json& operator[](const std::string& key) const
{
auto it = obj.find(key);
if (it == obj.end()) throw std::runtime_error("Key not found: '" + key + "'");
return it->second;
}
const Json& operator[](const char* key) const { return (*this)[std::string(key)]; }
Json& operator[](const std::string& key)
{
if (type != Type::Object) *this = JsonObj();
return obj[key];
}
Json& operator[](const char* key)
{
if (type != Type::Object) *this = JsonObj();
return obj[key];
}
// Display
std::string toString(const std::string& tab = "", const std::string& newLine = "") const
{
std::ostringstream ostr;
display(ostr, tab, newLine);
return ostr.str();
}
friend std::ostream& operator<<(std::ostream& os, const Json& v) { return v.display(os); }
std::ostream& display(
std::ostream& os, const std::string& tab = "", const std::string& newLine = "", size_t currentTabCount = 0) const
{
switch (type)
{
case Type::Null:
return os << "null";
case Type::Bool:
return os << (b ? "true" : "false");
case Type::Number:
return os << num;
case Type::String:
return os << '"' << str << '"';
case Type::Object:
return displayAsObject(os, currentTabCount, tab, newLine);
case Type::Array:
return displayAsArray(os, currentTabCount, tab, newLine);
}
return os;
}
void writeToFile(const std::string& fileName, const std::string& tab = "", const std::string& newLine = "") const
{
std::ofstream ofs(fileName);
ofs << toString(tab, newLine);
}
// Getters
Type getType() const { return type; }
private:
Type type = Type::Null;
union
{
std::string str;
bool b;
double num;
JsonArr arr;
JsonObj obj; // TODO: add a map key to index, and change JsonObj to a map of index to Pair<string, Json>
};
void get() const {}
static bool tryGet() { return true; }
void set() {}
void checkKeyType(const std::string& key, Type expectedType) const
{
if (expectedType == Type::Null) return; // allow any type
if (type != expectedType)
throw std::runtime_error(
"Expected " + typeToString(expectedType) + " but got " + typeToString(type) + " for key '" + key + "'");
}
std::ostream& displayAsObject(
std::ostream& os, size_t currentTabCount, const std::string& tab, const std::string& newLine) const
{
if (obj.empty()) return os << "{}";
size_t newTabCount = currentTabCount + 1;
const auto& newTab = getTab(tab, newTabCount);
auto it = obj.begin();
auto beforeEnd = obj.size() - 1;
os << "{";
for (size_t i = 0; i < beforeEnd; ++i, ++it)
it->second.display(os << newLine << newTab << '"' << it->first << "\": ", tab, newLine, newTabCount) << ", ";
return it->second.display(os << newLine << newTab << '"' << it->first << "\": ", tab, newLine, newTabCount)
<< newLine << getTab(tab, currentTabCount) << "}";
}
std::ostream& displayAsArray(
std::ostream& os, size_t currentTabCount, const std::string& tab, const std::string& newLine) const
{
if (arr.empty()) return os << "[]";
size_t newTabCount = currentTabCount + 1;
const auto& newTab = getTab(tab, newTabCount);
auto it = arr.begin();
auto beforeEnd = arr.size() - 1;
os << "[";
for (size_t i = 0; i < beforeEnd; ++i, ++it) it->display(os << newLine << newTab, tab, newLine, newTabCount) << ", ";
return it->display(os << newLine << newTab, tab, newLine, newTabCount)
<< newLine << getTab(tab, currentTabCount) << "]";
}
};
namespace detail
{
inline void skipSpace(const std::string_view& str, size_t& pos)
{
while (pos < str.size() && std::isspace(str[pos])) ++pos;
}
inline void parseChar(const std::string_view& str, size_t& pos, char c)
{
if (pos >= str.size() || str[pos] != c)
throw std::runtime_error("Expected '" + std::string(1, c) + "' at position " + std::to_string(pos));
++pos;
}
inline void parseHex(const std::string_view& str, size_t& pos)
{
if (!std::isxdigit(str[pos])) throw std::runtime_error("Expected hex digit at position " + std::to_string(pos));
++pos;
}
inline void parseEscape(const std::string_view& str, size_t& pos)
{
switch (str[pos])
{
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
++pos;
break;
case 'u':
++pos;
for (size_t i = 0; i < 4; ++i) parseHex(str, pos);
break;
default:
throw std::runtime_error("Invalid escape character at position " + std::to_string(pos));
}
}
inline void parseString(const std::string_view& str, size_t& pos, std::string& value)
{
size_t start = pos;
while (str[pos] != '"' && pos < str.size())
{
if (str[pos] == '\\')
{
++pos;
parseEscape(str, pos);
}
else if (str[pos] == '\n' || str[pos] == '\t' || str[pos] == '\r')
throw std::runtime_error("Invalid character in string at position " + std::to_string(pos));
else
++pos;
}
value = str.substr(start, pos - start);
parseChar(str, pos, '"');
}
inline void parseDigits(const std::string_view& str, size_t& pos)
{
if (!std::isdigit(str[pos])) throw std::runtime_error("Invalid number at position " + std::to_string(pos));
while (std::isdigit(str[pos])) ++pos;
}
inline void parseExponent(const std::string_view& str, size_t& pos)
{
++pos;
if (str[pos] == '+' || str[pos] == '-') ++pos;
parseDigits(str, pos);
}
inline void parseDecimal(const std::string_view& str, size_t& pos)
{
++pos;
parseDigits(str, pos);
if (str[pos] == 'e' || str[pos] == 'E') parseExponent(str, pos);
}
inline void parseNumber(const std::string_view& str, size_t& pos, double& value)
{
size_t start = pos;
if (str[pos] == '-') ++pos;
if (str[pos] == '0')
{
++pos;
if (str[pos] == '.') parseDecimal(str, pos);
else if (str[pos] == 'e' || str[pos] == 'E')
parseExponent(str, pos);
}
else
{
parseDigits(str, pos);
if (str[pos] == '.') parseDecimal(str, pos);
else if (str[pos] == 'e' || str[pos] == 'E')
parseExponent(str, pos);
}
#ifdef __cpp_lib_to_chars
std::from_chars(str.data() + start, str.data() + pos, value);
#else
value = std::stod(std::string(str.substr(start, pos - start)));
#endif
}
inline void parseObject(const std::string_view& str, size_t& pos, Json& jsonValue, size_t depth)
{
skipSpace(str, pos);
if (str[pos] == '}') jsonValue = JsonObj();
while (pos < str.size() && str[pos] != '}')
{
parseChar(str, pos, '"');
std::string key;
parseString(str, pos, key);
skipSpace(str, pos);
parseChar(str, pos, ':');
Json value;
parseValue(str, pos, value, depth + 1);
jsonValue[key] = value;
if (str[pos] == '}') break;
parseChar(str, pos, ',');
skipSpace(str, pos);
if (str[pos] == '}') throw std::runtime_error("Extra comma at position " + std::to_string(pos));
}
parseChar(str, pos, '}');
}
inline void parseArray(const std::string_view& str, size_t& pos, Json& jsonValue, size_t depth)
{
size_t i = 0;
if (str[pos] == ']') jsonValue = JsonArr();
while (pos < str.size() && str[pos] != ']')
{
Json value;
parseValue(str, pos, value, depth + 1);
jsonValue[i++] = value;
if (str[pos] == ']') break;
parseChar(str, pos, ',');
skipSpace(str, pos);
if (str[pos] == ']') throw std::runtime_error("Extra comma at position " + std::to_string(pos));
}
parseChar(str, pos, ']');
}
} // namespace detail
inline void parseValue(const std::string_view& str, size_t& pos, Json& jsonValue, size_t depth)
{
using namespace detail;
if (depth == MAX_JSON_DEPTH) throw std::runtime_error("Exceeded maximum depth of " + std::to_string(MAX_JSON_DEPTH));
skipSpace(str, pos);
std::string s;
double d = 0.0;
switch (str[pos])
{
case 'n':
pos++;
parseChar(str, pos, 'u');
parseChar(str, pos, 'l');
parseChar(str, pos, 'l');
break;
case 't':
pos++;
jsonValue = true;
parseChar(str, pos, 'r');
parseChar(str, pos, 'u');
parseChar(str, pos, 'e');
break;
case 'f':
pos++;
jsonValue = false;
parseChar(str, pos, 'a');
parseChar(str, pos, 'l');
parseChar(str, pos, 's');
parseChar(str, pos, 'e');
break;
case '"':
pos++;
parseString(str, pos, s);
jsonValue = s;
break;
case '[':
pos++;
parseArray(str, pos, jsonValue, depth);
break;
case '{':
pos++;
parseObject(str, pos, jsonValue, depth);
break;
default:
parseNumber(str, pos, d);
jsonValue = d;
break;
}
skipSpace(str, pos);
}
#ifdef USE_BSTT_NAMESPACE
} // namespace bstt
#endif