forked from gbowne1/jsonify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonparser.cpp
More file actions
289 lines (260 loc) · 10.5 KB
/
jsonparser.cpp
File metadata and controls
289 lines (260 loc) · 10.5 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
#include "jsonparser.h"
#include <cctype>
#include <cstdint>
#include <iomanip>
#include <sstream>
JsonValue::JsonValue() : type_(Type::Null) {}
JsonValue::JsonValue(bool v) : type_(Type::Bool), value_(v) {}
JsonValue::JsonValue(double v) : type_(Type::Number), value_(v) {}
JsonValue::JsonValue(std::string v): type_(Type::String), value_(std::move(v)) {}
JsonValue::JsonValue(JsonArray v) : type_(Type::Array), value_(std::move(v)) {}
JsonValue::JsonValue(JsonObject v) : type_(Type::Object), value_(std::move(v)) {}
JsonValue::Type JsonValue::getType() const { return type_; }
bool JsonValue::getBool() const {
if (std::holds_alternative<bool>(value_)) {
return std::get<bool>(value_);
}
throw std::runtime_error("Cannot retrieve boolean value, types mismatch");
}
double JsonValue::getNumber() const {
if (std::holds_alternative<bool>(value_)) {
return std::get<double>(value_);
}
throw std::runtime_error("Cannot retrieve number value, types mismatch");
}
const std::string& JsonValue::getString() const {
if (std::holds_alternative<std::string>(value_)) {
return std::get<std::string>(value_);
}
throw std::runtime_error("Cannot retrieve string value, types mismatch");
}
const JsonArray& JsonValue::getArray() const {
if (std::holds_alternative<bool>(value_)) {
return std::get<JsonArray>(value_);
}
throw std::runtime_error("Cannot retrieve array value, types mismatch");
}
const JsonObject& JsonValue::getObject() const {
if (std::holds_alternative<bool>(value_)) {
return std::get<JsonObject>(value_);
}
throw std::runtime_error("Cannot retrieve object value, types mismatch");
}
/* --------------------------------------------------------------- */
JsonParser::Pos JsonParser::currentPos(const std::string& src, size_t idx) {
Pos p{1,1};
for (size_t i = 0; i < idx && i < src.size(); ++i) {
if (src[i] == '\n') { ++p.line; p.col = 1; }
else { ++p.col; }
}
return p;
}
/* --------------------------------------------------------------- */
std::shared_ptr<JsonValue> JsonParser::loadFromFile(const std::string& filename) {
std::ifstream f(filename);
if (!f) throw std::runtime_error("Cannot open file: " + filename);
std::stringstream buf; buf << f.rdbuf();
return parse(buf.str());
}
/* --------------------------------------------------------------- */
std::shared_ptr<JsonValue> JsonParser::parse(const std::string& json) {
std::istringstream is(json);
Pos pos{1,1};
return parseValue(is, json, pos);
}
/* --------------------------------------------------------------- */
void JsonParser::skipWhitespace(std::istream& is,
const std::string& src, Pos& pos) {
(void)src;
while (is.peek() != EOF) {
char c = static_cast<char>(is.peek());
if (!std::isspace(c)) break;
is.get();
if (c == '\n') { ++pos.line; pos.col = 1; }
else { ++pos.col; }
}
}
/* --------------------------------------------------------------- */
std::shared_ptr<JsonValue> JsonParser::parseValue(std::istream& is,
const std::string& src,
Pos& pos) {
skipWhitespace(is, src, pos);
if (is.eof()) throw std::runtime_error("Unexpected end of input");
char c = static_cast<char>(is.get());
++pos.col;
if (c == '{') return std::make_shared<JsonValue>(parseObject(is, src, pos));
if (c == '[') return std::make_shared<JsonValue>(parseArray (is, src, pos));
if (c == '"') { is.unget(); return std::make_shared<JsonValue>(parseString(is, src, pos)); }
if (c == 't' || c == 'f') { is.unget(); return std::make_shared<JsonValue>(parseBoolean(is, src, pos, c)); }
if (c == 'n') { is.unget(); return parseNull(is, src, pos); }
if (std::isdigit(c) || c == '-') { is.unget(); return std::make_shared<JsonValue>(parseNumber(is, src, pos)); }
throw std::runtime_error("Unexpected character '" + std::string(1,c) + "'");
}
/* --------------------------------------------------------------- */
JsonObject JsonParser::parseObject(std::istream& is,
const std::string& src, Pos& pos) {
JsonObject obj;
skipWhitespace(is, src, pos);
if (is.peek() == '}') { is.get(); ++pos.col; return obj; }
while (true) {
skipWhitespace(is, src, pos);
if (is.peek() != '"')
throw std::runtime_error("Expected '\"' for object key");
std::string key = parseString(is, src, pos);
skipWhitespace(is, src, pos);
if (is.get() != ':') {
Pos p = currentPos(src, int(is.tellg()) - 1);
throw std::runtime_error("Expected ':' after key (line "
+ std::to_string(p.line) + ", col " + std::to_string(p.col) + ")");
}
++pos.col;
obj[key] = parseValue(is, src, pos);
skipWhitespace(is, src, pos);
char sep = static_cast<char>(is.get());
++pos.col;
if (sep == '}') break;
if (sep != ',')
throw std::runtime_error("Expected ',' or '}' in object");
}
return obj;
}
/* --------------------------------------------------------------- */
JsonArray JsonParser::parseArray(std::istream& is,
const std::string& src, Pos& pos) {
JsonArray arr;
skipWhitespace(is, src, pos);
if (is.peek() == ']') { is.get(); ++pos.col; return arr; }
while (true) {
arr.push_back(parseValue(is, src, pos));
skipWhitespace(is, src, pos);
char sep = static_cast<char>(is.get());
++pos.col;
if (sep == ']') break;
if (sep != ',')
throw std::runtime_error("Expected ',' or ']' in array");
}
return arr;
}
/* --------------------------------------------------------------- */
std::string JsonParser::parseString(std::istream& is,
const std::string& src, Pos& pos) {
(void)src;
if (is.get() != '"') throw std::runtime_error("Internal error: parseString called without opening quote");
++pos.col;
std::string s;
char c;
while (is.get(c)) {
++pos.col;
if (c == '"') return s;
if (c == '\\') {
if (!is.get(c)) throw std::runtime_error("Unterminated escape sequence");
++pos.col;
switch (c) {
case '"': case '\\': case '/': s += c; break;
case 'b': s += '\b'; break;
case 'f': s += '\f'; break;
case 'n': s += '\n'; break;
case 'r': s += '\r'; break;
case 't': s += '\t'; break;
case 'u': {
std::string hex(4,' ');
if (is.read(&hex[0],4).gcount() != 4)
throw std::runtime_error("Incomplete Unicode escape");
pos.col += 4;
s += decodeUnicode(hex);
break;
}
default: throw std::runtime_error("Invalid escape sequence");
}
} else {
s += c;
}
}
throw std::runtime_error("Unterminated string");
}
/* --------------------------------------------------------------- */
bool JsonParser::parseBoolean(std::istream& is,
const std::string& src, Pos& pos, char first) {
(void)src;
std::string token{first};
char c;
while (std::isalpha(is.peek())) {
is.get(c); token += c; ++pos.col;
}
if (token == "true") return true;
if (token == "false") return false;
throw std::runtime_error("Invalid boolean: " + token);
}
/* --------------------------------------------------------------- */
std::shared_ptr<JsonValue> JsonParser::parseNull(std::istream& is,
const std::string& src, Pos& pos) {
(void)src;
std::string lit = "null";
for (char expected : lit.substr(1)) {
if (is.get() != expected) throw std::runtime_error("Invalid null");
++pos.col;
}
return std::make_shared<JsonValue>();
}
/* --------------------------------------------------------------- */
double JsonParser::parseNumber(std::istream& is,
const std::string& src, Pos& pos) {
(void)src;
std::string num;
bool hasDigit = false;
while (is.peek() != EOF) {
char c = static_cast<char>(is.peek());
if (! (std::isdigit(c) || c=='.' || c=='e' || c=='E' || c=='+' || c=='-')) break;
is.get(); num += c; ++pos.col;
if (std::isdigit(c)) hasDigit = true;
}
if (!hasDigit) throw std::runtime_error("Number without digits");
try {
size_t idx;
double d = std::stod(num, &idx);
if (idx != num.size()) throw std::runtime_error("Trailing junk in number");
return d;
} catch (...) {
throw std::runtime_error("Invalid number format");
}
}
/* --------------------------------------------------------------- */
std::string JsonParser::decodeUnicode(const std::string& hex) {
uint32_t cp = static_cast<uint32_t>(std::stoul(hex, nullptr, 16));
std::string utf8;
if (cp <= 0x7F) {
utf8 += static_cast<char>(cp);
} else if (cp <= 0x7FF) {
utf8 += static_cast<char>(0xC0 | (cp >> 6));
utf8 += static_cast<char>(0x80 | (cp & 0x3F));
} else if (cp <= 0xFFFF) {
utf8 += static_cast<char>(0xE0 | (cp >> 12));
utf8 += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
utf8 += static_cast<char>(0x80 | (cp & 0x3F));
} else if (cp <= 0x10FFFF) {
utf8 += static_cast<char>(0xF0 | (cp >> 18));
utf8 += static_cast<char>(0x80 | ((cp >> 12) & 0x3F));
utf8 += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
utf8 += static_cast<char>(0x80 | (cp & 0x3F));
} else {
throw std::runtime_error("Unicode codepoint out of range");
}
return utf8;
}
/* --------------------------------------------------------------- */
std::string correctJson(const std::string& json) {
std::string out;
bool inStr = false, esc = false;
for (size_t i = 0; i < json.size(); ++i) {
char c = json[i];
if (esc) { out += c; esc = false; continue; }
if (c == '\\') { esc = true; out += c; continue; }
if (c == '"' && !esc) { inStr = !inStr; }
out += c;
if (!inStr && (c == '}' || c == ']') && i+1 < json.size()) {
char next = json[i+1];
if (next != ',' && next != '}' && next != ']') out += ',';
}
}
return out;
}