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
|
#include "json.hh"
#include <cassert>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <ostream>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
namespace json {
namespace {
struct StackEntry {
enum class Type : uint8_t {
kRoot,
kObject,
kArray,
};
Type type;
bool need_comma{false};
explicit StackEntry(Type type) : type(type) {}
};
class BaseWriter : public Writer {
public:
BaseWriter() : stack_({StackEntry(StackEntry::Type::kRoot)}) {}
void value(std::string_view value) override {
before_value();
quote(value);
}
void value(int64_t value) override {
before_value();
write(value);
}
void value(uint64_t value) override {
before_value();
write(value);
}
void value(float value) override {
before_value();
write(value);
}
void value(double value) override {
before_value();
write(value);
}
void value(bool value) override {
before_value();
write(value);
}
void value(std::nullptr_t) override {
before_value();
write(nullptr);
}
void start_array() override {
before_value();
stack_.emplace_back(StackEntry::Type::kArray);
write('[');
}
void end_array() override {
if (stack_.empty()) {
assert(false);
return;
}
assert(stack_.back().type == StackEntry::Type::kArray);
stack_.pop_back();
write(']');
if (!stack_.empty())
stack_.back().need_comma = true;
}
void start_object() override {
before_value();
stack_.emplace_back(StackEntry::Type::kObject);
write('{');
}
void key(std::string_view name) override {
if (stack_.empty()) {
assert(false);
return;
}
assert(stack_.back().type == StackEntry::Type::kObject);
if (stack_.back().need_comma) {
write(',');
stack_.back().need_comma = false;
}
quote(name);
write(':');
}
void end_object() override {
if (stack_.empty()) {
assert(false);
return;
}
assert(stack_.back().type == StackEntry::Type::kObject);
stack_.pop_back();
write('}');
if (!stack_.empty())
stack_.back().need_comma = true;
}
void clear() override {
stack_.clear();
stack_.emplace_back(StackEntry::Type::kRoot);
}
protected:
virtual void write(int64_t value) = 0;
virtual void write(uint64_t value) = 0;
virtual void write(float value) = 0;
virtual void write(double value) = 0;
virtual void write(bool value) = 0;
virtual void write(char value) = 0;
virtual void write(std::string_view value) = 0;
virtual void write(std::nullptr_t) = 0;
void write(char const* value) { this->write(std::string_view(value)); }
private:
void before_value() {
if (stack_.empty()) {
assert(false);
return;
}
if (stack_.back().need_comma) {
assert(stack_.back().type != StackEntry::Type::kRoot);
write(',');
} else {
stack_.back().need_comma = true;
}
}
void quote(std::string_view str) {
write('"');
size_t last = 0;
while (true) {
auto pos = need_quote(str, last);
if (pos == std::string_view::npos) {
write(str.substr(last));
break;
}
write(str.substr(last, pos - last));
switch (str[pos]) {
case '"':
write("\\\"");
break;
case '\\':
write("\\\\");
break;
case '\n':
write("\\n");
break;
case '\r':
write("\\r");
break;
case '\t':
write("\\t");
break;
case '\b':
write("\\b");
break;
case '\f':
write("\\f");
break;
default: {
char tmp[4];
write("\\u");
auto [ptr, ec] = std::to_chars(tmp, tmp + sizeof(tmp), str[pos], 16);
if (ec == std::errc()) {
size_t len = ptr - tmp;
assert(len > 0);
for (size_t i = 4; i > len; --i)
write('0');
write(std::string_view(tmp, len));
} else {
assert(false);
}
break;
};
}
last = pos + 1;
}
write('"');
}
static inline size_t need_quote(std::string_view str, size_t offset) {
for (; offset < str.size(); ++offset) {
if (str[offset] == '\\' || str[offset] == '"' ||
(str[offset] >= 0 && str[offset] < ' '))
return offset;
}
return std::string_view::npos;
}
std::vector<StackEntry> stack_;
};
class IosWriter : public BaseWriter {
public:
explicit IosWriter(std::ostream& out) : out_(out) {}
void write(std::string_view value) override { out_ << value; }
void write(int64_t value) override { out_ << value; }
void write(uint64_t value) override { out_ << value; }
void write(float value) override { out_ << value; }
void write(double value) override { out_ << value; }
void write(bool value) override { out_ << (value ? "true" : "false"); }
void write(char value) override { out_ << value; }
void write(std::nullptr_t) override { out_ << "null"; }
private:
std::ostream& out_;
};
class StringWriter : public BaseWriter {
public:
explicit StringWriter(std::string& out) : out_(out) {}
void write(std::string_view value) override { out_.append(value); }
void write(int64_t value) override {
auto [ptr, ec] = std::to_chars(tmp_, tmp_ + sizeof(tmp_), value);
if (ec == std::errc()) {
out_.append(tmp_, ptr - tmp_);
} else {
assert(false);
}
}
void write(uint64_t value) override {
auto [ptr, ec] = std::to_chars(tmp_, tmp_ + sizeof(tmp_), value);
if (ec == std::errc()) {
out_.append(tmp_, ptr - tmp_);
} else {
assert(false);
}
}
void write(float value) override {
auto [ptr, ec] = std::to_chars(tmp_, tmp_ + sizeof(tmp_), value);
if (ec == std::errc()) {
out_.append(tmp_, ptr - tmp_);
} else {
assert(false);
}
}
void write(double value) override {
auto [ptr, ec] = std::to_chars(tmp_, tmp_ + sizeof(tmp_), value);
if (ec == std::errc()) {
out_.append(tmp_, ptr - tmp_);
} else {
assert(false);
}
}
void write(bool value) override { out_.append(value ? "true" : "false"); }
void write(char value) override { out_.push_back(value); }
void write(std::nullptr_t) override { out_.append("null"); }
void clear() override {
BaseWriter::clear();
out_.clear();
}
private:
std::string& out_;
char tmp_[100];
};
} // namespace
void Writer::value(char const* value) { this->value(std::string_view(value)); }
void Writer::value(int32_t value) { this->value(static_cast<int64_t>(value)); }
void Writer::value(uint32_t value) {
this->value(static_cast<uint64_t>(value));
}
std::unique_ptr<Writer> writer(std::string& out) {
return std::make_unique<StringWriter>(out);
}
std::unique_ptr<Writer> writer(std::ostream& out) {
return std::make_unique<IosWriter>(out);
}
} // namespace json
|