-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.cpp
352 lines (315 loc) · 9.17 KB
/
json.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
#include <iterator>
#include "json.h"
namespace json {
namespace {
using namespace std::literals;
Node LoadNode(std::istream& input);
Node LoadString(std::istream& input);
std::string LoadLiteral(std::istream& input) {
std::string result_literal;
while (std::isalpha(input.peek())) {
result_literal.push_back(static_cast<char>(input.get()));
}
return result_literal;
}
Node LoadArray(std::istream& input) {
std::vector<Node> result;
for (char character; input >> character && character != ']';) {
if (character != ',') {
input.putback(character);
}
result.push_back(LoadNode(input));
}
if (!input) {
throw ParsingError("Array parsing error"s);
}
return Node(std::move(result));
}
Node LoadDict(std::istream& input) {
Dict dict;
for (char character; input >> character && character != '}';) {
if (character == '"') {
std::string key = LoadString(input).AsString();
if (input >> character && character == ':') {
if (dict.find(key) != dict.end()) {
throw ParsingError("Duplicate key '"s + key + "' have been found"s);
}
dict.emplace(std::move(key), LoadNode(input));
} else {
throw ParsingError(": is expected but '"s + character + "' has been found"s);
}
} else if (character != ',') {
throw ParsingError(R"(',' is expected but ')"s + character + "' has been found"s);
}
}
if (!input) {
throw ParsingError("Dictionary parsing error"s);
}
return Node(std::move(dict));
}
Node LoadString(std::istream& input) {
auto it = std::istreambuf_iterator<char>(input);
auto end = std::istreambuf_iterator<char>();
std::string input_string;
while (true) {
if (it == end) {
throw ParsingError("String parsing error");
}
const char ch = *it;
if (ch == '"') {
++it;
break;
} else if (ch == '\\') {
++it;
if (it == end) {
throw ParsingError("String parsing error");
}
const char escaped_char = *(it);
switch (escaped_char) {
case 'n':
input_string.push_back('\n');
break;
case 't':
input_string.push_back('\t');
break;
case 'r':
input_string.push_back('\r');
break;
case '"':
input_string.push_back('"');
break;
case '\\':
input_string.push_back('\\');
break;
default:
throw ParsingError("Unrecognized escape sequence \\"s + escaped_char);
}
} else if (ch == '\n' || ch == '\r') {
throw ParsingError("Unexpected end of line"s);
} else {
input_string.push_back(ch);
}
++it;
}
return Node(std::move(input_string));
}
Node LoadBool(std::istream& input) {
const std::string literal = LoadLiteral(input);
if (literal == "true"sv) {
return Node{ true };
} else if (literal == "false"sv) {
return Node{ false };
} else {
throw ParsingError("Failed to parse '"s + literal + "' as bool"s);
}
}
Node LoadNull(std::istream& input) {
if (std::string literal = LoadLiteral(input); literal == "null"sv) {
return Node{ nullptr };
} else {
throw ParsingError("Failed to parse '"s + literal + "' as null"s);
}
}
Node LoadNumber(std::istream& input) {
std::string parsed_num;
// Считывает в parsed_num очередной символ из input
auto read_char = [&parsed_num, &input] {
parsed_num += static_cast<char>(input.get());
if (!input) {
throw ParsingError("Failed to read number from stream"s);
}
};
// Считывает одну или более цифр в parsed_num из input
auto read_digits = [&input, read_char] {
if (!std::isdigit(input.peek())) {
throw ParsingError("A digit is expected"s);
}
while (std::isdigit(input.peek())) {
read_char();
}
};
if (input.peek() == '-') {
read_char();
}
// Парсим целую часть числа
if (input.peek() == '0') {
read_char();
// После 0 в JSON не могут идти другие цифры
} else {
read_digits();
}
bool is_int = true;
// Парсим дробную часть числа
if (input.peek() == '.') {
read_char();
read_digits();
is_int = false;
}
// Парсим экспоненциальную часть числа
if (int character = input.peek(); character == 'e' || character == 'E') {
read_char();
if (character = input.peek(); character == '+' || character == '-') {
read_char();
}
read_digits();
is_int = false;
}
try {
if (is_int) {
// Сначала пробуем преобразовать строку в int
try {
return std::stoi(parsed_num);
} catch (...) {
// В случае неудачи, например, при переполнении
// код ниже попробует преобразовать строку в double
}
}
return std::stod(parsed_num);
} catch (...) {
throw ParsingError("Failed to convert "s + parsed_num + " to number"s);
}
}
Node LoadNode(std::istream& input) {
char character;
if (!(input >> character)) {
throw ParsingError("Unexpected EOF"s);
}
switch (character) {
case '[':
return LoadArray(input);
case '{':
return LoadDict(input);
case '"':
return LoadString(input);
case 't':
// Атрибут [[fallthrough]] (провалиться) ничего не делает, и является
// подсказкой компилятору и человеку, что здесь программист явно задумывал
// разрешить переход к инструкции следующей ветки case, а не случайно забыл
// написать break, return или throw.
// В данном случае, встретив t или f, переходим к попытке парсинга
// литералов true либо false
[[fallthrough]];
case 'f':
input.putback(character);
return LoadBool(input);
case 'n':
input.putback(character);
return LoadNull(input);
default:
input.putback(character);
return LoadNumber(input);
}
}
struct PrintContext {
std::ostream& out;
int indent_step = 4;
int indent = 0;
void PrintIndent() const {
for (int i = 0; i < indent; ++i) {
out.put(' ');
}
}
PrintContext Indented() const {
return { out, indent_step, indent_step + indent };
}
};
void PrintNode(const Node& value, const PrintContext& ctx);
template <typename Value>
void PrintValue(const Value& value, const PrintContext& ctx) {
ctx.out << value;
}
void PrintString(const std::string& value, std::ostream& out) {
out.put('"');
for (const char character : value) {
switch (character) {
case '\r':
out << "\\r"sv;
break;
case '\n':
out << "\\n"sv;
break;
case '"':
// Символы " и \ выводятся как \" или \\, соответственно
[[fallthrough]];
case '\\':
out.put('\\');
[[fallthrough]];
default:
out.put(character);
break;
}
}
out.put('"');
}
template <>
void PrintValue<std::string>(const std::string& value, const PrintContext& ctx) {
PrintString(value, ctx.out);
}
template <>
void PrintValue<std::nullptr_t>(const std::nullptr_t&, const PrintContext& ctx) {
ctx.out << "null"sv;
}
// В специализаци шаблона PrintValue для типа bool параметр value передаётся
// по константной ссылке, как и в основном шаблоне.
// В качестве альтернативы можно использовать перегрузку:
// void PrintValue(bool value, const PrintContext& ctx);
template <>
void PrintValue<bool>(const bool& value, const PrintContext& ctx) {
ctx.out << (value ? "true"sv : "false"sv);
}
template <>
void PrintValue<Array>(const Array& nodes, const PrintContext& ctx) {
std::ostream& out = ctx.out;
out << "[\n"sv;
bool first = true;
auto inner_ctx = ctx.Indented();
for (const Node& node : nodes) {
if (first) {
first = false;
} else {
out << ",\n"sv;
}
inner_ctx.PrintIndent();
PrintNode(node, inner_ctx);
}
out.put('\n');
ctx.PrintIndent();
out.put(']');
}
template <>
void PrintValue<Dict>(const Dict& nodes, const PrintContext& ctx) {
std::ostream& out = ctx.out;
out << "{\n"sv;
bool first = true;
auto inner_ctx = ctx.Indented();
for (const auto& [key, node] : nodes) {
if (first) {
first = false;
} else {
out << ",\n"sv;
}
inner_ctx.PrintIndent();
PrintString(key, ctx.out);
out << ": "sv;
PrintNode(node, inner_ctx);
}
out.put('\n');
ctx.PrintIndent();
out.put('}');
}
void PrintNode(const Node& node, const PrintContext& ctx) {
std::visit(
[&ctx](const auto& value) {
PrintValue(value, ctx);
ctx.out;
},
node.GetValue());
}
} // namespace
Document Load(std::istream& input) {
return Document{ LoadNode(input) };
}
void Print(const Document& doc, std::ostream& output) {
PrintNode(doc.GetRoot(), PrintContext{ output });
}
} // namespace json