-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.hpp
More file actions
394 lines (349 loc) · 9.05 KB
/
Copy pathjson.hpp
File metadata and controls
394 lines (349 loc) · 9.05 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#ifndef LOCK3_JSON_HPP
#define LOCK3_JSON_HPP
#include "concepts.hpp"
#include <string>
#include <sstream>
#include <variant>
#include <experimental/meta>
#include <experimental/compiler>
#include <iostream> // FIXME: Remove this
namespace lock3::json
{
/// Writes JSON-formatted values to an output stream. This is a CRTP class,
/// meaning it is parameterized by its derived class. Doing so means that
/// the derived class can provide additional overrides of write_value()
/// for application-specific types.
template<typename Derived, typename Out>
struct basic_writer
{
basic_writer(Out& out)
: out(out)
{ }
/// Returns this cast as the derived class.
Derived const& derived() const
{
return static_cast<Derived const&>(*this);
}
/// Returns this cast as the derived class.
Derived& derived()
{
return static_cast<Derived&>(*this);
}
void write_value(bool b)
{
out << (b ? "true" : "false");
}
template<std::integral T>
void write_value(T n)
{
out << n;
}
template<std::floating_point T>
void write_value(T n)
{
out << n;
}
void write_value(std::string const& str)
{
out << '"' << str << '"';
}
template<std::ranges::range R>
void write_array(R const& range)
{
out << '[';
auto first = std::begin(range);
auto last = std::end(range);
for (auto iter = first; iter != last; ++iter) {
derived().write(*iter);
if (std::next(iter) != last)
out << ',';
}
out << ']';
}
/// Write the members of a simple class.
///
/// TODO: This does not handle base classes.
template<basic_data_type T>
void write_class(T const& obj)
{
namespace meta = std::experimental::meta;
out << '{';
constexpr auto members = meta::members_of(^T, meta::is_data_member);
constexpr std::size_t num = size(members);
std::size_t count = 0;
template for (constexpr meta::info member : members) {
out << '"' << meta::name_of(member) << '"' << ':';
derived().write(obj.[:member:]);
if (++count != num)
out << ',';
}
out << '}';
}
/// Write the value of user-defined types (and arrays).
template<typename T>
void write_value(T const& t)
{
if constexpr (std::ranges::range<T>)
return write_array(t);
if constexpr (basic_data_type<T>)
return write_class(t);
else
static_assert(dependent_false<T>(), "unreachable");
}
/// Write the JSON-formatted version of `t` to the output stream.
template<typename T>
void write(T const& t)
{
derived().write_value(t);
}
Out& out;
};
/// A simple JSON writer that handles classes without indirection.
template<typename Out>
struct writer : basic_writer<writer<Out>, Out>
{
writer(Out& out)
: basic_writer<writer<Out>, Out>(out)
{ }
};
/// Reads JSON-formatted values from an input stream. This is a CRTP class,
/// meaning it is parameterized by its derived class. Doing so means that
/// the derived class can provide additional overrides of read_value()
/// for application-specific types.
///
/// This is a type-directed parser. That is, the type of object provided to
/// `read()` will determine how the input is parsed.
///
/// NOTE: This is not an efficient parser.
template<typename Derived, typename In>
struct basic_reader
{
basic_reader(In& in)
: in(in)
{ }
/// Returns this cast as the derived class.
Derived const& derived() const
{
return static_cast<Derived const&>(*this);
}
/// Returns this cast as the derived class.
Derived& derived()
{
return static_cast<Derived&>(*this);
}
[[noreturn]]
void error(std::string const& str)
{
std::stringstream ss;
ss << "error @ " << line << ':' << column << ": " << str;
throw std::runtime_error(ss.str());
}
char get_char()
{
char c = in.get();
if (c == '\n') {
++line;
column = 1;
}
else {
++column;
}
return c;
}
char expect_char(char c)
{
if (in.peek() != c) {
std::stringstream ss;
ss << "expected '" << c << "' but got '" << (char)in.peek() << "'";
error(ss.str());
}
get_char();
return c;
}
char expect_punctuation(char c)
{
skip_space();
char r = expect_char(c);
skip_space();
return r;
}
void skip_space()
{
while (char c = in.peek()) {
if (!std::isspace(c))
break;
get_char();
}
}
std::string scan_word()
{
std::string s;
skip_space();
while (char c = in.peek()) {
if (!std::isalpha(c))
break;
s += get_char();
}
skip_space();
return s;
}
void scan_number(std::string& s)
{
while (char c = in.peek()) {
if (!std::isdigit(c))
break;
s += get_char();
}
}
// TODO: Support hex numbers?
std::string scan_integer()
{
std::string s;
skip_space();
scan_number(s);
if (s.empty())
error("expected integer value");
skip_space();
return s;
}
// FIXME: Make this conform to the floating point input.
std::string scan_float()
{
std::string s;
skip_space();
scan_number(s);
error("expected floating point value");
if (in.peek() == '.')
s += get_char();
scan_number(s);
skip_space();
return s;
}
// FIXME: Do a better job with escape characters.
std::string scan_string()
{
std::string s;
skip_space();
expect_char('"');
while (char c = in.peek()) {
if (c == '"')
break;
if (c == '\\')
get_char();
s += get_char();
}
expect_char('"');
skip_space();
return s;
}
void read_value(bool& b)
{
std::string s = scan_word();
if (s == "true")
b = true;
else if (s == "false")
b = false;
else
error("expected 'true' or 'false'");
}
template<std::integral T>
void read_value(T& n)
{
std::string num = scan_integer();
n = std::stoll(num);
}
template<std::floating_point T>
void read_value(T& n)
{
std::string num = scan_float();
n = std::stod(num);
}
void read_value(std::string& str)
{
str = scan_string();
}
// TODO: There's another version where the size of the of sequence is
// fixed at compile-time (e.g., array). Presumably, we could do something
// similar for tuples also.
template<back_insertion_sequence S>
void read_sequence(S& seq)
{
expect_punctuation('[');
while (true) {
container_value_t<S> obj;
derived().read(obj);
seq.push_back(obj);
if (in.peek() == ']')
break;
expect_punctuation(',');
}
expect_punctuation(']');
}
template<typename T>
void read_member(T& obj, std::string const& name)
{
namespace meta = std::experimental::meta;
constexpr auto members = meta::members_of(^T, meta::is_data_member);
template for (constexpr meta::info member : members) {
if (meta::name_of(member) == name)
return derived().read(obj.[:member:]);
}
std::stringstream ss;
ss << "no member named '" << name << "' in '" << meta::name_of(^T) << "'";
error(ss.str());
}
/// Read the members of a simple class.
///
/// TODO: This does not handle base classes.
template<basic_data_type T>
void read_class(T& obj)
{
namespace meta = std::experimental::meta;
constexpr auto members = meta::members_of(^T, meta::is_data_member);
constexpr std::size_t num = size(members);
std::size_t count = 0;
expect_punctuation('{');
while (true) {
std::string key = scan_string();
expect_punctuation(':');
read_member(obj, key);
++count;
if (in.peek() == '}')
break;
expect_punctuation(',');
}
expect_punctuation('}');
if (count != num)
error("incomplete initialization of object");
}
/// Write the value of user-defined types (and arrays).
template<typename T>
void read_value(T& t)
{
// if constexpr (std::ranges::range<T>)
// return write_array(t);
if constexpr (basic_data_type<T>)
return read_class(t);
else
static_assert(dependent_false<T>(), "unreachable");
}
/// Write the JSON-formatted version of `t` to the output stream.
template<typename T>
void read(T& t)
{
derived().read_value(t);
}
In& in;
int line = 1;
int column = 1;
};
/// A simple JSON reader that handles classes without indirection.
template<typename In>
struct reader : basic_reader<reader<In>, In>
{
reader(In& in)
: basic_reader<reader<In>, In>(in)
{ }
};
} // namespace lock3
#endif