-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.cpp
417 lines (397 loc) · 7.96 KB
/
import.cpp
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
#include <sstream>
#include <stdexcept>
#include <cmath>
#include <limits>
#include <codecvt>
#include <locale>
namespace tokox::json
{
namespace
{
template<class IT>
IT& mov(IT& it)
{
it++;
return it;
}
template<class IT>
IT& put(IT& it, char c, int t = 1)
{
while (t-- > 0)
{
(*it) = c;
it++;
}
return it;
}
template<class IT>
IT& put(IT& it, std::string s, int t = 1)
{
while (t-- > 0)
{
for (auto& c : s)
put(it, c);
}
return it;
}
template<class IT>
char get(IT& it, IT end)
{
if (it == end)
throw parse_error(parse_error::UNEXPECTED_END);
return *it;
}
template<class IT>
char getmv(IT& it, IT end)
{
char c = get(it, end);
mov(it);
return c;
}
template<class IT>
void eat_ws(IT& it, IT end)
{
while (get(it, end) == ' ' || get(it, end) == '\n' || get(it, end) == '\r' || get(it, end) == '\t')
mov(it);
}
}
template<class IT>
object parse_null(IT& it, IT end)
{
eat_ws(it, end);
for (int i = 0; i < 4; i++)
{
if (getmv(it, end) != "null"[i])
throw parse_error(parse_error::INCORRECT_VALUE);
}
return object();
}
template<class IT>
object parse_bool(IT& it, IT end)
{
eat_ws(it, end);
char c = getmv(it, end);
if (c == 't')
{
for (int i = 1; i < 4; i++)
{
if (getmv(it, end) != "true"[i])
throw parse_error(parse_error::INCORRECT_VALUE);
}
return object(true);
}
else if (c == 'f')
{
for (int i = 1; i < 5; i++)
{
if (getmv(it, end) != "false"[i])
throw parse_error(parse_error::INCORRECT_VALUE);
}
return object(false);
}
else
throw parse_error(parse_error::INCORRECT_VALUE);
}
template<class IT>
object parse_number(IT& it, IT end)
{
eat_ws(it, end);
bool minus = false;
std::string num;
if (get(it, end) == '-')
{
minus = true;
mov(it);
}
if (get(it, end) == '0')
{
num += '0';
mov(it);
}
else if (get(it, end) >= '1' && get(it, end) <= '9')
{
do
num += getmv(it, end);
while (it != end && get(it, end) >= '0' && get(it, end) <= '9');
}
else
throw parse_error(parse_error::INCORRECT_VALUE);
std::string apt;
if (it != end && get(it, end) == '.')
{
mov(it);
if (get(it, end) >= '0' && get(it, end) <= '9')
{
do
apt += getmv(it, end);
while (it != end && get(it, end) >= '0' && get(it, end) <= '9');
}
else
throw parse_error(parse_error::INCORRECT_VALUE);
}
std::string exp;
long long expll;
if (it != end && (get(it, end) == 'E' || get(it, end) == 'e'))
{
mov(it);
if (get(it, end) == '+')
mov(it);
else if (get(it, end) == '-')
exp += getmv(it, end);
if (get(it, end) >= '0' && get(it, end) <= '9')
{
do
exp += getmv(it, end);
while (it != end && get(it, end) >= '0' && get(it, end) <= '9');
}
else
throw parse_error(parse_error::INCORRECT_VALUE);
try
{
expll = std::stoll(exp);
}
catch (const std::out_of_range& e)
{
throw parse_error(parse_error::VALUE_OUT_OF_RANGE);
}
}
else
{
exp = "0";
expll = 0;
}
int max_dgt = (int) std::log10(std::numeric_limits<long long>::max()) + 1;
if (expll + num.size() > (size_t) max_dgt || expll + num.size() < 1)
{
try
{
return object((minus ? -1 : 1) * std::stold(num + "." + apt + "e" + exp));
}
catch (const std::out_of_range& e)
{
throw parse_error(parse_error::VALUE_OUT_OF_RANGE);
}
}
if (expll > 0)
{
if (num.size() == 1 && num[0] == '0')
num.clear();
num += apt.substr(0, std::min(expll, (long long) apt.size()));
for (int i = 0; (size_t) i < expll - apt.size(); i++)
num += '0';
apt.erase(0, std::min(expll, (long long) apt.size()));
}
else
{
apt.insert(0, num.substr(num.size() + expll, -expll));
num.erase(num.size() + expll, -expll);
}
long long resultll;
long double resultld;
try
{
resultll = std::stoll(num);
}
catch (const std::out_of_range& e)
{
try
{
resultld = std::stold(num + "." + apt);
}
catch (const std::out_of_range& e2)
{
throw parse_error(parse_error::VALUE_OUT_OF_RANGE);
}
return object((minus ? -1 : 1) * resultld);
}
if (apt.empty())
return object((minus ? -1 : 1) * resultll);
else if (apt[0] >= '5')
resultll++;
try
{
resultld = std::stold(num + "." + apt);
}
catch (const std::out_of_range& e)
{
return object((minus ? -1 : 1) * resultll);
}
if (std::llround(resultld) == resultll)
return object((minus ? -1 : 1) * resultld);
return object((minus ? -1 : 1) * resultll);
}
template<class IT>
object parse_string(IT& it, IT end)
{
eat_ws(it, end);
if (getmv(it, end) != '"')
throw parse_error(parse_error::INCORRECT_VALUE);
std::string result;
char c;
while ((c = getmv(it, end)) != '"')
{
if (c == '\\')
{
switch (c = getmv(it, end))
{
case '"':
case '\\':
case '/':
result += c;
break;
case 'b':
result += '\b';
break;
case 'f':
result += '\f';
break;
case 'n':
result += '\n';
break;
case 'r':
result += '\r';
break;
case 't':
result += '\t';
break;
case 'u':
{
unsigned int codepoint = 0;
for (int i = 0; i < 4; i++)
{
c = getmv(it, end);
if (c >= '0' && c <= '9')
codepoint = codepoint * 16 + c - '0';
else if (c >= 'A' && c <= 'F')
codepoint = codepoint * 16 + c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
codepoint = codepoint * 16 + c - 'a' + 10;
else
throw parse_error(parse_error::INCORRECT_VALUE);
}
result += std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t>().to_bytes(codepoint);
}
break;
default:
throw parse_error(parse_error::INCORRECT_VALUE);
}
}
else if (c >= ' ' && c != 127) // TODO: check utf-8 too
{
result += c;
}
else
throw parse_error(parse_error::INCORRECT_VALUE);
}
return object(result);
}
template<class IT>
object parse_vector(IT& it, IT end)
{
eat_ws(it, end);
if (getmv(it, end) != '[')
throw parse_error(parse_error::INCORRECT_VALUE);
object_type_from_value_type<Vector> result;
while (get(it, end) != ']')
{
eat_ws(it, end);
result.push_back(parse(it, end));
eat_ws(it, end);
char c;
if ((c = get(it, end)) == ']')
break;
else if (c != ',')
throw parse_error(parse_error::INCORRECT_VALUE);
else
mov(it);
}
mov(it);
return object(result);
}
template<class IT>
object parse_map(IT& it, IT end)
{
eat_ws(it, end);
if (getmv(it, end) != '{')
throw parse_error(parse_error::INCORRECT_VALUE);
object_type_from_value_type<Map> result;
while (get(it, end) != '}')
{
eat_ws(it, end);
std::string key = parse_string(it, end);
if (result.contains(key))
throw parse_error(parse_error::REPEATED_MAP_KEY);
eat_ws(it, end);
if (getmv(it, end) != ':')
throw parse_error(parse_error::INCORRECT_VALUE);
eat_ws(it, end);
object value = parse(it, end);
result[key] = value;
eat_ws(it, end);
char c;
if ((c = get(it, end)) == '}')
break;
else if (c != ',')
throw parse_error(parse_error::INCORRECT_VALUE);
else
mov(it);
}
mov(it);
return object(result);
}
template<class IT>
object parse(IT& it, IT end)
{
eat_ws(it, end);
switch (get(it, end))
{
case 'n':
return parse_null(it, end);
case 't':
case 'f':
return parse_bool(it, end);
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return parse_number(it, end);
case '"':
return parse_string(it, end);
case '[':
return parse_vector(it, end);
case '{':
return parse_map(it, end);
}
throw parse_error(parse_error::INCORRECT_VALUE);
}
object parse(const std::string& json_str)
{
std::string::const_iterator jsit(json_str.begin());
return parse(jsit, json_str.end());
}
object parse(std::istream& json_stream)
{
std::istreambuf_iterator<char> jsit(json_stream);
return parse(jsit, std::istreambuf_iterator<char>());
}
object& parse(object& obj, const std::string& json_str)
{
return (obj = parse(json_str));
}
object& parse(object& obj, std::istream& json_stream)
{
return (obj = parse(json_stream));
}
std::istream& operator>>(std::istream& json_stream, object& obj)
{
parse(obj, json_stream);
return json_stream;
}
}