-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.cpp
328 lines (313 loc) · 8.19 KB
/
io.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
#include "jsonx.hpp"
#include "io.hpp"
#include "scanner.hpp"
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <stdexcept>
#include <cctype>
#include <regex>
#include <cstdint>
using namespace std;
static const regex UINT_EXPR(R"(^\d+$)");
static const regex SINT_EXPR(R"(^[+-]\d+$)");
static const regex REAL_EXPR(R"(^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$)");
namespace jsonx {
void serialize(ostream &ss, const string &v) {
ss << "\"";
for_each(v.begin(), v.end(), [&ss](const char ch) {
switch (ch) {
case '\0':
ss << "\\0";
break;
case '\"':
ss << "\\\"";
break;
case '\\':
ss << "\\\\";
break;
case '\b':
ss << "\\b";
break;
case '\f':
ss << "\\f";
break;
case '\n':
ss << "\\n";
break;
case '\r':
ss << "\\r";
break;
case '\t':
ss << "\\t";
break;
default:
ss << ch;
break;
} // end switch //
});
ss << "\"";
}
void serialize(ostream &ss, const json_array_t &v) {
ss << "[";
bool first{true};
for_each (v.begin(), v.end(), [&first, &ss] (const json& v) {
if (first)
first = false;
else
ss << ",";
v.write(ss);
});
ss << "]";
}
void serialize(ostream &ss, const json_object_t &v) {
ss << "{";
bool first{true};
for_each (v.begin(), v.end(), [&first, &ss] (const pair<const string, json> &v) {
if (v.second.isDefined()) {
if (first)
first = false;
else
ss << ",";
serialize(ss, v.first);
ss << ":";
v.second.write(ss);
}
});
ss << "}";
}
void json::write(std::ostream &os) const
{
switch (type) {
case UNDEFINED_T:
break;
case NULL_T:
os << "null";
break;
case BOOL_T:
os << (bool_value ? "true" : "false");
break;
case SIGNED_T:
os << int_value;
break;
case UNSIGNED_T:
os << uint_value;
break;
case REAL_T:
os << real_value;
break;
case STRING_T:
::jsonx::serialize(os, *string_value);
break;
case ARRAY_T:
::jsonx::serialize(os, *array_value);
break;
case OBJECT_T:
::jsonx::serialize(os, *object_value);
break;
default:
cerr << "jsonx::json: Invalid data type " << type << endl;
} // end switch //}
}
static inline std::string chartostring(char c)
{
return string(&c, 1);
}
static void parse_string(jsonx::scanner &sc, std::string &s)
{
sc.skip_whitespace();
if (sc.cur_ch != '"')
throw runtime_error(string("Expected ':', got '") +
string((char*)&sc.cur_ch, 1) + "'");
std::ostringstream is;
while (true) {
sc.get_ch();
if (sc.eof())
throw runtime_error("Premature EOF");
// End of string?:
if (sc.cur_ch == '"') {
sc.get_ch();
s = is.str();
return;
}
// Not escape?
if (sc.cur_ch != '\\') {
is.put(sc.cur_ch);
continue;
}
sc.get_ch();
switch (sc.cur_ch) {
case '0':
is.put('\0');
break;
case '"':
is.put('"');
break;
case '\\':
is.put('\\');
break;
case 'b':
is.put('\b');
break;
case 'f':
is.put('\f');
break;
case 'n':
is.put('\n');
break;
case 'r':
is.put('\r');
break;
case 't':
is.put('\t');
break;
case 'u': // Unicode not implemented for now
throw runtime_error("Unicode escape sequences not implemented for now");
default:
throw runtime_error("Invalid escape sequence '\\"
+ chartostring(sc.cur_ch) + "'");
} // end switch //
} // end while //
}
static void parse_token(jsonx::scanner &sc, std::string &s)
{
sc.skip_whitespace();
std::ostringstream is;
while ((!sc.eof())
&& (!std::isspace(sc.cur_ch))
&& (sc.cur_ch != ',')
&& (sc.cur_ch != ']')
&& (sc.cur_ch != '}'))
{
is.put(sc.cur_ch);
sc.get_ch();
}
s = is.str();
}
void json::parse(jsonx::scanner &sc)
{
clear();
sc.skip_whitespace();
switch (sc.cur_ch) {
case '{':
{
type = OBJECT_T;
object_value = new json_object_t();
sc.get_ch();
sc.skip_whitespace();
while (sc.cur_ch != '}') {
if (sc.eof())
throw runtime_error("Premature EOF");
std::string key;
jsonx::parse_string(sc, key);
sc.skip_whitespace();
if (sc.cur_ch != ':') {
char s[2];
s[0] = sc.cur_ch;
s[1] = '\0';
throw runtime_error(string("Expected ':', got '") + s + "'");
}
sc.get_ch();
sc.skip_whitespace();
json child;
child.parse(sc);
add(key, child);
sc.skip_whitespace();
if (sc.cur_ch == ',') {
sc.get_ch();
sc.skip_whitespace();
}
} // end while //
sc.get_ch();
}
break;
case '[':
{
type = ARRAY_T;
array_value = new json_array_t();
sc.get_ch();
sc.skip_whitespace();
while (sc.cur_ch != ']') {
if (sc.eof())
throw runtime_error("Premature EOF");
json child;
child.parse(sc);
add(child);
sc.skip_whitespace();
if (sc.cur_ch == ',') {
sc.get_ch();
sc.skip_whitespace();
}
} // end while //
sc.get_ch();
}
break;
case '"':
{
std::string s;
jsonx::parse_string(sc, s);
set(s);
}
break;
default:
{
std::string s;
jsonx::parse_token(sc, s);
if (s == "") {
type = UNDEFINED_T;
return;
}
if (s == "null") {
type = NULL_T;
return;
}
if (s == "true") {
type = BOOL_T;
bool_value = true;
return;
}
if (s == "false") {
type = BOOL_T;
bool_value = false;
return;
}
if (regex_match(s, UINT_EXPR)) {
type = UNSIGNED_T;
uint_value = std::stoull(s);
return;
}
if (regex_match(s, SINT_EXPR)) {
type = SIGNED_T;
uint_value = std::stoll(s);
return;
}
if (regex_match(s, REAL_EXPR)) {
type = REAL_T;
real_value = static_cast<json_real_t>(std::stod(s));
return;
}
throw runtime_error(string("Unexpected token: \"" + s + "\""));
}
} // end switch //
}
void json::parse(std::istream &is)
{
scanner sc(is);
try {
parse(sc);
}
catch (const std::exception &ex) {
clear();
throw runtime_error(std::string("Syntax error ") + ex.what()
+ " in line " + std::to_string(sc.cur_line)
+ ", column " + std::to_string(sc.cur_col));
}
catch (...) {
clear();
throw runtime_error(std::string("Syntax error ...")
+ " in line " + std::to_string(sc.cur_line)
+ ", column " + std::to_string(sc.cur_col));
}
}
} // end namespace jsonx //