dataclass-native 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dataclass_native/__init__.py +60 -0
- dataclass_native/__main__.py +5 -0
- dataclass_native/_arrow.py +822 -0
- dataclass_native/_json.py +783 -0
- dataclass_native/_mode.py +57 -0
- dataclass_native/_msgpack.py +207 -0
- dataclass_native/_native.py +234 -0
- dataclass_native/_runtime.py +607 -0
- dataclass_native/_version.py +24 -0
- dataclass_native/_wire.py +901 -0
- dataclass_native/arrays.py +13 -0
- dataclass_native/backends/__init__.py +0 -0
- dataclass_native/backends/bindings.py +74 -0
- dataclass_native/backends/cpp.py +574 -0
- dataclass_native/backends/templates/bindings.cpp.j2 +58 -0
- dataclass_native/backends/templates/extensions.cmake.j2 +12 -0
- dataclass_native/backends/templates/header.hpp.j2 +75 -0
- dataclass_native/cli.py +125 -0
- dataclass_native/cmake/dataclass_native-config.cmake +67 -0
- dataclass_native/include/dataclass_native/arrow.hpp +2022 -0
- dataclass_native/include/dataclass_native/json.hpp +1903 -0
- dataclass_native/include/dataclass_native/msgpack.hpp +1513 -0
- dataclass_native/include/dataclass_native/python.hpp +594 -0
- dataclass_native/include/dataclass_native/reflect.hpp +63 -0
- dataclass_native/include/dataclass_native/types.hpp +417 -0
- dataclass_native/introspect.py +811 -0
- dataclass_native/py.typed +0 -0
- dataclass_native/schema.py +553 -0
- dataclass_native/types.py +103 -0
- dataclass_native-1.0.0.dist-info/METADATA +339 -0
- dataclass_native-1.0.0.dist-info/RECORD +35 -0
- dataclass_native-1.0.0.dist-info/WHEEL +5 -0
- dataclass_native-1.0.0.dist-info/entry_points.txt +2 -0
- dataclass_native-1.0.0.dist-info/licenses/LICENSE +21 -0
- dataclass_native-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1903 @@
|
|
|
1
|
+
// JSON of the structs generated by dataclass-native, in the format of JSON_FORMAT.md: as the Python of
|
|
2
|
+
// dataclass_native._json writes and reads it, character for character. Depends on nothing but the standard library.
|
|
3
|
+
//
|
|
4
|
+
// std::string data = dataclass_native::json::to_json(trade);
|
|
5
|
+
// auto read = dataclass_native::json::from_json<Trade>(data);
|
|
6
|
+
#pragma once
|
|
7
|
+
|
|
8
|
+
#include <algorithm>
|
|
9
|
+
#include <array>
|
|
10
|
+
#include <charconv>
|
|
11
|
+
#include <chrono>
|
|
12
|
+
#include <cmath>
|
|
13
|
+
#include <cstddef>
|
|
14
|
+
#include <cstdint>
|
|
15
|
+
#include <cstdlib>
|
|
16
|
+
#include <limits>
|
|
17
|
+
#include <map>
|
|
18
|
+
#include <optional>
|
|
19
|
+
#include <set>
|
|
20
|
+
#include <span>
|
|
21
|
+
#include <stdexcept>
|
|
22
|
+
#include <string>
|
|
23
|
+
#include <string_view>
|
|
24
|
+
#include <tuple>
|
|
25
|
+
#include <type_traits>
|
|
26
|
+
#include <utility>
|
|
27
|
+
#include <variant>
|
|
28
|
+
#include <vector>
|
|
29
|
+
|
|
30
|
+
// For the tags of a discriminated union's members, which are those of msgpack
|
|
31
|
+
#include "dataclass_native/msgpack.hpp"
|
|
32
|
+
#include "dataclass_native/reflect.hpp"
|
|
33
|
+
#include "dataclass_native/types.hpp"
|
|
34
|
+
|
|
35
|
+
namespace dataclass_native::json
|
|
36
|
+
{
|
|
37
|
+
// Thrown where what is written cannot be, and where what is read is not JSON, or not of its type
|
|
38
|
+
class Error : public std::invalid_argument
|
|
39
|
+
{
|
|
40
|
+
public:
|
|
41
|
+
using std::invalid_argument::invalid_argument;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
namespace detail
|
|
45
|
+
{
|
|
46
|
+
// Thrown where what is read is not of the type expected. Caught by what is reading the field of a struct,
|
|
47
|
+
// the member of a union, or the whole value
|
|
48
|
+
struct Mismatch
|
|
49
|
+
{
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
inline constexpr int max_depth = 256;
|
|
53
|
+
inline constexpr std::int64_t day_microseconds = 86'400'000'000;
|
|
54
|
+
|
|
55
|
+
[[noreturn]] inline void malformed(std::string_view what)
|
|
56
|
+
{
|
|
57
|
+
throw Error("The data is not JSON: " + std::string(what));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The kinds of JSON value a type is written as: by which the members of a plain union are told apart. A
|
|
61
|
+
// string may be any, or of a form which only its type has
|
|
62
|
+
enum Kind : unsigned
|
|
63
|
+
{
|
|
64
|
+
Null = 1,
|
|
65
|
+
Bool = 2,
|
|
66
|
+
Int = 4,
|
|
67
|
+
Float = 8,
|
|
68
|
+
String = 16,
|
|
69
|
+
List = 32,
|
|
70
|
+
Object = 64,
|
|
71
|
+
DateForm = 128,
|
|
72
|
+
TimeForm = 256,
|
|
73
|
+
DateTimeForm = 512,
|
|
74
|
+
NaiveDateTimeForm = 1024,
|
|
75
|
+
DurationForm = 2048,
|
|
76
|
+
UuidForm = 4096,
|
|
77
|
+
DecimalForm = 8192,
|
|
78
|
+
Forms = DateForm | TimeForm | DateTimeForm | NaiveDateTimeForm | DurationForm | UuidForm | DecimalForm,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Whether a value of one type may be taken for one of another: any string, for one of a form
|
|
82
|
+
constexpr bool collide(unsigned a, unsigned b)
|
|
83
|
+
{
|
|
84
|
+
return (a & b) || ((a & String) && (b & Forms)) || ((b & String) && (a & Forms));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// As Python writes a float which is finite: the fewest digits which read back as it, in full from 1e-4 to
|
|
88
|
+
// 1e16 and with an exponent beyond
|
|
89
|
+
inline void append_float(std::string& out, double value)
|
|
90
|
+
{
|
|
91
|
+
if (std::isnan(value))
|
|
92
|
+
{
|
|
93
|
+
out += "NaN";
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (std::isinf(value))
|
|
97
|
+
{
|
|
98
|
+
out += value > 0 ? "Infinity" : "-Infinity";
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
char buffer[48];
|
|
102
|
+
const char* end = std::to_chars(buffer, buffer + sizeof(buffer), value, std::chars_format::scientific).ptr;
|
|
103
|
+
const std::string_view scientific(buffer, static_cast<std::size_t>(end - buffer));
|
|
104
|
+
const std::size_t e = scientific.find('e');
|
|
105
|
+
int exponent = 0;
|
|
106
|
+
for (const char digit : scientific.substr(e + 2))
|
|
107
|
+
exponent = exponent * 10 + (digit - '0');
|
|
108
|
+
if (scientific[e + 1] == '-')
|
|
109
|
+
exponent = -exponent;
|
|
110
|
+
if (exponent < -4 || exponent >= 16)
|
|
111
|
+
{
|
|
112
|
+
out += scientific;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
end = std::to_chars(buffer, buffer + sizeof(buffer), value, std::chars_format::fixed).ptr;
|
|
116
|
+
out.append(static_cast<const char*>(buffer), end);
|
|
117
|
+
if (std::find(static_cast<const char*>(buffer), end, '.') == end)
|
|
118
|
+
out += ".0";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
struct Writer
|
|
122
|
+
{
|
|
123
|
+
std::string out;
|
|
124
|
+
// How many objects which are not frozen what is being written is within
|
|
125
|
+
int depth = 0;
|
|
126
|
+
|
|
127
|
+
// Within quotes, with what JSON does not allow in a string escaped: as Python escapes it
|
|
128
|
+
void string(std::string_view text)
|
|
129
|
+
{
|
|
130
|
+
if (!msgpack::detail::is_utf8(text))
|
|
131
|
+
throw Error("A str which is not UTF-8 cannot be written");
|
|
132
|
+
out += '"';
|
|
133
|
+
for (const char c : text)
|
|
134
|
+
switch (c)
|
|
135
|
+
{
|
|
136
|
+
case '"':
|
|
137
|
+
out += "\\\"";
|
|
138
|
+
break;
|
|
139
|
+
case '\\':
|
|
140
|
+
out += "\\\\";
|
|
141
|
+
break;
|
|
142
|
+
case '\n':
|
|
143
|
+
out += "\\n";
|
|
144
|
+
break;
|
|
145
|
+
case '\r':
|
|
146
|
+
out += "\\r";
|
|
147
|
+
break;
|
|
148
|
+
case '\t':
|
|
149
|
+
out += "\\t";
|
|
150
|
+
break;
|
|
151
|
+
case '\b':
|
|
152
|
+
out += "\\b";
|
|
153
|
+
break;
|
|
154
|
+
case '\f':
|
|
155
|
+
out += "\\f";
|
|
156
|
+
break;
|
|
157
|
+
default:
|
|
158
|
+
if (static_cast<unsigned char>(c) < 0x20)
|
|
159
|
+
{
|
|
160
|
+
constexpr std::string_view digits = "0123456789abcdef";
|
|
161
|
+
out += "\\u00";
|
|
162
|
+
out += digits[static_cast<unsigned char>(c) >> 4];
|
|
163
|
+
out += digits[static_cast<unsigned char>(c) & 15];
|
|
164
|
+
}
|
|
165
|
+
else
|
|
166
|
+
out += c;
|
|
167
|
+
}
|
|
168
|
+
out += '"';
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
template <std::integral I>
|
|
172
|
+
void integer(I value)
|
|
173
|
+
{
|
|
174
|
+
char buffer[24];
|
|
175
|
+
out.append(buffer, std::to_chars(buffer, buffer + sizeof(buffer), value).ptr);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// A number which is finite, of the text JSON has for one: Mismatch if it is too great to be
|
|
180
|
+
inline double real(std::string_view text)
|
|
181
|
+
{
|
|
182
|
+
// As an integer has no nought which is negative, and one with no fraction or exponent is read as one
|
|
183
|
+
if (text == "-0")
|
|
184
|
+
return 0.0;
|
|
185
|
+
double value = 0;
|
|
186
|
+
const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), value);
|
|
187
|
+
if (error == std::errc::result_out_of_range)
|
|
188
|
+
{
|
|
189
|
+
// Too great, or too small: which is nought, or nearly
|
|
190
|
+
value = std::strtod(std::string(text).c_str(), nullptr);
|
|
191
|
+
if (std::isinf(value))
|
|
192
|
+
throw Mismatch{};
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
if (error != std::errc{} || end != text.data() + text.size())
|
|
196
|
+
throw Mismatch{};
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
inline double not_finite(std::string_view name)
|
|
201
|
+
{
|
|
202
|
+
if (name == "NaN")
|
|
203
|
+
return std::numeric_limits<double>::quiet_NaN();
|
|
204
|
+
if (name == "Infinity")
|
|
205
|
+
return std::numeric_limits<double>::infinity();
|
|
206
|
+
if (name == "-Infinity")
|
|
207
|
+
return -std::numeric_limits<double>::infinity();
|
|
208
|
+
throw Mismatch{};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
struct Reader
|
|
212
|
+
{
|
|
213
|
+
const char* at;
|
|
214
|
+
const char* end;
|
|
215
|
+
int depth = 0;
|
|
216
|
+
// What the last string read is held in, if it had escapes
|
|
217
|
+
std::string scratch{};
|
|
218
|
+
|
|
219
|
+
// While one exists, what is read is within an array or an object
|
|
220
|
+
class Nested
|
|
221
|
+
{
|
|
222
|
+
public:
|
|
223
|
+
explicit Nested(Reader& reader) : reader_(reader)
|
|
224
|
+
{
|
|
225
|
+
if (++reader_.depth > max_depth)
|
|
226
|
+
malformed("it is too deeply nested");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
Nested(const Nested&) = delete;
|
|
230
|
+
Nested& operator=(const Nested&) = delete;
|
|
231
|
+
|
|
232
|
+
~Nested()
|
|
233
|
+
{
|
|
234
|
+
--reader_.depth;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private:
|
|
238
|
+
Reader& reader_;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
void space()
|
|
242
|
+
{
|
|
243
|
+
while (at != end && (*at == ' ' || *at == '\t' || *at == '\n' || *at == '\r'))
|
|
244
|
+
++at;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
char peek()
|
|
248
|
+
{
|
|
249
|
+
space();
|
|
250
|
+
if (at == end)
|
|
251
|
+
malformed("it ends before its value does");
|
|
252
|
+
return *at;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
void expect(char c)
|
|
256
|
+
{
|
|
257
|
+
if (peek() != c)
|
|
258
|
+
malformed(std::string("there is no ") + c + " where one must be");
|
|
259
|
+
++at;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
bool word(std::string_view text)
|
|
263
|
+
{
|
|
264
|
+
if (static_cast<std::size_t>(end - at) < text.size() || std::string_view(at, text.size()) != text)
|
|
265
|
+
return false;
|
|
266
|
+
at += text.size();
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// The kind of the value which is next: a string of any form is a String
|
|
271
|
+
unsigned kind()
|
|
272
|
+
{
|
|
273
|
+
switch (peek())
|
|
274
|
+
{
|
|
275
|
+
case 'n':
|
|
276
|
+
return Null;
|
|
277
|
+
case 't':
|
|
278
|
+
case 'f':
|
|
279
|
+
return Bool;
|
|
280
|
+
case '"':
|
|
281
|
+
return String;
|
|
282
|
+
case '[':
|
|
283
|
+
return List;
|
|
284
|
+
case '{':
|
|
285
|
+
return Object;
|
|
286
|
+
default:
|
|
287
|
+
{
|
|
288
|
+
const char* start = at;
|
|
289
|
+
const bool floating = number().second;
|
|
290
|
+
at = start;
|
|
291
|
+
return floating ? Float : Int;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
void null()
|
|
297
|
+
{
|
|
298
|
+
space();
|
|
299
|
+
if (!word("null"))
|
|
300
|
+
malformed("a value is not one");
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
bool boolean()
|
|
304
|
+
{
|
|
305
|
+
space();
|
|
306
|
+
if (word("true"))
|
|
307
|
+
return true;
|
|
308
|
+
if (!word("false"))
|
|
309
|
+
malformed("a value is not one");
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// The text of a number, and whether it has a fraction or an exponent
|
|
314
|
+
std::pair<std::string_view, bool> number()
|
|
315
|
+
{
|
|
316
|
+
space();
|
|
317
|
+
const char* start = at;
|
|
318
|
+
const auto digits = [&]
|
|
319
|
+
{
|
|
320
|
+
const char* first = at;
|
|
321
|
+
while (at != end && *at >= '0' && *at <= '9')
|
|
322
|
+
++at;
|
|
323
|
+
if (at == first)
|
|
324
|
+
malformed("a value is not one");
|
|
325
|
+
};
|
|
326
|
+
if (at != end && *at == '-')
|
|
327
|
+
++at;
|
|
328
|
+
if (at != end && *at == '0')
|
|
329
|
+
++at;
|
|
330
|
+
else
|
|
331
|
+
digits();
|
|
332
|
+
bool floating = false;
|
|
333
|
+
if (at != end && *at == '.')
|
|
334
|
+
{
|
|
335
|
+
floating = true;
|
|
336
|
+
++at;
|
|
337
|
+
digits();
|
|
338
|
+
}
|
|
339
|
+
if (at != end && (*at == 'e' || *at == 'E'))
|
|
340
|
+
{
|
|
341
|
+
floating = true;
|
|
342
|
+
++at;
|
|
343
|
+
if (at != end && (*at == '+' || *at == '-'))
|
|
344
|
+
++at;
|
|
345
|
+
digits();
|
|
346
|
+
}
|
|
347
|
+
return {{start, static_cast<std::size_t>(at - start)}, floating};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// A string, without its quotes and escapes: which holds only until the next is read
|
|
351
|
+
std::string_view string()
|
|
352
|
+
{
|
|
353
|
+
expect('"');
|
|
354
|
+
const char* start = at;
|
|
355
|
+
while (at != end && *at != '"' && *at != '\\' && static_cast<unsigned char>(*at) >= 0x20)
|
|
356
|
+
++at;
|
|
357
|
+
if (at != end && *at == '"')
|
|
358
|
+
return {start, static_cast<std::size_t>(at++ - start)};
|
|
359
|
+
|
|
360
|
+
scratch.assign(start, at);
|
|
361
|
+
for (;;)
|
|
362
|
+
{
|
|
363
|
+
if (at == end)
|
|
364
|
+
malformed("a string does not end");
|
|
365
|
+
const char c = *at++;
|
|
366
|
+
if (c == '"')
|
|
367
|
+
return scratch;
|
|
368
|
+
if (static_cast<unsigned char>(c) < 0x20)
|
|
369
|
+
malformed("a string has a control character");
|
|
370
|
+
if (c != '\\')
|
|
371
|
+
{
|
|
372
|
+
scratch += c;
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (at == end)
|
|
376
|
+
malformed("a string does not end");
|
|
377
|
+
switch (*at++)
|
|
378
|
+
{
|
|
379
|
+
case '"':
|
|
380
|
+
scratch += '"';
|
|
381
|
+
break;
|
|
382
|
+
case '\\':
|
|
383
|
+
scratch += '\\';
|
|
384
|
+
break;
|
|
385
|
+
case '/':
|
|
386
|
+
scratch += '/';
|
|
387
|
+
break;
|
|
388
|
+
case 'b':
|
|
389
|
+
scratch += '\b';
|
|
390
|
+
break;
|
|
391
|
+
case 'f':
|
|
392
|
+
scratch += '\f';
|
|
393
|
+
break;
|
|
394
|
+
case 'n':
|
|
395
|
+
scratch += '\n';
|
|
396
|
+
break;
|
|
397
|
+
case 'r':
|
|
398
|
+
scratch += '\r';
|
|
399
|
+
break;
|
|
400
|
+
case 't':
|
|
401
|
+
scratch += '\t';
|
|
402
|
+
break;
|
|
403
|
+
case 'u':
|
|
404
|
+
escaped();
|
|
405
|
+
break;
|
|
406
|
+
default:
|
|
407
|
+
malformed("a string has an escape which is not one");
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// The key of an object's entry, as string() gives it, having passed the colon after it
|
|
413
|
+
std::string_view key()
|
|
414
|
+
{
|
|
415
|
+
if (peek() != '"')
|
|
416
|
+
malformed("an object has a key which is not a string");
|
|
417
|
+
const std::string_view name = string();
|
|
418
|
+
expect(':');
|
|
419
|
+
return name;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Whether an array or an object, which close is the end of, has another element: having passed what
|
|
423
|
+
// separates it from the last
|
|
424
|
+
bool more(char close, bool& first)
|
|
425
|
+
{
|
|
426
|
+
if (peek() == close)
|
|
427
|
+
{
|
|
428
|
+
++at;
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
if (!std::exchange(first, false))
|
|
432
|
+
expect(',');
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Passes the value which is next
|
|
437
|
+
void skip()
|
|
438
|
+
{
|
|
439
|
+
switch (kind())
|
|
440
|
+
{
|
|
441
|
+
case Null:
|
|
442
|
+
null();
|
|
443
|
+
break;
|
|
444
|
+
case Bool:
|
|
445
|
+
boolean();
|
|
446
|
+
break;
|
|
447
|
+
case String:
|
|
448
|
+
string();
|
|
449
|
+
break;
|
|
450
|
+
case List:
|
|
451
|
+
{
|
|
452
|
+
const Nested nested(*this);
|
|
453
|
+
++at;
|
|
454
|
+
for (bool first = true; more(']', first);)
|
|
455
|
+
skip();
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case Object:
|
|
459
|
+
{
|
|
460
|
+
const Nested nested(*this);
|
|
461
|
+
++at;
|
|
462
|
+
for (bool first = true; more('}', first);)
|
|
463
|
+
{
|
|
464
|
+
key();
|
|
465
|
+
skip();
|
|
466
|
+
}
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
default:
|
|
470
|
+
number();
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
void finish()
|
|
475
|
+
{
|
|
476
|
+
space();
|
|
477
|
+
if (at != end)
|
|
478
|
+
malformed("it has more than one value");
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
private:
|
|
482
|
+
unsigned hex()
|
|
483
|
+
{
|
|
484
|
+
unsigned value = 0;
|
|
485
|
+
for (int place = 0; place != 4; ++place)
|
|
486
|
+
{
|
|
487
|
+
if (at == end)
|
|
488
|
+
malformed("a string does not end");
|
|
489
|
+
const char c = *at++;
|
|
490
|
+
const int digit = c >= '0' && c <= '9' ? c - '0'
|
|
491
|
+
: c >= 'a' && c <= 'f' ? c - 'a' + 10
|
|
492
|
+
: c >= 'A' && c <= 'F' ? c - 'A' + 10
|
|
493
|
+
: -1;
|
|
494
|
+
if (digit < 0)
|
|
495
|
+
malformed("a string has an escape which is not one");
|
|
496
|
+
value = value * 16 + static_cast<unsigned>(digit);
|
|
497
|
+
}
|
|
498
|
+
return value;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Adds to scratch, as UTF-8, the character which \u is the escape of: with that after it, if of two
|
|
502
|
+
void escaped()
|
|
503
|
+
{
|
|
504
|
+
unsigned code = hex();
|
|
505
|
+
if (code >= 0xD800 && code < 0xDC00 && end - at >= 2 && at[0] == '\\' && at[1] == 'u')
|
|
506
|
+
{
|
|
507
|
+
at += 2;
|
|
508
|
+
const unsigned low = hex();
|
|
509
|
+
if (low < 0xDC00 || low >= 0xE000)
|
|
510
|
+
malformed("a string has half of a character");
|
|
511
|
+
code = 0x10000 + ((code - 0xD800) << 10) + (low - 0xDC00);
|
|
512
|
+
}
|
|
513
|
+
else if (code >= 0xD800 && code < 0xE000)
|
|
514
|
+
malformed("a string has half of a character");
|
|
515
|
+
|
|
516
|
+
if (code < 0x80)
|
|
517
|
+
scratch += static_cast<char>(code);
|
|
518
|
+
else if (code < 0x800)
|
|
519
|
+
{
|
|
520
|
+
scratch += static_cast<char>(0xC0 | (code >> 6));
|
|
521
|
+
scratch += static_cast<char>(0x80 | (code & 0x3F));
|
|
522
|
+
}
|
|
523
|
+
else if (code < 0x10000)
|
|
524
|
+
{
|
|
525
|
+
scratch += static_cast<char>(0xE0 | (code >> 12));
|
|
526
|
+
scratch += static_cast<char>(0x80 | ((code >> 6) & 0x3F));
|
|
527
|
+
scratch += static_cast<char>(0x80 | (code & 0x3F));
|
|
528
|
+
}
|
|
529
|
+
else
|
|
530
|
+
{
|
|
531
|
+
scratch += static_cast<char>(0xF0 | (code >> 18));
|
|
532
|
+
scratch += static_cast<char>(0x80 | ((code >> 12) & 0x3F));
|
|
533
|
+
scratch += static_cast<char>(0x80 | ((code >> 6) & 0x3F));
|
|
534
|
+
scratch += static_cast<char>(0x80 | (code & 0x3F));
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
// Digits, to a width: and the number which digits alone are, of no more than nine
|
|
540
|
+
inline void padded(std::string& out, std::int64_t value, std::size_t width)
|
|
541
|
+
{
|
|
542
|
+
const std::string digits = std::to_string(value);
|
|
543
|
+
if (digits.size() < width)
|
|
544
|
+
out.append(width - digits.size(), '0');
|
|
545
|
+
out += digits;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
inline int digits(std::string_view text)
|
|
549
|
+
{
|
|
550
|
+
if (text.empty() || text.size() > 9)
|
|
551
|
+
throw Mismatch{};
|
|
552
|
+
int value = 0;
|
|
553
|
+
for (const char c : text)
|
|
554
|
+
{
|
|
555
|
+
if (c < '0' || c > '9')
|
|
556
|
+
throw Mismatch{};
|
|
557
|
+
value = value * 10 + (c - '0');
|
|
558
|
+
}
|
|
559
|
+
return value;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// A date, as 2026-01-02: of the years 1 to 9999, as Python's are
|
|
563
|
+
inline void write_date(std::string& out, std::chrono::sys_days day)
|
|
564
|
+
{
|
|
565
|
+
const std::chrono::year_month_day date{day};
|
|
566
|
+
const int year = static_cast<int>(date.year());
|
|
567
|
+
if (year < 1 || year > 9999)
|
|
568
|
+
throw Error("A date which is not of the years 1 to 9999 cannot be written");
|
|
569
|
+
padded(out, year, 4);
|
|
570
|
+
out += '-';
|
|
571
|
+
padded(out, static_cast<unsigned>(date.month()), 2);
|
|
572
|
+
out += '-';
|
|
573
|
+
padded(out, static_cast<unsigned>(date.day()), 2);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
inline std::chrono::sys_days read_date(std::string_view text)
|
|
577
|
+
{
|
|
578
|
+
if (text.size() != 10 || text[4] != '-' || text[7] != '-')
|
|
579
|
+
throw Mismatch{};
|
|
580
|
+
const int year = digits(text.substr(0, 4));
|
|
581
|
+
const std::chrono::year_month_day date{std::chrono::year{year},
|
|
582
|
+
std::chrono::month{static_cast<unsigned>(digits(text.substr(5, 2)))},
|
|
583
|
+
std::chrono::day{static_cast<unsigned>(digits(text.substr(8, 2)))}};
|
|
584
|
+
if (year < 1 || !date.ok())
|
|
585
|
+
throw Mismatch{};
|
|
586
|
+
return std::chrono::sys_days{date};
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// A time of day, as 03:04:05, or 03:04:05.000006 if it has microseconds
|
|
590
|
+
inline void write_time(std::string& out, std::chrono::microseconds since_midnight)
|
|
591
|
+
{
|
|
592
|
+
const std::int64_t whole = since_midnight.count();
|
|
593
|
+
padded(out, whole / 3'600'000'000, 2);
|
|
594
|
+
out += ':';
|
|
595
|
+
padded(out, whole / 60'000'000 % 60, 2);
|
|
596
|
+
out += ':';
|
|
597
|
+
padded(out, whole / 1'000'000 % 60, 2);
|
|
598
|
+
if (whole % 1'000'000)
|
|
599
|
+
{
|
|
600
|
+
out += '.';
|
|
601
|
+
padded(out, whole % 1'000'000, 6);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
inline std::chrono::microseconds read_time(std::string_view text)
|
|
606
|
+
{
|
|
607
|
+
if (text.size() < 8 || text[2] != ':' || text[5] != ':' || text.size() == 9 || text.size() > 15 ||
|
|
608
|
+
(text.size() > 8 && text[8] != '.'))
|
|
609
|
+
throw Mismatch{};
|
|
610
|
+
const int hours = digits(text.substr(0, 2));
|
|
611
|
+
const int minutes = digits(text.substr(3, 2));
|
|
612
|
+
const int seconds = digits(text.substr(6, 2));
|
|
613
|
+
std::int64_t fraction = text.size() > 8 ? digits(text.substr(9)) : 0;
|
|
614
|
+
for (std::size_t width = text.size() > 8 ? text.size() - 9 : 6; width != 6; ++width)
|
|
615
|
+
fraction *= 10;
|
|
616
|
+
if (hours > 23 || minutes > 59 || seconds > 59)
|
|
617
|
+
throw Mismatch{};
|
|
618
|
+
return std::chrono::microseconds{((hours * 60LL + minutes) * 60 + seconds) * 1'000'000 + fraction};
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// What each type which is a string, or the key of an object, is as text: and that read
|
|
622
|
+
template <class T>
|
|
623
|
+
struct Text;
|
|
624
|
+
|
|
625
|
+
template <>
|
|
626
|
+
struct Text<std::string>
|
|
627
|
+
{
|
|
628
|
+
static constexpr unsigned kinds = String;
|
|
629
|
+
|
|
630
|
+
static void write(std::string& out, const std::string& value)
|
|
631
|
+
{
|
|
632
|
+
out += value;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
static std::string read(std::string_view text)
|
|
636
|
+
{
|
|
637
|
+
return std::string(text);
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
template <>
|
|
642
|
+
struct Text<bool>
|
|
643
|
+
{
|
|
644
|
+
static void write(std::string& out, bool value)
|
|
645
|
+
{
|
|
646
|
+
out += value ? "true" : "false";
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
static bool read(std::string_view text)
|
|
650
|
+
{
|
|
651
|
+
if (text != "true" && text != "false")
|
|
652
|
+
throw Mismatch{};
|
|
653
|
+
return text == "true";
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
template <std::integral T>
|
|
658
|
+
struct Text<T>
|
|
659
|
+
{
|
|
660
|
+
static void write(std::string& out, T value)
|
|
661
|
+
{
|
|
662
|
+
char buffer[24];
|
|
663
|
+
out.append(buffer, std::to_chars(buffer, buffer + sizeof(buffer), value).ptr);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// Of digits alone, with no nought before them
|
|
667
|
+
static T read(std::string_view text)
|
|
668
|
+
{
|
|
669
|
+
const std::string_view number = text.starts_with('-') ? text.substr(1) : text;
|
|
670
|
+
T value{};
|
|
671
|
+
if (number.empty() || (number.size() > 1 && number[0] == '0') ||
|
|
672
|
+
number.find_first_not_of("0123456789") != number.npos)
|
|
673
|
+
throw Mismatch{};
|
|
674
|
+
// Which is nought, though a type with no sign has no minus
|
|
675
|
+
if (text == "-0")
|
|
676
|
+
return value;
|
|
677
|
+
const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), value);
|
|
678
|
+
if (error != std::errc{} || end != text.data() + text.size())
|
|
679
|
+
throw Mismatch{};
|
|
680
|
+
return value;
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
template <std::floating_point T>
|
|
685
|
+
struct Text<T>
|
|
686
|
+
{
|
|
687
|
+
static void write(std::string& out, T value)
|
|
688
|
+
{
|
|
689
|
+
append_float(out, value);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
static T read(std::string_view text)
|
|
693
|
+
{
|
|
694
|
+
if (text == "NaN" || text == "Infinity" || text == "-Infinity")
|
|
695
|
+
return static_cast<T>(not_finite(text));
|
|
696
|
+
// As JSON has a number, with space about it or not
|
|
697
|
+
Reader reader{text.data(), text.data() + text.size()};
|
|
698
|
+
try
|
|
699
|
+
{
|
|
700
|
+
const std::string_view number = reader.number().first;
|
|
701
|
+
reader.finish();
|
|
702
|
+
return narrowed(real(number));
|
|
703
|
+
}
|
|
704
|
+
catch (const Error&)
|
|
705
|
+
{
|
|
706
|
+
throw Mismatch{};
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// Mismatch, if it is too great for a float of 32 bits: which is to be nearer to what is beyond the
|
|
711
|
+
// greatest than to it
|
|
712
|
+
static T narrowed(double value)
|
|
713
|
+
{
|
|
714
|
+
if constexpr (sizeof(T) < sizeof(double))
|
|
715
|
+
if (std::abs(value) >= 0x1.ffffffp127)
|
|
716
|
+
throw Mismatch{};
|
|
717
|
+
return static_cast<T>(value);
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
// As base64, of the alphabet which is safe in a URL: read from either
|
|
722
|
+
template <>
|
|
723
|
+
struct Text<Bytes>
|
|
724
|
+
{
|
|
725
|
+
static constexpr unsigned kinds = String;
|
|
726
|
+
|
|
727
|
+
static void write(std::string& out, std::span<const std::uint8_t> value)
|
|
728
|
+
{
|
|
729
|
+
constexpr std::string_view alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
730
|
+
for (std::size_t at = 0; at < value.size(); at += 3)
|
|
731
|
+
{
|
|
732
|
+
const std::size_t count = std::min<std::size_t>(3, value.size() - at);
|
|
733
|
+
unsigned group = 0;
|
|
734
|
+
for (std::size_t i = 0; i != 3; ++i)
|
|
735
|
+
group = (group << 8) | (i < count ? value[at + i] : 0U);
|
|
736
|
+
for (std::size_t i = 0; i != 4; ++i)
|
|
737
|
+
out += i <= count ? alphabet[(group >> (18 - 6 * i)) & 63] : '=';
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
static Bytes read(std::string_view text)
|
|
742
|
+
{
|
|
743
|
+
if (text.size() % 4)
|
|
744
|
+
throw Mismatch{};
|
|
745
|
+
const std::size_t padding = text.ends_with("==") ? 2 : text.ends_with('=') ? 1 : 0;
|
|
746
|
+
Bytes value;
|
|
747
|
+
value.reserve(text.size() / 4 * 3);
|
|
748
|
+
for (std::size_t at = 0; at != text.size(); at += 4)
|
|
749
|
+
{
|
|
750
|
+
const std::size_t count = at + 4 == text.size() ? 4 - padding : 4;
|
|
751
|
+
unsigned group = 0;
|
|
752
|
+
for (std::size_t i = 0; i != 4; ++i)
|
|
753
|
+
group = (group << 6) | (i < count ? sextet(text[at + i]) : 0U);
|
|
754
|
+
for (std::size_t i = 0; i + 1 < count; ++i)
|
|
755
|
+
value.push_back(static_cast<std::uint8_t>(group >> (16 - 8 * i)));
|
|
756
|
+
}
|
|
757
|
+
return value;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
private:
|
|
761
|
+
static unsigned sextet(char c)
|
|
762
|
+
{
|
|
763
|
+
if (c >= 'A' && c <= 'Z')
|
|
764
|
+
return static_cast<unsigned>(c - 'A');
|
|
765
|
+
if (c >= 'a' && c <= 'z')
|
|
766
|
+
return static_cast<unsigned>(c - 'a' + 26);
|
|
767
|
+
if (c >= '0' && c <= '9')
|
|
768
|
+
return static_cast<unsigned>(c - '0' + 52);
|
|
769
|
+
if (c == '-' || c == '+')
|
|
770
|
+
return 62;
|
|
771
|
+
if (c == '_' || c == '/')
|
|
772
|
+
return 63;
|
|
773
|
+
throw Mismatch{};
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
|
|
777
|
+
template <>
|
|
778
|
+
struct Text<Date>
|
|
779
|
+
{
|
|
780
|
+
static constexpr unsigned kinds = DateForm;
|
|
781
|
+
|
|
782
|
+
// Whether text is of this form, if of any: so that a union's member is told without being read
|
|
783
|
+
static bool formed(std::string_view text)
|
|
784
|
+
{
|
|
785
|
+
return text.size() == 10 && text[4] == '-';
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
static void write(std::string& out, Date value)
|
|
789
|
+
{
|
|
790
|
+
write_date(out, value);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
static Date read(std::string_view text)
|
|
794
|
+
{
|
|
795
|
+
return read_date(text);
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
|
|
799
|
+
template <>
|
|
800
|
+
struct Text<TimeOfDay>
|
|
801
|
+
{
|
|
802
|
+
static constexpr unsigned kinds = TimeForm;
|
|
803
|
+
|
|
804
|
+
// Whether text is of this form, if of any: so that a union's member is told without being read
|
|
805
|
+
static bool formed(std::string_view text)
|
|
806
|
+
{
|
|
807
|
+
return text.size() >= 3 && text[2] == ':';
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
static void write(std::string& out, TimeOfDay value)
|
|
811
|
+
{
|
|
812
|
+
if (value.since_midnight.count() < 0 || value.since_midnight.count() >= day_microseconds)
|
|
813
|
+
throw Error("A time which is not within a day cannot be written");
|
|
814
|
+
write_time(out, value.since_midnight);
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
static TimeOfDay read(std::string_view text)
|
|
818
|
+
{
|
|
819
|
+
return {read_time(text)};
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
// Whether text ends with the offset of a timezone: Z, or as +01:00
|
|
824
|
+
inline bool zoned(std::string_view text)
|
|
825
|
+
{
|
|
826
|
+
return text.ends_with('Z') || (text.size() >= 6 && text[text.size() - 3] == ':' &&
|
|
827
|
+
(text[text.size() - 6] == '+' || text[text.size() - 6] == '-'));
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// A date and a time of day, with T between them, of no timezone
|
|
831
|
+
template <>
|
|
832
|
+
struct Text<NaiveDateTime>
|
|
833
|
+
{
|
|
834
|
+
static constexpr unsigned kinds = NaiveDateTimeForm;
|
|
835
|
+
|
|
836
|
+
// Whether text is of this form, if of any: so that a union's member is told without being read
|
|
837
|
+
static bool formed(std::string_view text)
|
|
838
|
+
{
|
|
839
|
+
return text.size() > 10 && text[10] == 'T' && !zoned(text);
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
static void write(std::string& out, NaiveDateTime value)
|
|
843
|
+
{
|
|
844
|
+
const auto since_epoch = value.time_since_epoch();
|
|
845
|
+
const auto day = std::chrono::floor<std::chrono::days>(since_epoch);
|
|
846
|
+
write_date(out, std::chrono::sys_days{day});
|
|
847
|
+
out += 'T';
|
|
848
|
+
write_time(out, since_epoch - day);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
static NaiveDateTime read(std::string_view text)
|
|
852
|
+
{
|
|
853
|
+
if (text.size() < 11 || text[10] != 'T')
|
|
854
|
+
throw Mismatch{};
|
|
855
|
+
return NaiveDateTime{read_date(text.substr(0, 10)).time_since_epoch() + read_time(text.substr(11))};
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
// As one of no timezone, in UTC, then Z: read with any offset
|
|
860
|
+
template <>
|
|
861
|
+
struct Text<DateTime>
|
|
862
|
+
{
|
|
863
|
+
static constexpr unsigned kinds = DateTimeForm;
|
|
864
|
+
|
|
865
|
+
// Whether text is of this form, if of any: so that a union's member is told without being read
|
|
866
|
+
static bool formed(std::string_view text)
|
|
867
|
+
{
|
|
868
|
+
return text.size() > 10 && text[10] == 'T' && zoned(text);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
static void write(std::string& out, DateTime value)
|
|
872
|
+
{
|
|
873
|
+
Text<NaiveDateTime>::write(out, NaiveDateTime{value.time_since_epoch()});
|
|
874
|
+
out += 'Z';
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
static DateTime read(std::string_view text)
|
|
878
|
+
{
|
|
879
|
+
std::chrono::minutes offset{};
|
|
880
|
+
if (text.ends_with('Z'))
|
|
881
|
+
text.remove_suffix(1);
|
|
882
|
+
else
|
|
883
|
+
{
|
|
884
|
+
if (text.size() < 6)
|
|
885
|
+
throw Mismatch{};
|
|
886
|
+
const std::string_view zone = text.substr(text.size() - 6);
|
|
887
|
+
if ((zone[0] != '+' && zone[0] != '-') || zone[3] != ':')
|
|
888
|
+
throw Mismatch{};
|
|
889
|
+
const int hours = digits(zone.substr(1, 2));
|
|
890
|
+
const int minutes = digits(zone.substr(4, 2));
|
|
891
|
+
if (hours > 23 || minutes > 59)
|
|
892
|
+
throw Mismatch{};
|
|
893
|
+
offset = std::chrono::minutes{(hours * 60 + minutes) * (zone[0] == '-' ? -1 : 1)};
|
|
894
|
+
text.remove_suffix(6);
|
|
895
|
+
}
|
|
896
|
+
// Of the years 1 to 9999, in UTC, as Python's are
|
|
897
|
+
constexpr std::chrono::sys_days first = std::chrono::year{1} / 1 / 1;
|
|
898
|
+
constexpr std::chrono::sys_days after = std::chrono::year{10000} / 1 / 1;
|
|
899
|
+
const DateTime value{Text<NaiveDateTime>::read(text).time_since_epoch() - offset};
|
|
900
|
+
if (value < first || value >= after)
|
|
901
|
+
throw Mismatch{};
|
|
902
|
+
return value;
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
// As ISO 8601 has a duration, of days, hours, minutes and seconds: read with years too, each of 365 days
|
|
907
|
+
template <>
|
|
908
|
+
struct Text<TimeDelta>
|
|
909
|
+
{
|
|
910
|
+
static constexpr unsigned kinds = DurationForm;
|
|
911
|
+
|
|
912
|
+
// Whether text is of this form, if of any: so that a union's member is told without being read
|
|
913
|
+
static bool formed(std::string_view text)
|
|
914
|
+
{
|
|
915
|
+
return text.starts_with('P') || text.starts_with("-P");
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
static void write(std::string& out, TimeDelta value)
|
|
919
|
+
{
|
|
920
|
+
const std::int64_t whole = value.count();
|
|
921
|
+
// Which the least of all has no positive of
|
|
922
|
+
std::uint64_t rest = whole < 0 ? 0 - static_cast<std::uint64_t>(whole) : static_cast<std::uint64_t>(whole);
|
|
923
|
+
const auto part = [&](std::uint64_t unit) { return std::exchange(rest, rest % unit) / unit; };
|
|
924
|
+
const std::uint64_t days = part(static_cast<std::uint64_t>(day_microseconds));
|
|
925
|
+
const std::uint64_t hours = part(3'600'000'000);
|
|
926
|
+
const std::uint64_t minutes = part(60'000'000);
|
|
927
|
+
const std::uint64_t seconds = part(1'000'000);
|
|
928
|
+
|
|
929
|
+
if (whole < 0)
|
|
930
|
+
out += '-';
|
|
931
|
+
out += 'P';
|
|
932
|
+
if (days)
|
|
933
|
+
out += std::to_string(days) + 'D';
|
|
934
|
+
if (hours || minutes || seconds || rest || !days)
|
|
935
|
+
out += 'T';
|
|
936
|
+
if (hours)
|
|
937
|
+
out += std::to_string(hours) + 'H';
|
|
938
|
+
if (minutes)
|
|
939
|
+
out += std::to_string(minutes) + 'M';
|
|
940
|
+
if (seconds || rest || !(days || hours || minutes))
|
|
941
|
+
{
|
|
942
|
+
out += std::to_string(seconds);
|
|
943
|
+
if (rest)
|
|
944
|
+
{
|
|
945
|
+
out += '.';
|
|
946
|
+
padded(out, static_cast<std::int64_t>(rest), 6);
|
|
947
|
+
}
|
|
948
|
+
out += 'S';
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
static TimeDelta read(std::string_view text)
|
|
953
|
+
{
|
|
954
|
+
constexpr std::string_view units = "YDHMS";
|
|
955
|
+
constexpr std::int64_t sizes[] = {365 * day_microseconds, day_microseconds, 3'600'000'000, 60'000'000,
|
|
956
|
+
1'000'000};
|
|
957
|
+
constexpr std::int64_t most = std::numeric_limits<std::int64_t>::max();
|
|
958
|
+
const bool negative = text.starts_with('-');
|
|
959
|
+
text.remove_prefix(negative ? 1 : 0);
|
|
960
|
+
if (!text.starts_with('P'))
|
|
961
|
+
throw Mismatch{};
|
|
962
|
+
text.remove_prefix(1);
|
|
963
|
+
|
|
964
|
+
std::int64_t total = 0;
|
|
965
|
+
std::size_t unit = 0;
|
|
966
|
+
// Whether T has been met, and whether what must follow it, or P, has
|
|
967
|
+
bool timed = false;
|
|
968
|
+
bool followed = false;
|
|
969
|
+
while (!text.empty())
|
|
970
|
+
{
|
|
971
|
+
if (text[0] == 'T')
|
|
972
|
+
{
|
|
973
|
+
if (std::exchange(timed, true))
|
|
974
|
+
throw Mismatch{};
|
|
975
|
+
followed = false;
|
|
976
|
+
text.remove_prefix(1);
|
|
977
|
+
continue;
|
|
978
|
+
}
|
|
979
|
+
std::int64_t number = 0;
|
|
980
|
+
std::size_t count = 0;
|
|
981
|
+
for (; count != text.size() && text[count] >= '0' && text[count] <= '9'; ++count)
|
|
982
|
+
{
|
|
983
|
+
if (number > (most - 9) / 10)
|
|
984
|
+
throw Mismatch{};
|
|
985
|
+
number = number * 10 + (text[count] - '0');
|
|
986
|
+
}
|
|
987
|
+
if (count == 0 || count == text.size())
|
|
988
|
+
throw Mismatch{};
|
|
989
|
+
text.remove_prefix(count);
|
|
990
|
+
|
|
991
|
+
std::int64_t fraction = 0;
|
|
992
|
+
const bool fractional = text[0] == '.';
|
|
993
|
+
if (fractional)
|
|
994
|
+
{
|
|
995
|
+
const std::size_t places = std::min(text.find_first_not_of("0123456789", 1), text.size()) - 1;
|
|
996
|
+
if (places < 1 || places > 6)
|
|
997
|
+
throw Mismatch{};
|
|
998
|
+
fraction = digits(text.substr(1, places));
|
|
999
|
+
for (std::size_t width = places; width != 6; ++width)
|
|
1000
|
+
fraction *= 10;
|
|
1001
|
+
text.remove_prefix(places + 1);
|
|
1002
|
+
if (text.empty())
|
|
1003
|
+
throw Mismatch{};
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// Each unit once, in their order: years and days before T, and the rest after it
|
|
1007
|
+
const std::size_t found = units.find(text[0], unit);
|
|
1008
|
+
if (found == units.npos || (found < 2) == timed || (fractional && found != 4))
|
|
1009
|
+
throw Mismatch{};
|
|
1010
|
+
if (number > (most - total - fraction) / sizes[found])
|
|
1011
|
+
throw Mismatch{};
|
|
1012
|
+
total += number * sizes[found] + fraction;
|
|
1013
|
+
unit = found + 1;
|
|
1014
|
+
followed = true;
|
|
1015
|
+
text.remove_prefix(1);
|
|
1016
|
+
}
|
|
1017
|
+
if (!followed)
|
|
1018
|
+
throw Mismatch{};
|
|
1019
|
+
return TimeDelta{negative ? -total : total};
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
|
|
1023
|
+
// As it is written: 12345678-1234-5678-1234-567812345678
|
|
1024
|
+
template <>
|
|
1025
|
+
struct Text<Uuid>
|
|
1026
|
+
{
|
|
1027
|
+
static constexpr unsigned kinds = UuidForm;
|
|
1028
|
+
|
|
1029
|
+
// Whether text is of this form, if of any: so that a union's member is told without being read
|
|
1030
|
+
static bool formed(std::string_view text)
|
|
1031
|
+
{
|
|
1032
|
+
return text.size() == 36 && text[8] == '-';
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
static void write(std::string& out, const Uuid& value)
|
|
1036
|
+
{
|
|
1037
|
+
constexpr std::string_view digits = "0123456789abcdef";
|
|
1038
|
+
for (std::size_t i = 0; i != value.bytes.size(); ++i)
|
|
1039
|
+
{
|
|
1040
|
+
if (i == 4 || i == 6 || i == 8 || i == 10)
|
|
1041
|
+
out += '-';
|
|
1042
|
+
out += digits[value.bytes[i] >> 4];
|
|
1043
|
+
out += digits[value.bytes[i] & 15];
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
static Uuid read(std::string_view text)
|
|
1048
|
+
{
|
|
1049
|
+
if (text.size() != 36)
|
|
1050
|
+
throw Mismatch{};
|
|
1051
|
+
Uuid value;
|
|
1052
|
+
std::size_t at = 0;
|
|
1053
|
+
for (std::size_t i = 0; i != value.bytes.size(); ++i)
|
|
1054
|
+
{
|
|
1055
|
+
if (i == 4 || i == 6 || i == 8 || i == 10)
|
|
1056
|
+
if (text[at++] != '-')
|
|
1057
|
+
throw Mismatch{};
|
|
1058
|
+
value.bytes[i] = static_cast<std::uint8_t>(nibble(text[at]) << 4 | nibble(text[at + 1]));
|
|
1059
|
+
at += 2;
|
|
1060
|
+
}
|
|
1061
|
+
return value;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
private:
|
|
1065
|
+
static unsigned nibble(char c)
|
|
1066
|
+
{
|
|
1067
|
+
if (c >= '0' && c <= '9')
|
|
1068
|
+
return static_cast<unsigned>(c - '0');
|
|
1069
|
+
if (c >= 'a' && c <= 'f')
|
|
1070
|
+
return static_cast<unsigned>(c - 'a' + 10);
|
|
1071
|
+
if (c >= 'A' && c <= 'F')
|
|
1072
|
+
return static_cast<unsigned>(c - 'A' + 10);
|
|
1073
|
+
throw Mismatch{};
|
|
1074
|
+
}
|
|
1075
|
+
};
|
|
1076
|
+
|
|
1077
|
+
// As its digits, with a point: read whatever the places of what reads it
|
|
1078
|
+
template <int Places>
|
|
1079
|
+
struct Text<Decimal<Places>>
|
|
1080
|
+
{
|
|
1081
|
+
static constexpr unsigned kinds = DecimalForm;
|
|
1082
|
+
|
|
1083
|
+
// Whether text is of this form, if of any: so that a union's member is told without being read
|
|
1084
|
+
static bool formed(std::string_view text)
|
|
1085
|
+
{
|
|
1086
|
+
return !text.empty() && text.find_first_not_of("0123456789.", text[0] == '-' ? 1 : 0) == text.npos;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
static void write(std::string& out, Decimal<Places> value)
|
|
1090
|
+
{
|
|
1091
|
+
if (!value.fits())
|
|
1092
|
+
throw Error("A Decimal of more than 18 digits cannot be written");
|
|
1093
|
+
out += value.str();
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
static Decimal<Places> read(std::string_view text)
|
|
1097
|
+
{
|
|
1098
|
+
const std::optional<Decimal<Places>> value = Decimal<Places>::parse(text);
|
|
1099
|
+
if (!value)
|
|
1100
|
+
throw Mismatch{};
|
|
1101
|
+
return *value;
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
|
|
1105
|
+
// An enum: as the text of its member's value, if those are strings or integers, else its member's name
|
|
1106
|
+
template <Enum T>
|
|
1107
|
+
struct Text<T>
|
|
1108
|
+
{
|
|
1109
|
+
static void write(std::string& out, T value)
|
|
1110
|
+
{
|
|
1111
|
+
for (const auto& member : Reflect<T>::members)
|
|
1112
|
+
if (member.value == value)
|
|
1113
|
+
{
|
|
1114
|
+
out += member.text;
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
throw Error(std::string(Reflect<T>::name) + " has no such member to write");
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
static T read(std::string_view text)
|
|
1121
|
+
{
|
|
1122
|
+
for (const auto& member : Reflect<T>::members)
|
|
1123
|
+
if (member.text == text)
|
|
1124
|
+
return member.value;
|
|
1125
|
+
throw Mismatch{};
|
|
1126
|
+
}
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1129
|
+
// Writes a value of each type, and reads it. Its kinds are those of JSON value which it is written as
|
|
1130
|
+
template <class T>
|
|
1131
|
+
struct Codec;
|
|
1132
|
+
|
|
1133
|
+
template <>
|
|
1134
|
+
struct Codec<bool>
|
|
1135
|
+
{
|
|
1136
|
+
static constexpr unsigned kinds = Bool;
|
|
1137
|
+
|
|
1138
|
+
static void write(Writer& writer, bool value)
|
|
1139
|
+
{
|
|
1140
|
+
writer.out += value ? "true" : "false";
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
static bool read(Reader& reader)
|
|
1144
|
+
{
|
|
1145
|
+
if (reader.kind() != Bool)
|
|
1146
|
+
throw Mismatch{};
|
|
1147
|
+
return reader.boolean();
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
|
|
1151
|
+
template <std::integral T>
|
|
1152
|
+
struct Codec<T>
|
|
1153
|
+
{
|
|
1154
|
+
static constexpr unsigned kinds = Int;
|
|
1155
|
+
|
|
1156
|
+
static void write(Writer& writer, T value)
|
|
1157
|
+
{
|
|
1158
|
+
writer.integer(value);
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
// Not from a number with a fraction or an exponent
|
|
1162
|
+
static T read(Reader& reader)
|
|
1163
|
+
{
|
|
1164
|
+
if (reader.kind() != Int)
|
|
1165
|
+
throw Mismatch{};
|
|
1166
|
+
return Text<T>::read(reader.number().first);
|
|
1167
|
+
}
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
// A number with a fraction or an exponent, or a string if it is not finite, which JSON has no number for.
|
|
1171
|
+
// Read from a number of either kind
|
|
1172
|
+
template <std::floating_point T>
|
|
1173
|
+
struct Codec<T>
|
|
1174
|
+
{
|
|
1175
|
+
static constexpr unsigned kinds = Float;
|
|
1176
|
+
|
|
1177
|
+
static void write(Writer& writer, T value)
|
|
1178
|
+
{
|
|
1179
|
+
if (std::isfinite(value))
|
|
1180
|
+
append_float(writer.out, value);
|
|
1181
|
+
else
|
|
1182
|
+
{
|
|
1183
|
+
writer.out += '"';
|
|
1184
|
+
append_float(writer.out, value);
|
|
1185
|
+
writer.out += '"';
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
static T read(Reader& reader)
|
|
1190
|
+
{
|
|
1191
|
+
switch (reader.kind())
|
|
1192
|
+
{
|
|
1193
|
+
case Int:
|
|
1194
|
+
case Float:
|
|
1195
|
+
return Text<T>::narrowed(real(reader.number().first));
|
|
1196
|
+
case String:
|
|
1197
|
+
return static_cast<T>(not_finite(reader.string()));
|
|
1198
|
+
default:
|
|
1199
|
+
throw Mismatch{};
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
};
|
|
1203
|
+
|
|
1204
|
+
template <>
|
|
1205
|
+
struct Codec<std::string>
|
|
1206
|
+
{
|
|
1207
|
+
static constexpr unsigned kinds = String;
|
|
1208
|
+
|
|
1209
|
+
static void write(Writer& writer, const std::string& value)
|
|
1210
|
+
{
|
|
1211
|
+
writer.string(value);
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
static std::string read(Reader& reader)
|
|
1215
|
+
{
|
|
1216
|
+
if (reader.kind() != String)
|
|
1217
|
+
throw Mismatch{};
|
|
1218
|
+
return std::string(reader.string());
|
|
1219
|
+
}
|
|
1220
|
+
};
|
|
1221
|
+
|
|
1222
|
+
// A type which is written as a string, of nothing which need be escaped
|
|
1223
|
+
template <class T>
|
|
1224
|
+
struct TextCodec
|
|
1225
|
+
{
|
|
1226
|
+
static constexpr unsigned kinds = Text<T>::kinds;
|
|
1227
|
+
|
|
1228
|
+
static void write(Writer& writer, const T& value)
|
|
1229
|
+
{
|
|
1230
|
+
writer.out += '"';
|
|
1231
|
+
Text<T>::write(writer.out, value);
|
|
1232
|
+
writer.out += '"';
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
static T read(Reader& reader)
|
|
1236
|
+
{
|
|
1237
|
+
if (reader.kind() != String)
|
|
1238
|
+
throw Mismatch{};
|
|
1239
|
+
return Text<T>::read(reader.string());
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
1242
|
+
|
|
1243
|
+
template <>
|
|
1244
|
+
struct Codec<Bytes> : TextCodec<Bytes>
|
|
1245
|
+
{
|
|
1246
|
+
};
|
|
1247
|
+
|
|
1248
|
+
template <>
|
|
1249
|
+
struct Codec<Date> : TextCodec<Date>
|
|
1250
|
+
{
|
|
1251
|
+
};
|
|
1252
|
+
|
|
1253
|
+
template <>
|
|
1254
|
+
struct Codec<TimeOfDay> : TextCodec<TimeOfDay>
|
|
1255
|
+
{
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1258
|
+
template <>
|
|
1259
|
+
struct Codec<DateTime> : TextCodec<DateTime>
|
|
1260
|
+
{
|
|
1261
|
+
};
|
|
1262
|
+
|
|
1263
|
+
template <>
|
|
1264
|
+
struct Codec<NaiveDateTime> : TextCodec<NaiveDateTime>
|
|
1265
|
+
{
|
|
1266
|
+
};
|
|
1267
|
+
|
|
1268
|
+
template <>
|
|
1269
|
+
struct Codec<TimeDelta> : TextCodec<TimeDelta>
|
|
1270
|
+
{
|
|
1271
|
+
};
|
|
1272
|
+
|
|
1273
|
+
template <>
|
|
1274
|
+
struct Codec<Uuid> : TextCodec<Uuid>
|
|
1275
|
+
{
|
|
1276
|
+
};
|
|
1277
|
+
|
|
1278
|
+
template <int Places>
|
|
1279
|
+
struct Codec<Decimal<Places>> : TextCodec<Decimal<Places>>
|
|
1280
|
+
{
|
|
1281
|
+
};
|
|
1282
|
+
|
|
1283
|
+
// The member's value, if those are strings or integers: else its name
|
|
1284
|
+
template <Enum T>
|
|
1285
|
+
struct Codec<T>
|
|
1286
|
+
{
|
|
1287
|
+
static constexpr unsigned kinds = []
|
|
1288
|
+
{
|
|
1289
|
+
unsigned found = 0;
|
|
1290
|
+
for (const auto& member : Reflect<T>::members)
|
|
1291
|
+
found |= member.quoted ? String : Int;
|
|
1292
|
+
return found;
|
|
1293
|
+
}();
|
|
1294
|
+
|
|
1295
|
+
static void write(Writer& writer, T value)
|
|
1296
|
+
{
|
|
1297
|
+
for (const auto& member : Reflect<T>::members)
|
|
1298
|
+
if (member.value == value)
|
|
1299
|
+
{
|
|
1300
|
+
if (member.quoted)
|
|
1301
|
+
writer.string(member.text);
|
|
1302
|
+
else
|
|
1303
|
+
writer.out += member.text;
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
throw Error(std::string(Reflect<T>::name) + " has no such member to write");
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
static T read(Reader& reader)
|
|
1310
|
+
{
|
|
1311
|
+
const unsigned kind = reader.kind();
|
|
1312
|
+
if (kind != String && kind != Int)
|
|
1313
|
+
throw Mismatch{};
|
|
1314
|
+
const std::string_view text = kind == String ? reader.string() : reader.number().first;
|
|
1315
|
+
for (const auto& member : Reflect<T>::members)
|
|
1316
|
+
if (member.quoted == (kind == String) && member.text == text)
|
|
1317
|
+
return member.value;
|
|
1318
|
+
throw Mismatch{};
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
|
|
1322
|
+
template <class T>
|
|
1323
|
+
struct Codec<std::optional<T>>
|
|
1324
|
+
{
|
|
1325
|
+
static constexpr unsigned kinds = Null | Codec<T>::kinds;
|
|
1326
|
+
|
|
1327
|
+
static void write(Writer& writer, const std::optional<T>& value)
|
|
1328
|
+
{
|
|
1329
|
+
if (value)
|
|
1330
|
+
Codec<T>::write(writer, *value);
|
|
1331
|
+
else
|
|
1332
|
+
writer.out += "null";
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
static std::optional<T> read(Reader& reader)
|
|
1336
|
+
{
|
|
1337
|
+
if (reader.kind() != Null)
|
|
1338
|
+
return Codec<T>::read(reader);
|
|
1339
|
+
reader.null();
|
|
1340
|
+
return std::nullopt;
|
|
1341
|
+
}
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1344
|
+
template <class T>
|
|
1345
|
+
struct Codec<std::vector<T>>
|
|
1346
|
+
{
|
|
1347
|
+
static constexpr unsigned kinds = List;
|
|
1348
|
+
|
|
1349
|
+
static void write(Writer& writer, const std::vector<T>& value)
|
|
1350
|
+
{
|
|
1351
|
+
writer.out += '[';
|
|
1352
|
+
bool first = true;
|
|
1353
|
+
for (const auto& element : value)
|
|
1354
|
+
{
|
|
1355
|
+
if (!std::exchange(first, false))
|
|
1356
|
+
writer.out += ',';
|
|
1357
|
+
Codec<T>::write(writer, element);
|
|
1358
|
+
}
|
|
1359
|
+
writer.out += ']';
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
static std::vector<T> read(Reader& reader)
|
|
1363
|
+
{
|
|
1364
|
+
if (reader.kind() != List)
|
|
1365
|
+
throw Mismatch{};
|
|
1366
|
+
const Reader::Nested nested(reader);
|
|
1367
|
+
++reader.at;
|
|
1368
|
+
std::vector<T> value;
|
|
1369
|
+
for (bool first = true; reader.more(']', first);)
|
|
1370
|
+
value.push_back(Codec<T>::read(reader));
|
|
1371
|
+
return value;
|
|
1372
|
+
}
|
|
1373
|
+
};
|
|
1374
|
+
|
|
1375
|
+
// An array of numbers: as the tuple of them is, after its shape if it is of more than one dimension
|
|
1376
|
+
template <class T, std::size_t N, bool W>
|
|
1377
|
+
struct Codec<dataclass_native::Array<T, N, W>>
|
|
1378
|
+
{
|
|
1379
|
+
static constexpr unsigned kinds = List;
|
|
1380
|
+
|
|
1381
|
+
static void write(Writer& writer, const dataclass_native::Array<T, N, W>& value)
|
|
1382
|
+
{
|
|
1383
|
+
if constexpr (N > 1)
|
|
1384
|
+
{
|
|
1385
|
+
writer.out += "[[";
|
|
1386
|
+
bool first = true;
|
|
1387
|
+
for (const std::size_t size : value.shape())
|
|
1388
|
+
{
|
|
1389
|
+
if (!std::exchange(first, false))
|
|
1390
|
+
writer.out += ',';
|
|
1391
|
+
writer.integer(size);
|
|
1392
|
+
}
|
|
1393
|
+
writer.out += "],";
|
|
1394
|
+
}
|
|
1395
|
+
const std::span<const T> numbers = *value;
|
|
1396
|
+
if constexpr (std::same_as<std::vector<T>, Bytes>)
|
|
1397
|
+
{
|
|
1398
|
+
writer.out += '"';
|
|
1399
|
+
Text<Bytes>::write(writer.out, numbers);
|
|
1400
|
+
writer.out += '"';
|
|
1401
|
+
}
|
|
1402
|
+
else
|
|
1403
|
+
{
|
|
1404
|
+
writer.out += '[';
|
|
1405
|
+
bool first = true;
|
|
1406
|
+
for (const T& number : numbers)
|
|
1407
|
+
{
|
|
1408
|
+
if (!std::exchange(first, false))
|
|
1409
|
+
writer.out += ',';
|
|
1410
|
+
Codec<T>::write(writer, number);
|
|
1411
|
+
}
|
|
1412
|
+
writer.out += ']';
|
|
1413
|
+
}
|
|
1414
|
+
if constexpr (N > 1)
|
|
1415
|
+
writer.out += ']';
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
static dataclass_native::Array<T, N, W> read(Reader& reader)
|
|
1419
|
+
{
|
|
1420
|
+
if constexpr (N == 1)
|
|
1421
|
+
return Codec<std::vector<T>>::read(reader);
|
|
1422
|
+
else
|
|
1423
|
+
{
|
|
1424
|
+
if (reader.kind() != List)
|
|
1425
|
+
throw Mismatch{};
|
|
1426
|
+
const Reader::Nested nested(reader);
|
|
1427
|
+
++reader.at;
|
|
1428
|
+
bool first = true;
|
|
1429
|
+
if (!reader.more(']', first))
|
|
1430
|
+
throw Mismatch{};
|
|
1431
|
+
const std::vector<std::size_t> sizes = Codec<std::vector<std::size_t>>::read(reader);
|
|
1432
|
+
if (sizes.size() != N || !reader.more(']', first))
|
|
1433
|
+
throw Mismatch{};
|
|
1434
|
+
std::vector<T> values = Codec<std::vector<T>>::read(reader);
|
|
1435
|
+
typename dataclass_native::Array<T, N, W>::Shape shape;
|
|
1436
|
+
std::copy(sizes.begin(), sizes.end(), shape.begin());
|
|
1437
|
+
if (reader.more(']', first) || !dataclass_native::Array<T, N, W>::fits(shape, values.size()))
|
|
1438
|
+
throw Mismatch{};
|
|
1439
|
+
return {std::move(values), shape};
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
};
|
|
1443
|
+
|
|
1444
|
+
// In ascending order, which is that it is held in
|
|
1445
|
+
template <class T>
|
|
1446
|
+
struct Codec<std::set<T>>
|
|
1447
|
+
{
|
|
1448
|
+
static constexpr unsigned kinds = List;
|
|
1449
|
+
|
|
1450
|
+
static void write(Writer& writer, const std::set<T>& value)
|
|
1451
|
+
{
|
|
1452
|
+
writer.out += '[';
|
|
1453
|
+
bool first = true;
|
|
1454
|
+
for (const auto& element : value)
|
|
1455
|
+
{
|
|
1456
|
+
if (!std::exchange(first, false))
|
|
1457
|
+
writer.out += ',';
|
|
1458
|
+
Codec<T>::write(writer, element);
|
|
1459
|
+
}
|
|
1460
|
+
writer.out += ']';
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
static std::set<T> read(Reader& reader)
|
|
1464
|
+
{
|
|
1465
|
+
if (reader.kind() != List)
|
|
1466
|
+
throw Mismatch{};
|
|
1467
|
+
const Reader::Nested nested(reader);
|
|
1468
|
+
++reader.at;
|
|
1469
|
+
std::set<T> value;
|
|
1470
|
+
for (bool first = true; reader.more(']', first);)
|
|
1471
|
+
if (!value.insert(Codec<T>::read(reader)).second)
|
|
1472
|
+
throw Mismatch{};
|
|
1473
|
+
return value;
|
|
1474
|
+
}
|
|
1475
|
+
};
|
|
1476
|
+
|
|
1477
|
+
// An object, in ascending order of key: each key the text of what it is written as
|
|
1478
|
+
template <class K, class V>
|
|
1479
|
+
struct Codec<std::map<K, V>>
|
|
1480
|
+
{
|
|
1481
|
+
static constexpr unsigned kinds = Object;
|
|
1482
|
+
|
|
1483
|
+
static void write(Writer& writer, const std::map<K, V>& value)
|
|
1484
|
+
{
|
|
1485
|
+
writer.out += '{';
|
|
1486
|
+
bool first = true;
|
|
1487
|
+
std::string key;
|
|
1488
|
+
for (const auto& [k, v] : value)
|
|
1489
|
+
{
|
|
1490
|
+
if (!std::exchange(first, false))
|
|
1491
|
+
writer.out += ',';
|
|
1492
|
+
key.clear();
|
|
1493
|
+
Text<K>::write(key, k);
|
|
1494
|
+
writer.string(key);
|
|
1495
|
+
writer.out += ':';
|
|
1496
|
+
Codec<V>::write(writer, v);
|
|
1497
|
+
}
|
|
1498
|
+
writer.out += '}';
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
static std::map<K, V> read(Reader& reader)
|
|
1502
|
+
{
|
|
1503
|
+
if (reader.kind() != Object)
|
|
1504
|
+
throw Mismatch{};
|
|
1505
|
+
const Reader::Nested nested(reader);
|
|
1506
|
+
++reader.at;
|
|
1507
|
+
std::map<K, V> value;
|
|
1508
|
+
for (bool first = true; reader.more('}', first);)
|
|
1509
|
+
{
|
|
1510
|
+
K key = Text<K>::read(reader.key());
|
|
1511
|
+
if (!value.emplace(std::move(key), Codec<V>::read(reader)).second)
|
|
1512
|
+
malformed("an object has a key twice");
|
|
1513
|
+
}
|
|
1514
|
+
return value;
|
|
1515
|
+
}
|
|
1516
|
+
};
|
|
1517
|
+
|
|
1518
|
+
template <class... Ts>
|
|
1519
|
+
struct Codec<std::tuple<Ts...>>
|
|
1520
|
+
{
|
|
1521
|
+
static constexpr unsigned kinds = List;
|
|
1522
|
+
|
|
1523
|
+
static void write(Writer& writer, const std::tuple<Ts...>& value)
|
|
1524
|
+
{
|
|
1525
|
+
writer.out += '[';
|
|
1526
|
+
bool first = true;
|
|
1527
|
+
std::apply(
|
|
1528
|
+
[&](const Ts&... elements)
|
|
1529
|
+
{
|
|
1530
|
+
((std::exchange(first, false) ? void() : void(writer.out += ','),
|
|
1531
|
+
Codec<Ts>::write(writer, elements)),
|
|
1532
|
+
...);
|
|
1533
|
+
},
|
|
1534
|
+
value);
|
|
1535
|
+
writer.out += ']';
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
static std::tuple<Ts...> read(Reader& reader)
|
|
1539
|
+
{
|
|
1540
|
+
if (reader.kind() != List)
|
|
1541
|
+
throw Mismatch{};
|
|
1542
|
+
const Reader::Nested nested(reader);
|
|
1543
|
+
++reader.at;
|
|
1544
|
+
bool first = true;
|
|
1545
|
+
// In braces, so in order
|
|
1546
|
+
std::tuple<Ts...> value{element<Ts>(reader, first)...};
|
|
1547
|
+
if (reader.more(']', first))
|
|
1548
|
+
throw Mismatch{};
|
|
1549
|
+
return value;
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
private:
|
|
1553
|
+
template <class T>
|
|
1554
|
+
static T element(Reader& reader, bool& first)
|
|
1555
|
+
{
|
|
1556
|
+
if (!reader.more(']', first))
|
|
1557
|
+
throw Mismatch{};
|
|
1558
|
+
return Codec<T>::read(reader);
|
|
1559
|
+
}
|
|
1560
|
+
};
|
|
1561
|
+
|
|
1562
|
+
// As its T
|
|
1563
|
+
template <class T>
|
|
1564
|
+
struct Codec<Indirect<T>>
|
|
1565
|
+
{
|
|
1566
|
+
static constexpr unsigned kinds = Codec<T>::kinds;
|
|
1567
|
+
|
|
1568
|
+
static void write(Writer& writer, const Indirect<T>& value)
|
|
1569
|
+
{
|
|
1570
|
+
if (!value)
|
|
1571
|
+
throw Error("An empty Indirect cannot be written");
|
|
1572
|
+
Codec<T>::write(writer, *value);
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
static Indirect<T> read(Reader& reader)
|
|
1576
|
+
{
|
|
1577
|
+
return Indirect<T>(Codec<T>::read(reader));
|
|
1578
|
+
}
|
|
1579
|
+
};
|
|
1580
|
+
|
|
1581
|
+
// As its T, wherever it is held: so what was one object held twice is read as two
|
|
1582
|
+
template <class T>
|
|
1583
|
+
struct Codec<Shared<T>>
|
|
1584
|
+
{
|
|
1585
|
+
static constexpr unsigned kinds = Object;
|
|
1586
|
+
|
|
1587
|
+
static void write(Writer& writer, const Shared<T>& value)
|
|
1588
|
+
{
|
|
1589
|
+
if (!value)
|
|
1590
|
+
throw Error("An empty Shared cannot be written");
|
|
1591
|
+
Codec<T>::write(writer, *value);
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
static Shared<T> read(Reader& reader)
|
|
1595
|
+
{
|
|
1596
|
+
return Shared<T>(Codec<T>::read(reader));
|
|
1597
|
+
}
|
|
1598
|
+
};
|
|
1599
|
+
|
|
1600
|
+
template <class T>
|
|
1601
|
+
struct Codec<Mutable<T>>
|
|
1602
|
+
{
|
|
1603
|
+
static constexpr unsigned kinds = Object;
|
|
1604
|
+
|
|
1605
|
+
static void write(Writer& writer, const Mutable<T>& value)
|
|
1606
|
+
{
|
|
1607
|
+
if (!value)
|
|
1608
|
+
throw Error("An empty Mutable cannot be written");
|
|
1609
|
+
// Which only an object that is not frozen may do
|
|
1610
|
+
if (++writer.depth > max_depth)
|
|
1611
|
+
throw Error("An object which holds itself, or is too deeply nested, cannot be written");
|
|
1612
|
+
Codec<T>::write(writer, *value);
|
|
1613
|
+
--writer.depth;
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
static Mutable<T> read(Reader& reader)
|
|
1617
|
+
{
|
|
1618
|
+
return Mutable<T>(Codec<T>::read(reader));
|
|
1619
|
+
}
|
|
1620
|
+
};
|
|
1621
|
+
|
|
1622
|
+
// As an object of each field's name and value
|
|
1623
|
+
template <Struct T>
|
|
1624
|
+
struct Codec<T>
|
|
1625
|
+
{
|
|
1626
|
+
static constexpr unsigned kinds = Object;
|
|
1627
|
+
|
|
1628
|
+
static void write(Writer& writer, const T& value)
|
|
1629
|
+
{
|
|
1630
|
+
writer.out += '{';
|
|
1631
|
+
bool first = true;
|
|
1632
|
+
std::apply([&](const auto&... field) { (write_field(writer, value, field, first), ...); },
|
|
1633
|
+
Reflect<T>::fields);
|
|
1634
|
+
writer.out += '}';
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
static T read(Reader& reader)
|
|
1638
|
+
{
|
|
1639
|
+
if (reader.kind() != Object)
|
|
1640
|
+
throw Mismatch{};
|
|
1641
|
+
const Reader::Nested nested(reader);
|
|
1642
|
+
++reader.at;
|
|
1643
|
+
T value{};
|
|
1644
|
+
std::array<bool, std::tuple_size_v<decltype(Reflect<T>::fields)>> written{};
|
|
1645
|
+
for (bool first = true; reader.more('}', first);)
|
|
1646
|
+
{
|
|
1647
|
+
// An entry which is not a field's is ignored
|
|
1648
|
+
const std::string_view name = reader.key();
|
|
1649
|
+
std::size_t index = 0;
|
|
1650
|
+
const bool is_field = std::apply(
|
|
1651
|
+
[&](const auto&... field)
|
|
1652
|
+
{
|
|
1653
|
+
return ((field.name == name ? (read_field(reader, value, field, written[index]), true)
|
|
1654
|
+
: (++index, false)) ||
|
|
1655
|
+
...);
|
|
1656
|
+
},
|
|
1657
|
+
Reflect<T>::fields);
|
|
1658
|
+
if (!is_field)
|
|
1659
|
+
reader.skip();
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
// A field which is not written takes its default, if it has one
|
|
1663
|
+
std::size_t index = 0;
|
|
1664
|
+
std::apply(
|
|
1665
|
+
[&](const auto&... field)
|
|
1666
|
+
{
|
|
1667
|
+
((written[index++] || field.has_default
|
|
1668
|
+
? void()
|
|
1669
|
+
: throw Error(std::string(Reflect<T>::name) + " cannot be read: no " +
|
|
1670
|
+
std::string(field.name))),
|
|
1671
|
+
...);
|
|
1672
|
+
},
|
|
1673
|
+
Reflect<T>::fields);
|
|
1674
|
+
return value;
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
private:
|
|
1678
|
+
static std::string named(std::string_view field)
|
|
1679
|
+
{
|
|
1680
|
+
return std::string(Reflect<T>::name) + "." + std::string(field);
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
template <class M>
|
|
1684
|
+
static void write_field(Writer& writer, const T& value, const Field<T, M>& field, bool& first)
|
|
1685
|
+
{
|
|
1686
|
+
if (!std::exchange(first, false))
|
|
1687
|
+
writer.out += ',';
|
|
1688
|
+
writer.out += '"';
|
|
1689
|
+
writer.out += field.name;
|
|
1690
|
+
writer.out += "\":";
|
|
1691
|
+
Codec<M>::write(writer, value.*field.member);
|
|
1692
|
+
}
|
|
1693
|
+
|
|
1694
|
+
template <class M>
|
|
1695
|
+
static void read_field(Reader& reader, T& value, const Field<T, M>& field, bool& written)
|
|
1696
|
+
{
|
|
1697
|
+
if (std::exchange(written, true))
|
|
1698
|
+
malformed("an object has a key twice");
|
|
1699
|
+
try
|
|
1700
|
+
{
|
|
1701
|
+
value.*field.member = Codec<M>::read(reader);
|
|
1702
|
+
}
|
|
1703
|
+
catch (const Mismatch&)
|
|
1704
|
+
{
|
|
1705
|
+
throw Error(named(field.name) + " cannot be read: it is not of its type, as written");
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
};
|
|
1709
|
+
|
|
1710
|
+
// Reads the first member of a union for which matches is true, and which is what is next, into value
|
|
1711
|
+
template <class Matches, class... Ts>
|
|
1712
|
+
void read_member(Reader& reader, std::variant<Ts...>& value, const Matches& matches)
|
|
1713
|
+
{
|
|
1714
|
+
const char* start = reader.at;
|
|
1715
|
+
const auto member = [&]<class M>(std::type_identity<M>)
|
|
1716
|
+
{
|
|
1717
|
+
if (!matches(std::type_identity<M>{}))
|
|
1718
|
+
return false;
|
|
1719
|
+
try
|
|
1720
|
+
{
|
|
1721
|
+
value.template emplace<M>(Codec<M>::read(reader));
|
|
1722
|
+
return true;
|
|
1723
|
+
}
|
|
1724
|
+
catch (const Mismatch&)
|
|
1725
|
+
{
|
|
1726
|
+
// Another may be: of strings, each has a form of its own
|
|
1727
|
+
reader.at = start;
|
|
1728
|
+
return false;
|
|
1729
|
+
}
|
|
1730
|
+
};
|
|
1731
|
+
if (!(member(std::type_identity<Ts>{}) || ...))
|
|
1732
|
+
throw Mismatch{};
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
// A plain union: as its member, each being written as a kind of JSON value of its own
|
|
1736
|
+
template <class... Ts>
|
|
1737
|
+
struct Codec<std::variant<Ts...>>
|
|
1738
|
+
{
|
|
1739
|
+
static constexpr unsigned kinds = (Codec<Ts>::kinds | ...);
|
|
1740
|
+
|
|
1741
|
+
static void write(Writer& writer, const std::variant<Ts...>& value)
|
|
1742
|
+
{
|
|
1743
|
+
static_assert(told_apart(), "The members of a union cannot be told apart in JSON, so it cannot be "
|
|
1744
|
+
"written as it: use DiscriminatedUnion");
|
|
1745
|
+
if (value.valueless_by_exception())
|
|
1746
|
+
throw Error("A variant without a value cannot be written");
|
|
1747
|
+
std::visit(
|
|
1748
|
+
[&]<class M>(const M& member)
|
|
1749
|
+
{
|
|
1750
|
+
if constexpr (std::floating_point<M>)
|
|
1751
|
+
if ((kinds & String) && !std::isfinite(member))
|
|
1752
|
+
// Which would be read as the string it is written as
|
|
1753
|
+
throw Error("A float which is not finite cannot be written in a union with a str");
|
|
1754
|
+
Codec<M>::write(writer, member);
|
|
1755
|
+
},
|
|
1756
|
+
value);
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
static std::variant<Ts...> read(Reader& reader)
|
|
1760
|
+
{
|
|
1761
|
+
static_assert(told_apart(), "The members of a union cannot be told apart in JSON, so it cannot be "
|
|
1762
|
+
"read from it: use DiscriminatedUnion");
|
|
1763
|
+
const unsigned kind = reader.kind();
|
|
1764
|
+
// A string is of the member whose form it has: found without reading it as each
|
|
1765
|
+
std::string text;
|
|
1766
|
+
if (kind == String)
|
|
1767
|
+
{
|
|
1768
|
+
const char* start = reader.at;
|
|
1769
|
+
text = reader.string();
|
|
1770
|
+
reader.at = start;
|
|
1771
|
+
}
|
|
1772
|
+
std::variant<Ts...> value;
|
|
1773
|
+
read_member(reader, value,
|
|
1774
|
+
[&]<class M>(std::type_identity<M>)
|
|
1775
|
+
{
|
|
1776
|
+
const unsigned member = Codec<M>::kinds;
|
|
1777
|
+
switch (kind)
|
|
1778
|
+
{
|
|
1779
|
+
case Int:
|
|
1780
|
+
// A float which is whole may be written as an integer, by another
|
|
1781
|
+
return (member & Int) || (!(kinds & Int) && (member & Float));
|
|
1782
|
+
case String:
|
|
1783
|
+
if constexpr (requires { Text<M>::formed(text); })
|
|
1784
|
+
return Text<M>::formed(text);
|
|
1785
|
+
else
|
|
1786
|
+
// Of any: or a float which is not finite, if no member is of any
|
|
1787
|
+
return (member & String) || (!(kinds & String) && (member & Float));
|
|
1788
|
+
default:
|
|
1789
|
+
return (member & kind) != 0;
|
|
1790
|
+
}
|
|
1791
|
+
});
|
|
1792
|
+
return value;
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
private:
|
|
1796
|
+
static constexpr bool told_apart()
|
|
1797
|
+
{
|
|
1798
|
+
constexpr unsigned each[] = {Codec<Ts>::kinds...};
|
|
1799
|
+
for (std::size_t i = 0; i != sizeof...(Ts); ++i)
|
|
1800
|
+
for (std::size_t j = i + 1; j != sizeof...(Ts); ++j)
|
|
1801
|
+
if (collide(each[i], each[j]))
|
|
1802
|
+
return false;
|
|
1803
|
+
return true;
|
|
1804
|
+
}
|
|
1805
|
+
};
|
|
1806
|
+
|
|
1807
|
+
// As an object of "t", its member's tag, and "v", its member
|
|
1808
|
+
template <class... Ts>
|
|
1809
|
+
struct Codec<DiscriminatedUnion<Ts...>>
|
|
1810
|
+
{
|
|
1811
|
+
static constexpr unsigned kinds = Object;
|
|
1812
|
+
|
|
1813
|
+
static void write(Writer& writer, const DiscriminatedUnion<Ts...>& value)
|
|
1814
|
+
{
|
|
1815
|
+
if (value.valueless_by_exception())
|
|
1816
|
+
throw Error("A variant without a value cannot be written");
|
|
1817
|
+
std::visit(
|
|
1818
|
+
[&]<class M>(const M& member)
|
|
1819
|
+
{
|
|
1820
|
+
writer.out += "{\"t\":";
|
|
1821
|
+
writer.string(msgpack::detail::tag_of<M>());
|
|
1822
|
+
writer.out += ",\"v\":";
|
|
1823
|
+
Codec<M>::write(writer, member);
|
|
1824
|
+
writer.out += '}';
|
|
1825
|
+
},
|
|
1826
|
+
value.as_variant());
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
static DiscriminatedUnion<Ts...> read(Reader& reader)
|
|
1830
|
+
{
|
|
1831
|
+
if (reader.kind() != Object)
|
|
1832
|
+
throw Mismatch{};
|
|
1833
|
+
const Reader::Nested nested(reader);
|
|
1834
|
+
++reader.at;
|
|
1835
|
+
|
|
1836
|
+
// In either order: so the member may be before the tag which says what it is
|
|
1837
|
+
std::optional<std::string> tag;
|
|
1838
|
+
const char* member = nullptr;
|
|
1839
|
+
for (bool first = true; reader.more('}', first);)
|
|
1840
|
+
{
|
|
1841
|
+
const std::string_view key = reader.key();
|
|
1842
|
+
const bool is_tag = key == "t";
|
|
1843
|
+
const bool is_member = key == "v";
|
|
1844
|
+
if (is_tag && !tag && reader.kind() == String)
|
|
1845
|
+
tag = std::string(reader.string());
|
|
1846
|
+
else if (is_member && !member)
|
|
1847
|
+
{
|
|
1848
|
+
member = reader.at;
|
|
1849
|
+
reader.skip();
|
|
1850
|
+
}
|
|
1851
|
+
else
|
|
1852
|
+
throw Mismatch{};
|
|
1853
|
+
}
|
|
1854
|
+
if (!tag || !member)
|
|
1855
|
+
throw Mismatch{};
|
|
1856
|
+
|
|
1857
|
+
const char* rest = std::exchange(reader.at, member);
|
|
1858
|
+
std::variant<Ts...> value;
|
|
1859
|
+
read_member(reader, value,
|
|
1860
|
+
[&]<class M>(std::type_identity<M>) { return msgpack::detail::tag_of<M>() == *tag; });
|
|
1861
|
+
reader.at = rest;
|
|
1862
|
+
return DiscriminatedUnion<Ts...>(std::move(value));
|
|
1863
|
+
}
|
|
1864
|
+
};
|
|
1865
|
+
|
|
1866
|
+
template <class T>
|
|
1867
|
+
std::string described()
|
|
1868
|
+
{
|
|
1869
|
+
if constexpr (Struct<T>)
|
|
1870
|
+
return "a " + std::string(Reflect<T>::name);
|
|
1871
|
+
else
|
|
1872
|
+
return "of the type read";
|
|
1873
|
+
}
|
|
1874
|
+
} // namespace detail
|
|
1875
|
+
|
|
1876
|
+
// As UTF-8. Throws Error if the value cannot be written
|
|
1877
|
+
template <class T>
|
|
1878
|
+
std::string to_json(const T& value)
|
|
1879
|
+
{
|
|
1880
|
+
detail::Writer writer;
|
|
1881
|
+
detail::Codec<T>::write(writer, value);
|
|
1882
|
+
return std::move(writer.out);
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
// Throws Error if data is not JSON, or not a T as written
|
|
1886
|
+
template <class T>
|
|
1887
|
+
T from_json(std::string_view data)
|
|
1888
|
+
{
|
|
1889
|
+
if (!msgpack::detail::is_utf8(data))
|
|
1890
|
+
detail::malformed("it is not UTF-8");
|
|
1891
|
+
detail::Reader reader{data.data(), data.data() + data.size()};
|
|
1892
|
+
try
|
|
1893
|
+
{
|
|
1894
|
+
T value = detail::Codec<T>::read(reader);
|
|
1895
|
+
reader.finish();
|
|
1896
|
+
return value;
|
|
1897
|
+
}
|
|
1898
|
+
catch (const detail::Mismatch&)
|
|
1899
|
+
{
|
|
1900
|
+
throw Error("The data is not " + detail::described<T>() + ", as written");
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
} // namespace dataclass_native::json
|