-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
389 lines (372 loc) · 10.6 KB
/
object.cpp
File metadata and controls
389 lines (372 loc) · 10.6 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
#include <utility>
#include <json/object.h>
// Constructors
json::object::object() noexcept(true)
: type(json::TYPE::UNDEFINED), value(nullptr) {
return;
}
json::object::object(json::object::AllValues data) noexcept(false)
: type(json::TYPE::UNDEFINED), value(nullptr) {
this->reset();
switch (data.index()) {
case 0:
this->value = std::get<std::nullptr_t>(data);
this->type = json::TYPE::NUL;
break;
case 1:
this->value = std::get<bool>(data);
this->type = json::TYPE::BOOLEAN;
break;
case 2:
this->value = std::get<long long int>(data);
this->type = json::TYPE::INTEGER;
break;
case 3:
this->value = std::get<double>(data);
this->type = json::TYPE::NUMBER;
break;
case 4:
this->value = std::get<std::string>(data);
this->type = json::TYPE::STRING;
break;
case 5:
this->array = std::get<std::vector<json::object>>(data);
this->type = json::TYPE::ARRAY;
break;
case 6:
this->map = std::get<std::map<std::string, json::object>>(data);
this->type = json::TYPE::OBJECT;
break;
default:
logger::error("The data type is not supported",
"json::object::object(json::object::AllValues data)");
break;
}
return;
}
json::object::object(const char *data) noexcept(false)
: type(json::TYPE::UNDEFINED), value(nullptr) {
*this = std::string(data);
return;
}
json::object::~object() noexcept(true) {
this->clearVariant();
return;
}
// Assignment operators
json::object &json::object::operator=(std::nullptr_t data) noexcept(true) {
return *this = json::object(data);
}
json::object &json::object::operator=(bool data) noexcept(true) {
return *this = json::object(data);
}
json::object &json::object::operator=(int data) noexcept(true) {
return *this = json::object(data);
}
json::object &json::object::operator=(long long int data) noexcept(true) {
return *this = json::object(data);
}
json::object &json::object::operator=(double data) noexcept(true) {
return *this = json::object(data);
}
json::object &json::object::operator=(const char *data) noexcept(true) {
return *this = json::object(data);
}
json::object &json::object::operator=(const std::string &data) noexcept(true) {
return *this = json::object(data);
}
json::object &
json::object::operator=(std::vector<json::object> data) noexcept(true) {
return *this = json::object(data);
}
json::object &json::object::operator=(
std::map<std::string, json::object> data) noexcept(true) {
return *this = json::object(data);
}
// Array operations
json::object &json::object::operator[](size_t index) noexcept(false) {
this->assertIsArray();
if (index >= this->array.size()) {
this->array.resize(index + 1);
}
return this->array[index];
}
json::object &json::object::operator[](int index) noexcept(false) {
return this->operator[]((size_t)index);
}
void json::object::push_back(const json::object &data) noexcept(false) {
this->assertIsArray();
this->array.push_back(data);
return;
}
void json::object::pop_back() noexcept(false) {
this->assertIsArray();
this->array.pop_back();
return;
}
void json::object::reserve(size_t size) noexcept(false) {
this->assertIsArray();
this->array.reserve(size);
return;
}
void json::object::resize(size_t size) noexcept(false) {
this->assertIsArray();
this->array.resize(size);
return;
}
void json::object::shrink_to_fit() noexcept(false) {
this->assertIsArray();
this->array.shrink_to_fit();
return;
}
// Map operations
json::object &json::object::operator[](const char *key) noexcept(false) {
return this->operator[](std::string(key));
}
json::object &json::object::operator[](const std::string &key) noexcept(false) {
this->assertIsMap();
return this->map[key];
}
std::map<std::string, json::object>::iterator
json::object::find(const std::string key) noexcept(false) {
this->assertIsMap();
return this->map.find(key);
}
std::map<std::string, json::object>::iterator
json::object::begin() noexcept(false) {
this->assertIsMap();
return this->map.begin();
}
std::map<std::string, json::object>::iterator
json::object::end() noexcept(false) {
this->assertIsMap();
return this->map.end();
}
void json::object::insert(const std::string key,
json::object::AllValues value) noexcept(false) {
this->assertIsMap();
this->map[key] = json::object(value);
return;
}
// Array and Map operators
void json::object::clear() noexcept(false) {
if (this->type == json::TYPE::ARRAY) {
this->clearArray();
} else if (this->type == json::TYPE::OBJECT) {
this->clearMap();
} else {
logger::error("The object is not an array or a map",
"void json::object::clear()");
}
return;
}
size_t json::object::size() const noexcept(false) {
if (this->type == json::TYPE::ARRAY) {
return this->array.size();
} else if (this->type == json::TYPE::OBJECT) {
return this->map.size();
} else if (this->type == json::TYPE::STRING) {
return std::get<std::string>(this->value).size();
} else {
logger::error("The object is not a string, array, or map",
"size_t json::object::size()");
}
return 0;
}
// Object operators
void json::object::reset() noexcept(true) {
this->clearVariant();
this->clearArray();
this->clearMap();
this->type = json::TYPE::UNDEFINED;
return;
}
void json::object::dump(std::string filename, size_t indent) const
noexcept(false) {
std::ofstream file;
try {
file.open(filename);
if (!file.is_open()) {
logger::error(
"Failed to open file " + filename,
"void json::object::dump(std::string filename, size_t indent)");
}
file << this->dumps(indent);
file.close();
} catch (const std::exception &e) {
logger::error(
"Error encountered for: " + filename + " - " + std::string(e.what()),
"void json::object::dump(std::string filename, size_t indent)");
} catch (...) {
logger::error(
"An unknown error occurred for file: " + filename,
"void json::object::dump(std::string filename, size_t indent)");
}
return;
}
std::string json::object::dumps(size_t indent, size_t baseIndent) const
noexcept(false) {
std::string result = "";
int newBaseIndent = indent + baseIndent;
switch (this->type) {
case json::TYPE::UNDEFINED:
result = "undefined";
break;
case json::TYPE::NUL:
result = "null";
break;
case json::TYPE::BOOLEAN:
result = std::get<bool>(this->value) ? "true" : "false";
break;
case json::TYPE::INTEGER:
result = std::to_string(std::get<long long int>(this->value));
break;
case json::TYPE::NUMBER:
result = std::to_string(std::get<double>(this->value));
break;
case json::TYPE::STRING:
result = "\"" + std::get<std::string>(this->value) + "\"";
break;
case json::TYPE::ARRAY:
result = "[";
if (this->size() == 0) {
result += "]";
return result;
}
if (indent > 0)
result += "\n";
for (size_t i = 0; i < this->size(); i++) {
result += std::string(newBaseIndent, ' ');
result += this->array[i].dumps(indent, newBaseIndent);
if (i + 1 != this->size())
result += ",";
if (indent > 0)
result += "\n";
else if (i + 1 != this->size())
result += " ";
}
result += std::string(baseIndent, ' ') + "]";
break;
case json::TYPE::OBJECT:
result = "{";
if (this->size() == 0) {
result += "}";
return result;
}
if (indent > 0)
result += "\n";
for (std::map<std::string, json::object>::const_iterator it =
this->map.begin();
it != this->map.end(); it++) {
result += std::string(newBaseIndent, ' ');
result += "\"" + it->first + "\": ";
result += it->second.dumps(indent, newBaseIndent);
if (std::next(it) != this->map.end())
result += ",";
if (indent > 0)
result += "\n";
else if (std::next(it) != this->map.end())
result += " ";
}
result += std::string(baseIndent, ' ') + "}";
}
return result;
}
// Conversion Operators
json::object::operator std::string() const noexcept(false) {
if (this->type == json::TYPE::STRING) {
return std::get<std::string>(this->value);
} else {
logger::error("The object is not a string",
"json::object::operator std::string()");
}
return "";
}
json::object::operator int() const noexcept(false) {
if (this->type == json::TYPE::INTEGER) {
return (int)std::get<long long int>(this->value);
} else {
logger::error("The object is not an integer",
"json::object::operator int()");
}
return 0;
}
json::object::operator long long int() const noexcept(false) {
if (this->type == json::TYPE::INTEGER) {
return (long long int)std::get<long long int>(this->value);
} else {
logger::error("The object is not an integer",
"json::object::operator long long int()");
}
return 0;
}
json::object::operator double() const noexcept(false) {
if (this->type == json::TYPE::NUMBER) {
return std::get<double>(this->value);
} else {
logger::error("The object is not a number",
"json::object::operator double()");
}
return 0.0;
}
json::object::operator bool() const noexcept(false) {
if (this->type == json::TYPE::BOOLEAN) {
return std::get<bool>(this->value);
} else {
logger::error("The object is not a boolean",
"json::object::operator bool()");
}
return false;
}
// Private methods
void json::object::clearVariant() noexcept(true) {
this->value = nullptr;
return;
}
void json::object::clearArray() noexcept(true) {
this->array.clear();
return;
}
void json::object::setArrayIfUndefined() noexcept(true) {
if (this->type == json::TYPE::UNDEFINED) {
this->clearVariant();
this->clearMap();
this->type = json::TYPE::ARRAY;
}
return;
}
void json::object::assertIsArray() noexcept(false) {
this->setArrayIfUndefined();
if (this->type != json::TYPE::ARRAY) {
logger::error("The object is not an array",
"void json::object::assertIsArray()");
}
return;
}
void json::object::clearMap() noexcept(true) {
this->map.clear();
return;
}
void json::object::setMapIfUndefined() noexcept(true) {
if (this->type == json::TYPE::UNDEFINED) {
this->clearVariant();
this->clearArray();
this->type = json::TYPE::OBJECT;
}
return;
}
void json::object::assertIsMap() noexcept(false) {
this->setMapIfUndefined();
if (this->type != json::TYPE::OBJECT) {
logger::error("The object is not a map",
"void json::object::assertIsMap()");
}
return;
}
// Friend functions
namespace json {
std::ostream &operator<<(std::ostream &os, object &obj) noexcept(true) {
os << obj.dumps(2);
return os;
}
} // namespace json