-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathfrom_json.hpp
More file actions
771 lines (703 loc) · 26.8 KB
/
Copy pathfrom_json.hpp
File metadata and controls
771 lines (703 loc) · 26.8 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#pragma once
#include <cstdlib>
#include <eosio/eosio_outcome.hpp>
#include <eosio/for_each_field.hpp>
#include <functional>
#include <optional>
#include <rapidjson/reader.h>
#include <vector>
#include <variant>
namespace eosio {
enum class from_json_error {
no_error,
expected_end,
expected_null,
expected_bool,
expected_string,
expected_hex_string,
hex_string_incorrect_length,
invalid_signature,
invalid_name,
expected_start_object,
expected_key,
expected_end_object,
expected_start_array,
expected_end_array,
expected_positive_uint,
expected_field,
expected_variant,
expected_public_key,
expected_private_key,
expected_signature,
expected_number,
expected_int,
expected_time_point,
expected_symbol_code,
expected_symbol,
expected_asset,
invalid_type_for_variant,
unexpected_field,
number_out_of_range,
from_json_no_pair,
// These are from rapidjson:
document_empty,
document_root_not_singular,
value_invalid,
object_miss_name,
object_miss_colon,
object_miss_comma_or_curly_bracket,
array_miss_comma_or_square_bracket,
string_unicode_escape_invalid_hex,
string_unicode_surrogate_invalid,
string_escape_invalid,
string_miss_quotation_mark,
string_invalid_encoding,
number_too_big,
number_miss_fraction,
number_miss_exponent,
terminated,
unspecific_syntax_error,
}; // from_json_error
} // namespace eosio
namespace std {
template <>
struct is_error_code_enum<eosio::from_json_error> : true_type {};
} // namespace std
namespace eosio {
class from_json_error_category_type : public std::error_category {
public:
const char* name() const noexcept override final { return "ConversionError"; }
std::string message(int c) const override final {
switch (static_cast<from_json_error>(c)) {
// clang-format off
case from_json_error::no_error: return "No error";
case from_json_error::expected_end: return "Expected end of json";
case from_json_error::expected_null: return "Expected null";
case from_json_error::expected_bool: return "Expected true or false";
case from_json_error::expected_string: return "Expected string";
case from_json_error::expected_hex_string: return "Expected string containing hex";
case from_json_error::hex_string_incorrect_length: return "Hex string has incorrect length";
case from_json_error::invalid_signature: return "Invalid signature format";
case from_json_error::invalid_name: return "Invalid name";
case from_json_error::expected_start_object: return "Expected {";
case from_json_error::expected_key: return "Expected key";
case from_json_error::expected_end_object: return "Expected }";
case from_json_error::expected_start_array: return "Expected [";
case from_json_error::expected_end_array: return "Expected ]";
case from_json_error::expected_positive_uint: return "Expected positive integer";
case from_json_error::expected_field: return "Expected field";
case from_json_error::expected_variant: return R"(Expected variant: ["type", value])";
case from_json_error::expected_public_key: return "Expected public key";
case from_json_error::expected_private_key: return "Expected private key";
case from_json_error::expected_signature: return "Expected signature";
case from_json_error::expected_number: return "Expected number or boolean";
case from_json_error::expected_int: return "Expected integer";
case from_json_error::expected_time_point: return "Expected time point";
case from_json_error::expected_symbol_code: return "Expected symbol code";
case from_json_error::expected_symbol: return "Expected symbol";
case from_json_error::expected_asset: return "Expected asset";
case from_json_error::invalid_type_for_variant: return "Invalid type for variant";
case from_json_error::unexpected_field: return "Unexpected field";
case from_json_error::number_out_of_range: return "number is out of range";
case from_json_error::from_json_no_pair: return "from_json does not support std::pair";
case from_json_error::document_empty: return "The document is empty";
case from_json_error::document_root_not_singular: return "The document root must not follow by other values";
case from_json_error::value_invalid: return "Invalid value";
case from_json_error::object_miss_name: return "Missing a name for object member";
case from_json_error::object_miss_colon: return "Missing a colon after a name of object member";
case from_json_error::object_miss_comma_or_curly_bracket: return "Missing a comma or '}' after an object member";
case from_json_error::array_miss_comma_or_square_bracket: return "Missing a comma or ']' after an array element";
case from_json_error::string_unicode_escape_invalid_hex: return "Incorrect hex digit after \\u escape in string";
case from_json_error::string_unicode_surrogate_invalid: return "The surrogate pair in string is invalid";
case from_json_error::string_escape_invalid: return "Invalid escape character in string";
case from_json_error::string_miss_quotation_mark: return "Missing a closing quotation mark in string";
case from_json_error::string_invalid_encoding: return "Invalid encoding in string";
case from_json_error::number_too_big: return "Number too big to be stored in double";
case from_json_error::number_miss_fraction: return "Miss fraction part in number";
case from_json_error::number_miss_exponent: return "Miss exponent in number";
case from_json_error::terminated: return "Parsing was terminated";
case from_json_error::unspecific_syntax_error: return "Unspecific syntax error";
// clang-format on
default: return "unknown";
}
}
}; // from_json_error_category_type
inline const from_json_error_category_type& from_json_error_category() {
static from_json_error_category_type c;
return c;
}
inline std::error_code make_error_code(from_json_error e) {
return { static_cast<int>(e), from_json_error_category() };
}
inline from_json_error convert_error(rapidjson::ParseErrorCode err) {
switch (err) {
// clang-format off
case rapidjson::kParseErrorNone: return from_json_error::no_error;
case rapidjson::kParseErrorDocumentEmpty: return from_json_error::document_empty;
case rapidjson::kParseErrorDocumentRootNotSingular: return from_json_error::document_root_not_singular;
case rapidjson::kParseErrorValueInvalid: return from_json_error::value_invalid;
case rapidjson::kParseErrorObjectMissName: return from_json_error::object_miss_name;
case rapidjson::kParseErrorObjectMissColon: return from_json_error::object_miss_colon;
case rapidjson::kParseErrorObjectMissCommaOrCurlyBracket: return from_json_error::object_miss_comma_or_curly_bracket;
case rapidjson::kParseErrorArrayMissCommaOrSquareBracket: return from_json_error::array_miss_comma_or_square_bracket;
case rapidjson::kParseErrorStringUnicodeEscapeInvalidHex: return from_json_error::string_unicode_escape_invalid_hex;
case rapidjson::kParseErrorStringUnicodeSurrogateInvalid: return from_json_error::string_unicode_surrogate_invalid;
case rapidjson::kParseErrorStringEscapeInvalid: return from_json_error::string_escape_invalid;
case rapidjson::kParseErrorStringMissQuotationMark: return from_json_error::string_miss_quotation_mark;
case rapidjson::kParseErrorStringInvalidEncoding: return from_json_error::string_invalid_encoding;
case rapidjson::kParseErrorNumberTooBig: return from_json_error::number_too_big;
case rapidjson::kParseErrorNumberMissFraction: return from_json_error::number_miss_fraction;
case rapidjson::kParseErrorNumberMissExponent: return from_json_error::number_miss_exponent;
case rapidjson::kParseErrorTermination: return from_json_error::terminated;
case rapidjson::kParseErrorUnspecificSyntaxError: return from_json_error::unspecific_syntax_error;
// clang-format on
default: return from_json_error::unspecific_syntax_error;
}
}
enum class json_token_type {
type_unread,
type_null,
type_bool,
type_string,
type_start_object,
type_key,
type_end_object,
type_start_array,
type_end_array,
};
struct json_token {
json_token_type type = {};
std::string_view key = {};
bool value_bool = {};
std::string_view value_string = {};
};
class json_token_stream : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, json_token_stream> {
private:
rapidjson::Reader reader;
rapidjson::InsituStringStream ss;
public:
json_token current_token;
// This modifies json
json_token_stream(char* json) : ss{ json } { reader.IterativeParseInit(); }
bool complete() { return reader.IterativeParseComplete(); }
result<std::reference_wrapper<const json_token>> peek_token() {
if (current_token.type != json_token_type::type_unread)
return current_token;
else if (reader.IterativeParseNext<rapidjson::kParseInsituFlag | rapidjson::kParseValidateEncodingFlag |
rapidjson::kParseIterativeFlag | rapidjson::kParseNumbersAsStringsFlag>(ss,
*this))
return current_token;
else
return convert_error(reader.GetParseErrorCode());
}
void eat_token() { current_token.type = json_token_type::type_unread; }
result<void> get_end() {
if (current_token.type != json_token_type::type_unread || !complete())
return from_json_error::expected_end;
return outcome::success();
}
result<void> get_null() {
auto t = peek_token();
if (!t)
return t.error();
if (t.value().get().type != json_token_type::type_null)
return from_json_error::expected_null;
eat_token();
return outcome::success();
}
result<bool> get_bool() {
auto t = peek_token();
if (!t)
return t.error();
if (t.value().get().type != json_token_type::type_bool)
return from_json_error::expected_bool;
eat_token();
return t.value().get().value_bool;
}
result<std::string_view> get_string() {
auto t = peek_token();
if (!t)
return t.error();
if (t.value().get().type != json_token_type::type_string)
return from_json_error::expected_string;
eat_token();
return t.value().get().value_string;
}
result<void> get_start_object() {
auto t = peek_token();
if (!t)
return t.error();
if (t.value().get().type != json_token_type::type_start_object)
return from_json_error::expected_start_object;
eat_token();
return outcome::success();
}
result<std::string_view> get_key() {
auto t = peek_token();
if (!t)
return t.error();
if (t.value().get().type != json_token_type::type_key)
return from_json_error::expected_key;
eat_token();
return t.value().get().key;
}
result<void> get_end_object() {
auto t = peek_token();
if (!t)
return t.error();
if (t.value().get().type != json_token_type::type_end_object)
return from_json_error::expected_end_object;
eat_token();
return outcome::success();
}
result<void> get_start_array() {
auto t = peek_token();
if (!t)
return t.error();
if (t.value().get().type != json_token_type::type_start_array)
return from_json_error::expected_start_array;
eat_token();
return outcome::success();
}
result<void> get_end_array() {
auto t = peek_token();
if (!t)
return t.error();
if (t.value().get().type != json_token_type::type_end_array)
return from_json_error::expected_end_array;
eat_token();
return outcome::success();
}
// BaseReaderHandler methods
bool Null() {
current_token.type = json_token_type::type_null;
return true;
}
bool Bool(bool v) {
current_token.type = json_token_type::type_bool;
current_token.value_bool = v;
return true;
}
bool RawNumber(const char* v, rapidjson::SizeType length, bool copy) { return String(v, length, copy); }
bool Int(int v) { return false; }
bool Uint(unsigned v) { return false; }
bool Int64(int64_t v) { return false; }
bool Uint64(uint64_t v) { return false; }
bool Double(double v) { return false; }
bool String(const char* v, rapidjson::SizeType length, bool) {
current_token.type = json_token_type::type_string;
current_token.value_string = { v, length };
return true;
}
bool StartObject() {
current_token.type = json_token_type::type_start_object;
return true;
}
bool Key(const char* v, rapidjson::SizeType length, bool) {
current_token.key = { v, length };
current_token.type = json_token_type::type_key;
return true;
}
bool EndObject(rapidjson::SizeType) {
current_token.type = json_token_type::type_end_object;
return true;
}
bool StartArray() {
current_token.type = json_token_type::type_start_array;
return true;
}
bool EndArray(rapidjson::SizeType) {
current_token.type = json_token_type::type_end_array;
return true;
}
}; // json_token_stream
template <typename SrcIt, typename DestIt>
[[nodiscard]] bool unhex(DestIt dest, SrcIt begin, SrcIt end) {
auto get_digit = [&](uint8_t& nibble) {
if (*begin >= '0' && *begin <= '9')
nibble = *begin++ - '0';
else if (*begin >= 'a' && *begin <= 'f')
nibble = *begin++ - 'a' + 10;
else if (*begin >= 'A' && *begin <= 'F')
nibble = *begin++ - 'A' + 10;
else
return false;
return true;
};
while (begin != end) {
uint8_t h, l;
if (!get_digit(h) || !get_digit(l))
return false;
*dest++ = (h << 4) | l;
}
return true;
}
/// \exclude
template <typename T, typename S>
result<void> from_json(T& result, S& stream);
/// \group from_json_explicit Parse JSON (Explicit Types)
/// Parse JSON and convert to `result`. These overloads handle specified types.
template <typename S>
result<void> from_json(std::string_view& result, S& stream) {
auto r = stream.get_string();
if (!r)
return r.error();
result = r.value();
return outcome::success();
}
/// \group from_json_explicit Parse JSON (Explicit Types)
/// Parse JSON and convert to `result`. These overloads handle specified types.
template <typename S>
result<void> from_json(std::string& result, S& stream) {
auto r = stream.get_string();
if (!r)
return r.error();
result = r.value();
return outcome::success();
}
/// \exclude
template <typename T, typename S>
result<void> from_json_int(T& result, S& stream) {
OUTCOME_TRY(r, stream.get_string());
auto pos = r.data();
auto end = pos + r.size();
bool found = false;
result = 0;
T limit;
T sign;
if (std::is_signed_v<T> && pos != end && *pos == '-') {
++pos;
sign = -1;
limit = std::numeric_limits<T>::min();
} else {
sign = 1;
limit = std::numeric_limits<T>::max();
}
while (pos != end && *pos >= '0' && *pos <= '9') {
T digit = (*pos++ - '0');
// abs(result) can overflow. Use -abs(result) instead.
if (std::is_signed_v<T> && (-sign * limit + digit) / 10 > -sign * result)
return from_json_error::number_out_of_range;
if (!std::is_signed_v<T> && (limit - digit) / 10 < result)
return from_json_error::number_out_of_range;
result = result * 10 + sign * digit;
found = true;
}
if (pos != end || !found)
return from_json_error::expected_int;
return outcome::success();
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(uint8_t& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(uint16_t& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(uint32_t& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(uint64_t& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(unsigned __int128& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(int8_t& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(int16_t& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(int32_t& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(int64_t& result, S& stream) {
return from_json_int(result, stream);
}
/// \group from_json_explicit
template <typename S>
result<void> from_json(__int128& result, S& stream) {
return from_json_int(result, stream);
}
template <typename S>
result<void> from_json(float& result, S& stream) {
OUTCOME_TRY(sv, stream.get_string());
if (sv.empty())
return from_json_error::expected_number;
std::string s(sv); // strtof expects a null-terminated string
errno = 0;
char* end;
result = std::strtof(s.c_str(), &end);
if (errno || end != s.c_str() + s.size())
return from_json_error::expected_number;
return outcome::success();
}
template <typename S>
result<void> from_json(double& result, S& stream) {
OUTCOME_TRY(sv, stream.get_string());
if (sv.empty())
return from_json_error::expected_number;
std::string s(sv);
errno = 0;
char* end;
result = std::strtod(s.c_str(), &end);
if (errno || end != s.c_str() + s.size())
return from_json_error::expected_number;
return outcome::success();
}
/*
/// \group from_json_explicit
template<typename S>
result<void> from_json(int32_t& result, S& stream) {
bool in_str = false;
if (pos != end && *pos == '"') {
in_str = true;
from_json_skip_space(pos, end);
}
bool neg = false;
if (pos != end && *pos == '-') {
neg = true;
++pos;
}
bool found = false;
result = 0;
while (pos != end && *pos >= '0' && *pos <= '9') {
result = result * 10 + *pos++ - '0';
found = true;
}
check(found, "expected integer");
from_json_skip_space(pos, end);
if (in_str) {
from_json_expect(pos, end, '"', "expected integer");
from_json_skip_space(pos, end);
}
if (neg)
result = -result;
}
*/
/// \group from_json_explicit
template <typename S>
result<void> from_json(bool& result, S& stream) {
auto r = stream.get_bool();
if (!r)
return r.error();
result = r.value();
return outcome::success();
}
/// \group from_json_explicit
template <typename T, typename S>
result<void> from_json(std::vector<T>& result, S& stream) {
auto r = stream.get_start_array();
if (!r)
return r.error();
while (true) {
auto t = stream.peek_token();
if (!t)
return t.error();
if (t.value().get().type == json_token_type::type_end_array)
break;
result.emplace_back();
r = from_json(result.back(), stream);
if (!r)
return r;
}
return stream.get_end_array();
}
/// \group from_json_explicit
template <typename T, typename S>
result<void> from_json(std::optional<T>& result, S& stream) {
if (stream.get_null()) {
result = std::nullopt;
return eosio::outcome::success();
} else {
result.emplace();
return from_json(*result, stream);
}
}
template <int N = 0, typename... T>
void set_variant_impl(std::variant<T...>& result, uint32_t type) {
if (type == N) {
result.template emplace<N>();
} else if constexpr (N + 1 < sizeof...(T)) {
set_variant_impl<N + 1>(result, type);
}
}
/// \group from_json_explicit
template <typename... T, typename S>
result<void> from_json(std::variant<T...>& result, S& stream) {
OUTCOME_TRY(stream.get_start_array());
std::string_view type;
OUTCOME_TRY(from_json(type, stream));
const char* const type_names[] = { get_type_name((T*)nullptr)... };
uint32_t type_idx = std::find(type_names, type_names + sizeof...(T), type) - type_names;
if (type_idx >= sizeof...(T))
return from_json_error::invalid_type_for_variant;
set_variant_impl(result, type_idx);
OUTCOME_TRY(std::visit([&](auto& x) { return from_json(x, stream); }, result));
return stream.get_end_array();
}
/// \group from_json_explicit
template <typename S>
result<void> from_json_hex(std::vector<char>& result, S& stream) {
OUTCOME_TRY(s, stream.get_string());
if (s.size() & 1)
return from_json_error::expected_hex_string;
result.clear();
result.reserve(s.size() / 2);
if (!unhex(std::back_inserter(result), s.begin(), s.end()))
return from_json_error::expected_hex_string;
return outcome::success();
}
/// \exclude
template <typename S, typename F>
inline result<void> from_json_object(S& stream, F f) {
auto r = stream.get_start_object();
if (!r)
return r.error();
while (true) {
auto t = stream.peek_token();
if (!t)
return t.error();
if (t.value().get().type == json_token_type::type_end_object)
break;
auto k = stream.get_key();
if (!k)
return k.error();
r = f(k.value());
if (!r)
return r;
}
return stream.get_end_object();
}
template <typename S>
result<void> from_json_skip_value(S& stream) {
uint64_t depth = 0;
do {
auto t = stream.peek_token();
if (!t)
return t.error();
auto type = t.value().get().type;
if (type == json_token_type::type_start_object || type == json_token_type::type_start_array)
++depth;
else if (type == json_token_type::type_end_object || type == json_token_type::type_end_array)
--depth;
stream.eat_token();
} while (depth);
return outcome::success();
}
/// \output_section Parse JSON (Reflected Objects)
/// Parse JSON and convert to `obj`. This overload works with
/// [reflected objects](standardese://reflection/).
template <typename T, typename S>
result<void> from_json(T& obj, S& stream) {
return from_json_object(stream, [&](std::string_view key) -> result<void> {
bool found = false;
result<void> r = outcome::success();
for_each_field<T>([&](std::string_view member_name, auto member) {
if (!found && key == member_name) {
r = from_json(member(&obj), stream);
found = true;
}
});
if (!r)
return r;
if (!found)
return from_json_skip_value(stream);
return outcome::success();
});
}
template <typename First, typename Second, typename S>
result<void> from_json(std::pair<First, Second>& obj, S& stream) {
return from_json_error::from_json_no_pair;
}
/*
/// \output_section Convenience Wrappers
/// Parse JSON and return result. This overload wraps the other `to_json` overloads.
template <typename T>
T from_json(const std::vector<char>& v) {
const char* pos = v.data();
const char* end = pos + v.size();
from_json_skip_space(pos, end);
T result;
from_json(result, pos, end);
from_json_expect_end(pos, end);
return result;
}
/// Parse JSON and return result. This overload wraps the other `to_json` overloads.
template <typename T>
T from_json(std::string_view s) {
const char* pos = s.data();
const char* end = pos + s.size();
from_json_skip_space(pos, end);
T result;
from_json(result, pos, end);
from_json_expect_end(pos, end);
return result;
}
*/
/// Parse JSON and return result. This overload wraps the other `to_json` overloads.
template <typename T, typename S>
result<T> from_json(S& stream) {
T x;
auto r = from_json(x, stream);
if (!r)
return r.error();
return x;
}
/*
/// \exclude
template <size_t I, tagged_variant_options Options, typename... NamedTypes>
__attribute__((noinline)) void parse_named_variant_impl(tagged_variant<Options, NamedTypes...>& v, size_t i,
const char*& pos, const char* end) {
if constexpr (I < sizeof...(NamedTypes)) {
if (i == I) {
auto& q = v.value;
auto& x = q.template emplace<I>();
if constexpr (!is_named_empty_type_v<std::decay_t<decltype(x)>>) {
from_json_expect(pos, end, ',', "expected ,");
from_json(x, pos, end);
}
} else {
return parse_named_variant_impl<I + 1>(v, i, pos, end);
}
} else {
check(false, "invalid variant index");
}
}
/// \group from_json_explicit
template <tagged_variant_options Options, typename... NamedTypes>
__attribute__((noinline)) result<void> from_json(tagged_variant<Options, NamedTypes...>& result, const char*& pos,
const char* end) {
from_json_skip_space(pos, end);
from_json_expect(pos, end, '[', "expected array");
eosio::name name;
from_json(name, pos, end);
for (size_t i = 0; i < sizeof...(NamedTypes); ++i) {
if (name == tagged_variant<Options, NamedTypes...>::keys[i]) {
parse_named_variant_impl<0>(result, i, pos, end);
from_json_expect(pos, end, ']', "expected ]");
return;
}
}
check(false, "invalid variant index name");
}
*/
} // namespace eosio