-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
json
456 lines (440 loc) · 22 KB
/
json
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_FILE_TEXT_JSON_HPP
#define GTL_FILE_TEXT_JSON_HPP
// Summary: A small json parser and composer. [wip]
#if defined(_MSC_VER)
#pragma warning(push, 0)
#endif
#include <cstdio>
#include <cstdlib>
#include <map>
#include <string>
#include <vector>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
namespace gtl {
class json final {
public:
class value final {
public:
using null_type = decltype(nullptr);
using bool_type = bool;
using number_type = double;
using string_type = std::string;
using object_type = std::map<value::string_type, value>;
using array_type = std::vector<value>;
private:
enum class contains {
null_type,
bool_type,
number_type,
string_type,
object_type,
array_type
};
private:
contains type;
union {
null_type null_value;
bool_type bool_value;
number_type number_value;
string_type string_value;
object_type object_value;
array_type array_value;
};
public:
~value() {
switch (this->type) {
case contains::null_type : { null_value.~null_type(); } break;
case contains::bool_type : { bool_value.~bool_type(); } break;
case contains::number_type: { number_value.~number_type(); } break;
case contains::string_type: { string_value.~string_type(); } break;
case contains::object_type: { object_value.~object_type(); } break;
case contains::array_type : { array_value.~array_type(); } break;
}
}
value()
: type(contains::null_type) {
new (&null_value) null_type(nullptr);
}
value(const value& other)
: type(other.type) {
switch (this->type) {
case contains::null_type : { new (&null_value) null_type(other.null_value); } break;
case contains::bool_type : { new (&bool_value) bool_type(other.bool_value); } break;
case contains::number_type: { new (&number_value) number_type(other.number_value); } break;
case contains::string_type: { new (&string_value) string_type(other.string_value); } break;
case contains::object_type: { new (&object_value) object_type(other.object_value); } break;
case contains::array_type : { new (&array_value) array_type(other.array_value); } break;
}
}
value(value&& other)
: type(other.type) {
switch (this->type) {
case contains::null_type : { new (&null_value) null_type(static_cast<null_type&&>(other.null_value)); } break;
case contains::bool_type : { new (&bool_value) bool_type(static_cast<bool_type&&>(other.bool_value)); } break;
case contains::number_type: { new (&number_value) number_type(static_cast<number_type&&>(other.number_value)); } break;
case contains::string_type: { new (&string_value) string_type(static_cast<string_type&&>(other.string_value)); } break;
case contains::object_type: { new (&object_value) object_type(static_cast<object_type&&>(other.object_value)); } break;
case contains::array_type : { new (&array_value) array_type(static_cast<array_type&&>(other.array_value)); } break;
}
}
value& operator=(const value& other) {
switch (this->type) {
case contains::null_type : { null_value.~null_type(); } break;
case contains::bool_type : { bool_value.~bool_type(); } break;
case contains::number_type: { number_value.~number_type(); } break;
case contains::string_type: { string_value.~string_type(); } break;
case contains::object_type: { object_value.~object_type(); } break;
case contains::array_type : { array_value.~array_type(); } break;
}
this->type = other.type;
switch (this->type) {
case contains::null_type : { new (&null_value) null_type(other.null_value); } break;
case contains::bool_type : { new (&bool_value) bool_type(other.bool_value); } break;
case contains::number_type: { new (&number_value) number_type(other.number_value); } break;
case contains::string_type: { new (&string_value) string_type(other.string_value); } break;
case contains::object_type: { new (&object_value) object_type(other.object_value); } break;
case contains::array_type : { new (&array_value) array_type(other.array_value); } break;
}
return *this;
}
value& operator=(value&& other) {
switch (this->type) {
case contains::null_type : { null_value.~null_type(); } break;
case contains::bool_type : { bool_value.~bool_type(); } break;
case contains::number_type: { number_value.~number_type(); } break;
case contains::string_type: { string_value.~string_type(); } break;
case contains::object_type: { object_value.~object_type(); } break;
case contains::array_type : { array_value.~array_type(); } break;
}
this->type = other.type;
switch (this->type) {
case contains::null_type : { new (&null_value) null_type(static_cast<null_type&&>(other.null_value)); } break;
case contains::bool_type : { new (&bool_value) bool_type(static_cast<bool_type&&>(other.bool_value)); } break;
case contains::number_type: { new (&number_value) number_type(static_cast<number_type&&>(other.number_value)); } break;
case contains::string_type: { new (&string_value) string_type(static_cast<string_type&&>(other.string_value)); } break;
case contains::object_type: { new (&object_value) object_type(static_cast<object_type&&>(other.object_value)); } break;
case contains::array_type : { new (&array_value) array_type(static_cast<array_type&&>(other.array_value)); } break;
}
return *this;
}
value(null_type value) : type(contains::null_type) { new (&null_value) null_type(value); }
value(bool_type value) : type(contains::bool_type) { new (&bool_value) bool_type(value); }
value(number_type value) : type(contains::number_type) { new (&number_value) number_type(value); }
value(const string_type& value) : type(contains::string_type) { new (&string_value) string_type(value); }
value(const object_type& value) : type(contains::object_type) { new (&object_value) object_type(value); }
value(const array_type& value) : type(contains::array_type) { new (&array_value) array_type(value); }
private:
template <typename contains_type>
class convert_to_contains_enum;
public:
template <typename is_type>
bool is() const {
return (this->type == convert_to_contains_enum<is_type>::type);
}
public:
template <typename as_type>
const as_type& as() const {
if constexpr (convert_to_contains_enum<as_type>::type == contains::null_type) { return null_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::bool_type) { return bool_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::number_type) { return number_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::string_type) { return string_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::object_type) { return object_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::array_type) { return array_value; }
}
template <typename as_type>
as_type& as() {
if constexpr (convert_to_contains_enum<as_type>::type == contains::null_type) { return null_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::bool_type) { return bool_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::number_type) { return number_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::string_type) { return string_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::object_type) { return object_value; }
else if constexpr (convert_to_contains_enum<as_type>::type == contains::array_type) { return array_value; }
}
public:
string_type to_string() const {
constexpr static const auto replace = [](string_type& string, const string_type& from, const string_type& to){
string_type::size_type position = 0;
while(true) {
string_type::size_type start = string.find(from, position);
if(start == string_type::npos) {
return;
}
string.replace(start, from.size(), to);
position += to.size();
}
};
switch (this->type) {
case contains::null_type : {
return "null";
} break;
case contains::bool_type : {
if (bool_value) {
return "true";
}
return "false";
} break;
case contains::number_type: {
char buffer[25] = {};
const int length = std::snprintf(&buffer[0], 25, "%g", number_value);
if ((length < 0) || (length >= 25)) {
return "";
}
return std::string(&buffer[0]);
} break;
case contains::string_type: {
string_type string = this->string_value;
replace(string, "\\", "\\\\");
replace(string, "\b", "\\\b");
replace(string, "\f", "\\\f");
replace(string, "\n", "\\\n");
replace(string, "\r", "\\\r");
replace(string, "\t", "\\\t");
replace(string, "\"", "\\\"");
return "\"" + string + "\"";
} break;
case contains::object_type: {
string_type string = "{";
for (const auto& [key, item] : this->object_value) {
string_type key_escaped = key;
replace(key_escaped, "\\", "\\\\");
replace(key_escaped, "\b", "\\\b");
replace(key_escaped, "\f", "\\\f");
replace(key_escaped, "\n", "\\\n");
replace(key_escaped, "\r", "\\\r");
replace(key_escaped, "\t", "\\\t");
replace(key_escaped, "\"", "\\\"");
if (string.size() == 1) {
string += "\"" + key_escaped + "\":" + item.to_string();
}
else {
string += ",\"" + key_escaped + "\":" + item.to_string();
}
}
return string + "}";
} break;
case contains::array_type : {
string_type string = "[";
for (const auto& value : this->array_value) {
if (string.size() == 1) {
string += value.to_string();
}
else {
string += "," + value.to_string();
}
}
return string + "]";
} break;
}
return "";
}
};
private:
value root;
private:
value parse_recursive(const std::string& data, std::string::size_type& index) {
skip_whitespace(data, index);
if (parse_null(data, index)) { return value(nullptr); }
value::bool_type boolean;
if (parse_bool(data, index, boolean)) { return value(boolean); }
value::number_type number;
if (parse_number(data, index, number)) { return value(number); }
value::string_type string;
if (parse_string(data, index, string)) { return value(string); }
value::object_type object;
if (parse_object(data, index, object)) { return value(object); }
value::array_type array;
if (parse_array(data, index, array)) { return value(array); }
return value(nullptr);
}
void skip_whitespace(const std::string& data, std::string::size_type& index) {
for (; index < data.size(); ++index) {
switch (data[index]) {
case '\f': break;
case '\n': break;
case '\r': break;
case '\t': break;
case ' ': break;
default: return;
}
}
}
bool parse_null(const std::string& data, std::string::size_type& index) {
if (data.substr(index, 4) == "null") {
index += 4;
return true;
}
return false;
}
bool parse_bool(const std::string& data, std::string::size_type& index, value::bool_type& boolean) {
if (data.substr(index, 4) == "true") {
boolean = true;
index += 4;
return true;
}
if (data.substr(index, 5) == "false") {
boolean = false;
index += 5;
return true;
}
return false;
}
bool parse_number(const std::string& data, std::string::size_type& index, value::number_type& number) {
std::string valid_number_characters = "0123456789-+eE.";
std::string::size_type length = 0;
while ((index + length < data.size()) && (valid_number_characters.find(data[index + length]) < valid_number_characters.size())) {
++length;
}
if (length == 0) {
return false;
}
char* end_pointer = nullptr;
std::string number_string = data.substr(index, length);
number = std::strtod(number_string.c_str(), &end_pointer);
if ((end_pointer != nullptr) && (*end_pointer == 0)) {
index += length;
return true;
}
return false;
}
bool parse_string(const std::string& data, std::string::size_type& index, value::string_type& string) {
if (data[index] != '"') {
return false;
}
std::string::size_type length = 1;
while ((index + length < data.size()) && ((data[index + length] != '"') || ((data[index + length - 1] == '\\') && (data[index + length] == '"')))) {
if (data[index + length] == '\b') {
return false;
}
if (data[index + length] == '\f') {
return false;
}
if (data[index + length] == '\n') {
return false;
}
if (data[index + length] == '\r') {
return false;
}
if (data[index + length] == '\t') {
return false;
}
++length;
}
if (index + length == data.size()) {
return false;
}
string = data.substr(index + 1, length - 1);
index += length + 1;
return true;
}
bool parse_object(const std::string& data, std::string::size_type& index, value::object_type& object) {
if (data[index] != '{') {
return false;
}
std::string::size_type index_object = index + 1;
object = value::object_type();
if (index_object >= data.size()) {
return false;
}
if (data[index_object] == '}') {
index = index_object + 1;
return true;
}
while (index_object < data.size()) {
skip_whitespace(data, index_object);
value::string_type key;
if (!parse_string(data, index_object, key)) {
return false;
}
skip_whitespace(data, index_object);
if ((index_object >= data.size()) || (data[index_object] != ':')) {
return false;
}
++index_object;
skip_whitespace(data, index_object);
value item = parse_recursive(data, index_object);
object.insert({key, item});
skip_whitespace(data, index_object);
if (index_object >= data.size()) {
return false;
}
if (data[index_object] == '}') {
index = index_object + 1;
return true;
}
if (data[index_object] != ',') {
return false;
}
++index_object;
}
return false;
}
bool parse_array(const std::string& data, std::string::size_type& index, value::array_type& array) {
if (data[index] != '[') {
return false;
}
std::string::size_type index_array = index + 1;
array = value::array_type();
if (index_array >= data.size()) {
return false;
}
if (data[index_array] == ']') {
index = index_array + 1;
return true;
}
while (index_array < data.size()) {
skip_whitespace(data, index_array);
value item = parse_recursive(data, index_array);
array.push_back(item);
skip_whitespace(data, index_array);
if (index_array >= data.size()) {
return false;
}
if (data[index_array] == ']') {
index = index_array + 1;
return true;
}
if (data[index_array] != ',') {
return false;
}
++index_array;
}
return false;
}
public:
bool parse(const std::string& data) {
std::string::size_type index = 0;
this->root = parse_recursive(data, index);
return (index == data.size());
}
std::string compose() const {
return this->root.to_string();
}
value& document() {
return this->root;
}
};
template <typename contains_type>
class json::value::convert_to_contains_enum<const contains_type> final { public: constexpr static const json::value::contains type = json::value::convert_to_contains_enum<contains_type>::type; };
template <> class json::value::convert_to_contains_enum<json::value::null_type> final { public: constexpr static const json::value::contains type = json::value::contains::null_type; };
template <> class json::value::convert_to_contains_enum<json::value::bool_type> final { public: constexpr static const json::value::contains type = json::value::contains::bool_type; };
template <> class json::value::convert_to_contains_enum<json::value::number_type> final { public: constexpr static const json::value::contains type = json::value::contains::number_type; };
template <> class json::value::convert_to_contains_enum<json::value::string_type> final { public: constexpr static const json::value::contains type = json::value::contains::string_type; };
template <> class json::value::convert_to_contains_enum<json::value::object_type> final { public: constexpr static const json::value::contains type = json::value::contains::object_type; };
template <> class json::value::convert_to_contains_enum<json::value::array_type> final { public: constexpr static const json::value::contains type = json::value::contains::array_type; };
}
#endif // GTL_FILE_TEXT_JSON_HPP