-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomba_json.hpp
359 lines (323 loc) · 9 KB
/
bomba_json.hpp
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
#ifndef BOMBA_JSON
#define BOMBA_JSON
#ifndef BOMBA_CORE // Needed to run in godbolt
#include "bomba_core.hpp"
#endif
#include <array>
#include <string_view>
#include <charconv>
#include <cstring>
#include <cmath>
namespace Bomba {
template <BetterAssembledString LocalStringType = std::string, AssembledString OutputStringType = LocalStringType>
struct BasicJson {
class Input : public IStructuredInput {
std::string_view _contents;
LocalStringType _resultBuffer;
int _position = 0;
void endOfInput() {
good = false;
parseError("Unexpected end of JSON input");
}
void fail(const char* problem) {
remoteError(problem);
good = false;
}
bool isPastEnd(int position) {
if (_contents.begin() + position < _contents.end()) [[likely]]
return false;
if (good) [[unlikely]]
endOfInput();
return true;
}
char getChar() {
if (!isPastEnd(_position)) [[likely]] {
char got = _contents[_position];
_position++;
return got;
}
return '\0';
}
template <bool mandatory = true>
char peekChar() {
if (_contents.begin() + _position < _contents.end()) [[likely]]
return _contents[_position];
if constexpr(mandatory)
endOfInput();
return '\0';
}
void eatWhitespace() {
char nextChar = peekChar<false>();
while (nextChar == ' ' || nextChar == '\t' || nextChar == '\n' || nextChar == '\r'
|| nextChar == ',' || nextChar == ':') {
_position++;
nextChar = peekChar<false>();
}
}
public:
Input(std::string_view contents) : _contents(contents) {}
MemberType identifyType(Flags flags) final override {
eatWhitespace();
if (_contents.begin() + _position >= _contents.end()) [[unlikely]]
return TYPE_INVALID;
char nextChar = peekChar();
if (nextChar == '"')
return TYPE_STRING;
if (nextChar == '-' || (nextChar >= '0' && nextChar <= '9')) {
for (int i = _position; _contents.begin() + i < _contents.end(); i++) {
if (_contents[i] == '.' || _contents[i] == 'e' || _contents[i] == 'E')
return TYPE_FLOAT;
if (_contents[i] < '0' || _contents[i] > '9')
return TYPE_INTEGER;
}
return TYPE_INTEGER;
}
if (nextChar == 'n')
return TYPE_NULL;
if (nextChar == 't' || nextChar == 'f')
return TYPE_BOOLEAN;
if (nextChar == '[')
return TYPE_ARRAY;
if (nextChar == '{')
return TYPE_OBJECT;
return TYPE_INVALID;
}
int64_t readInt(Flags flags) final override {
eatWhitespace();
int64_t result = 0;
std::from_chars_result got = std::from_chars(&_contents[_position], &*_contents.end(), result);
_position += got.ptr - &_contents[_position];
if (got.ec != std::errc()) [[unlikely]]
fail("Expected JSON integer");
// Deal with floats sent instead of integers
char next = peekChar();
while ((next >= '0' && next <= '9') || next == '.' || next == 'e' || next == 'E' || next == '-') {
_position++;
next = peekChar();
}
return result;
}
double readFloat(Flags flags) final override {
eatWhitespace();
double result = 0;
std::from_chars_result got = std::from_chars(&_contents[_position], &*_contents.end(), result);
_position += got.ptr - &_contents[_position];
if (got.ec != std::errc()) [[unlikely]]
fail("Expected JSON double");
return result;
}
std::string_view readString(Flags flags) final override {
eatWhitespace();
char readingChar = getChar();
if (readingChar != '"') [[unlikely]] {
// std::cout << "Found " << readingChar << " instead of \"" << std::endl;
fail("Expected JSON string");
}
_resultBuffer.clear();
readingChar = getChar();
while (readingChar != '"') {
if (readingChar == '\\') [[unlikely]] {
readingChar = getChar();
if (readingChar == '\\')
_resultBuffer += '\\';
else if (readingChar == '"')
_resultBuffer += '"';
else if (readingChar == 'n')
_resultBuffer += '\n';
} else _resultBuffer += readingChar;
readingChar = getChar();
}
return _resultBuffer;
}
bool readBool(Flags flags) final override {
eatWhitespace();
char readingChar = getChar();
if (readingChar == 't') {
for (auto& it : {'r', 'u', 'e'}) {
readingChar = getChar();
if (it != readingChar)
fail("Expected JSON bool");
}
return true;
} else if (readingChar == 'f') {
for (auto& it : {'a', 'l', 's', 'e'}) {
readingChar = getChar();
if (it != readingChar)
fail("Expected JSON bool");
}
return false;
} else [[unlikely]] fail("Expected JSON bool");
return false;
}
void readNull(Flags flags) final override {
eatWhitespace();
for (auto& it : {'n', 'u', 'l', 'l'}) {
char readingChar = getChar();
if (it != readingChar)
fail("Expected JSON null");
}
}
void startReadingArray(Flags flags) final override {
eatWhitespace();
char readingChar = getChar();
if (readingChar != '[')
fail("Expected JSON array");
}
bool nextArrayElement(Flags flags) final override {
eatWhitespace();
if (peekChar() != ']')
return true;
return false;
}
void endReadingArray(Flags flags) final override {
eatWhitespace();
getChar();
}
virtual void readObject(Flags flags, Callback<bool(std::optional<std::string_view> memberName, int index)> onEach) {
eatWhitespace();
char readingChar = getChar();
if (readingChar != '{')
fail("Expected JSON object");
for (int index = 0; true; index++) {
eatWhitespace();
if (peekChar() != '}') {
if (!onEach(readString(flags), index))
break;
} else
break;
}
eatWhitespace();
getChar();
}
void skipObjectElement(Flags) final override {
eatWhitespace();
int depth = 0;
do {
char readingChar = peekChar();
if (readingChar == '{' || readingChar == '[') {
depth++;
getChar();
} else if (readingChar == '}' || readingChar == ']') {
depth--;
getChar();
if (depth <= 0)
break;
} else if (depth == 0 && (readingChar == ' ' || readingChar == '\t'
|| readingChar == '\r' || readingChar == '\n' || readingChar == ',')) {
break; // eatWhitespace() was called before
} else if (readingChar == '"') {
getChar();
do {
readingChar = getChar();
} while (readingChar != '"' && _contents[_position - 1] != '\\');
} else { // Numbers, bool, null...
getChar();
}
} while (_contents.begin() + _position < _contents.end());
}
bool readOptional(Flags flags, Callback<> readValue) override {
if (identifyType(flags) != IStructuredInput::TYPE_NULL) {
readValue();
return true;
} else {
readNull(flags);
return false;
}
}
Location storePosition(Flags) final override {
return Location{ _position };
}
void restorePosition(Flags, Location location) final override {
_position = location.loc;
}
};
class Output : public IStructuredOutput {
OutputStringType& _contents;
int _depth = 0;
bool _skipNewline = false;
template <typename Num>
void writeValue(Num value) {
constexpr int SIZE = 20;
std::array<char, SIZE> bytes = {};
std::to_chars(bytes.data(), bytes.data() + SIZE, value);
_contents += &bytes[0];
}
void newLine() {
if (_skipNewline) [[unlikely]] {
_skipNewline = false;
return;
}
_contents += '\n';
for (int i = 0; i < _depth; i++)
_contents += '\t';
}
public:
Output(OutputStringType& contents) : _contents(contents) {}
void writeInt(Flags, int64_t value) final override {
writeValue(value);
}
void writeFloat(Flags flags, double value) final override {
writeValue(value);
}
void writeString(Flags flags, std::string_view value) final override {
_contents += '"';
for (const char it : value) {
if (it == '\\' || it == '\n' || it == '"') [[unlikely]]
_contents += '\\';
_contents += it;
}
_contents += '"';
}
void writeBool(Flags flags, bool value) final override {
if (value)
_contents += "true";
else
_contents += "false";
}
void writeNull(Flags flags) final override {
_contents += "null";
}
void startWritingArray(Flags flags, int size) final override {
_contents += '[';
_depth++;
if (size == 0)
_skipNewline = true;
}
void introduceArrayElement(Flags flags, int index) final override {
if (index > 0)
_contents += ',';
newLine();
}
void endWritingArray(Flags flags) final override {
_depth--;
newLine();
_contents += ']';
}
void startWritingObject(Flags flags, int size) final override {
_contents += '{';
_depth++;
if (size == 0)
_skipNewline = true;
}
void introduceObjectMember(Flags flags, std::string_view name, int index) final override {
if (index > 0)
_contents += ',';
newLine();
writeString(flags, name);
_contents += " : ";
}
void endWritingObject(Flags flags) final override {
_depth--;
newLine();
_contents += '}';
}
void writeOptional(Flags flags, bool present, Callback<> writeValue) final override {
if (!present)
writeNull(flags);
else
writeValue();
}
};
};
} // namespace Bomba
#endif // BOMBA_JSON