@eosrio/node-abieos 2.1.1 → 2.2.0

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.
Files changed (99) hide show
  1. package/README.md +4 -1
  2. package/dist/abieos.node +0 -0
  3. package/lib/abieos.d.ts +19 -0
  4. package/lib/abieos.d.ts.map +1 -0
  5. package/lib/abieos.js +100 -0
  6. package/lib/abieos.ts +107 -0
  7. package/package.json +13 -8
  8. package/.idea/modules.xml +0 -8
  9. package/.idea/node-abieos.iml +0 -12
  10. package/.idea/vcs.xml +0 -9
  11. package/.idea/workspace.xml +0 -66
  12. package/CMakeLists.txt +0 -35
  13. package/abieos.d.ts +0 -21
  14. package/abieos.js +0 -59
  15. package/eosio.json +0 -1596
  16. package/include/.clang-format +0 -76
  17. package/include/eosio/abi.hpp +0 -393
  18. package/include/eosio/asset.hpp +0 -458
  19. package/include/eosio/bytes.hpp +0 -27
  20. package/include/eosio/chain_conversions.hpp +0 -450
  21. package/include/eosio/chain_types.hpp +0 -14
  22. package/include/eosio/check.hpp +0 -178
  23. package/include/eosio/convert.hpp +0 -95
  24. package/include/eosio/crypto.hpp +0 -148
  25. package/include/eosio/eosio_outcome.hpp +0 -41
  26. package/include/eosio/fixed_bytes.hpp +0 -250
  27. package/include/eosio/float.hpp +0 -35
  28. package/include/eosio/for_each_field.hpp +0 -48
  29. package/include/eosio/fpconv.c +0 -336
  30. package/include/eosio/fpconv.h +0 -41
  31. package/include/eosio/fpconv.license +0 -23
  32. package/include/eosio/from_bin.hpp +0 -272
  33. package/include/eosio/from_json.hpp +0 -749
  34. package/include/eosio/from_string.hpp +0 -28
  35. package/include/eosio/map_macro.h +0 -64
  36. package/include/eosio/murmur.hpp +0 -55
  37. package/include/eosio/name.hpp +0 -178
  38. package/include/eosio/opaque.hpp +0 -184
  39. package/include/eosio/operators.hpp +0 -71
  40. package/include/eosio/powers.h +0 -76
  41. package/include/eosio/reflection.hpp +0 -61
  42. package/include/eosio/ship_protocol.hpp +0 -971
  43. package/include/eosio/stream.hpp +0 -235
  44. package/include/eosio/symbol.hpp +0 -300
  45. package/include/eosio/time.hpp +0 -264
  46. package/include/eosio/to_bin.hpp +0 -189
  47. package/include/eosio/to_json.hpp +0 -334
  48. package/include/eosio/to_key.hpp +0 -305
  49. package/include/eosio/types.hpp +0 -103
  50. package/include/eosio/varint.hpp +0 -452
  51. package/include/outcome-basic.hpp +0 -6836
  52. package/include/rapidjson/allocators.h +0 -284
  53. package/include/rapidjson/cursorstreamwrapper.h +0 -78
  54. package/include/rapidjson/document.h +0 -2646
  55. package/include/rapidjson/encodedstream.h +0 -299
  56. package/include/rapidjson/encodings.h +0 -716
  57. package/include/rapidjson/error/en.h +0 -74
  58. package/include/rapidjson/error/error.h +0 -161
  59. package/include/rapidjson/filereadstream.h +0 -99
  60. package/include/rapidjson/filewritestream.h +0 -104
  61. package/include/rapidjson/fwd.h +0 -151
  62. package/include/rapidjson/internal/biginteger.h +0 -290
  63. package/include/rapidjson/internal/diyfp.h +0 -271
  64. package/include/rapidjson/internal/dtoa.h +0 -245
  65. package/include/rapidjson/internal/ieee754.h +0 -78
  66. package/include/rapidjson/internal/itoa.h +0 -308
  67. package/include/rapidjson/internal/meta.h +0 -186
  68. package/include/rapidjson/internal/pow10.h +0 -55
  69. package/include/rapidjson/internal/regex.h +0 -732
  70. package/include/rapidjson/internal/stack.h +0 -231
  71. package/include/rapidjson/internal/strfunc.h +0 -69
  72. package/include/rapidjson/internal/strtod.h +0 -290
  73. package/include/rapidjson/internal/swap.h +0 -46
  74. package/include/rapidjson/istreamwrapper.h +0 -113
  75. package/include/rapidjson/memorybuffer.h +0 -70
  76. package/include/rapidjson/memorystream.h +0 -71
  77. package/include/rapidjson/msinttypes/inttypes.h +0 -316
  78. package/include/rapidjson/msinttypes/stdint.h +0 -300
  79. package/include/rapidjson/ostreamwrapper.h +0 -81
  80. package/include/rapidjson/pointer.h +0 -1357
  81. package/include/rapidjson/prettywriter.h +0 -277
  82. package/include/rapidjson/rapidjson.h +0 -654
  83. package/include/rapidjson/reader.h +0 -2230
  84. package/include/rapidjson/schema.h +0 -2494
  85. package/include/rapidjson/stream.h +0 -223
  86. package/include/rapidjson/stringbuffer.h +0 -121
  87. package/include/rapidjson/writer.h +0 -710
  88. package/src/abi.cpp +0 -274
  89. package/src/abieos.cpp +0 -334
  90. package/src/abieos.h +0 -91
  91. package/src/abieos.hpp +0 -1011
  92. package/src/abieos_exception.hpp +0 -89
  93. package/src/abieos_numeric.hpp +0 -94
  94. package/src/abieos_ripemd160.hpp +0 -417
  95. package/src/crypto.cpp +0 -215
  96. package/src/main.cpp +0 -242
  97. package/src/ship.abi.cpp +0 -695
  98. package/tools/CMakeLists.txt +0 -9
  99. package/tools/name.cpp +0 -86
@@ -1,749 +0,0 @@
1
- #pragma once
2
-
3
- #include <cstdlib>
4
- #include "for_each_field.hpp"
5
- #include "check.hpp"
6
- #include <functional>
7
- #include <optional>
8
- #include <rapidjson/reader.h>
9
- #include <vector>
10
- #include <variant>
11
- #include <errno.h>
12
- #include <map>
13
-
14
- namespace eosio {
15
- enum class from_json_error {
16
- no_error,
17
-
18
- expected_end,
19
- expected_null,
20
- expected_bool,
21
- expected_string,
22
- expected_hex_string,
23
- hex_string_incorrect_length,
24
- invalid_signature,
25
- invalid_name,
26
- expected_start_object,
27
- expected_key,
28
- expected_end_object,
29
- expected_start_array,
30
- expected_end_array,
31
- expected_positive_uint,
32
- expected_field,
33
- expected_variant,
34
- expected_public_key,
35
- expected_private_key,
36
- expected_signature,
37
- expected_number,
38
- expected_int,
39
- expected_time_point,
40
- expected_symbol_code,
41
- expected_symbol,
42
- expected_asset,
43
- invalid_type_for_variant,
44
- unexpected_field,
45
- number_out_of_range,
46
- from_json_no_pair,
47
-
48
- // These are from rapidjson:
49
- document_empty,
50
- document_root_not_singular,
51
- value_invalid,
52
- object_miss_name,
53
- object_miss_colon,
54
- object_miss_comma_or_curly_bracket,
55
- array_miss_comma_or_square_bracket,
56
- string_unicode_escape_invalid_hex,
57
- string_unicode_surrogate_invalid,
58
- string_escape_invalid,
59
- string_miss_quotation_mark,
60
- string_invalid_encoding,
61
- number_too_big,
62
- number_miss_fraction,
63
- number_miss_exponent,
64
- terminated,
65
- unspecific_syntax_error,
66
- }; // from_json_error
67
-
68
- constexpr inline std::string_view convert_json_error(from_json_error e) {
69
- switch (e) {
70
- // clang-format off
71
- case from_json_error::no_error: return "No error";
72
-
73
- case from_json_error::expected_end: return "Expected end of json";
74
- case from_json_error::expected_null: return "Expected null";
75
- case from_json_error::expected_bool: return "Expected true or false";
76
- case from_json_error::expected_string: return "Expected string";
77
- case from_json_error::expected_hex_string: return "Expected string containing hex";
78
- case from_json_error::hex_string_incorrect_length: return "Hex string has incorrect length";
79
- case from_json_error::invalid_signature: return "Invalid signature format";
80
- case from_json_error::invalid_name: return "Invalid name";
81
- case from_json_error::expected_start_object: return "Expected {";
82
- case from_json_error::expected_key: return "Expected key";
83
- case from_json_error::expected_end_object: return "Expected }";
84
- case from_json_error::expected_start_array: return "Expected [";
85
- case from_json_error::expected_end_array: return "Expected ]";
86
- case from_json_error::expected_positive_uint: return "Expected positive integer";
87
- case from_json_error::expected_field: return "Expected field";
88
- case from_json_error::expected_variant: return R"(Expected variant: ["type", value])";
89
- case from_json_error::expected_public_key: return "Expected public key";
90
- case from_json_error::expected_private_key: return "Expected private key";
91
- case from_json_error::expected_signature: return "Expected signature";
92
- case from_json_error::expected_number: return "Expected number or boolean";
93
- case from_json_error::expected_int: return "Expected integer";
94
- case from_json_error::expected_time_point: return "Expected time point";
95
- case from_json_error::expected_symbol_code: return "Expected symbol code";
96
- case from_json_error::expected_symbol: return "Expected symbol";
97
- case from_json_error::expected_asset: return "Expected asset";
98
- case from_json_error::invalid_type_for_variant: return "Invalid type for variant";
99
- case from_json_error::unexpected_field: return "Unexpected field";
100
- case from_json_error::number_out_of_range: return "number is out of range";
101
- case from_json_error::from_json_no_pair: return "from_json does not support std::pair";
102
-
103
- case from_json_error::document_empty: return "The document is empty";
104
- case from_json_error::document_root_not_singular: return "The document root must not follow by other values";
105
- case from_json_error::value_invalid: return "Invalid value";
106
- case from_json_error::object_miss_name: return "Missing a name for object member";
107
- case from_json_error::object_miss_colon: return "Missing a colon after a name of object member";
108
- case from_json_error::object_miss_comma_or_curly_bracket: return "Missing a comma or '}' after an object member";
109
- case from_json_error::array_miss_comma_or_square_bracket: return "Missing a comma or ']' after an array element";
110
- case from_json_error::string_unicode_escape_invalid_hex: return "Incorrect hex digit after \\u escape in string";
111
- case from_json_error::string_unicode_surrogate_invalid: return "The surrogate pair in string is invalid";
112
- case from_json_error::string_escape_invalid: return "Invalid escape character in string";
113
- case from_json_error::string_miss_quotation_mark: return "Missing a closing quotation mark in string";
114
- case from_json_error::string_invalid_encoding: return "Invalid encoding in string";
115
- case from_json_error::number_too_big: return "Number too big to be stored in double";
116
- case from_json_error::number_miss_fraction: return "Miss fraction part in number";
117
- case from_json_error::number_miss_exponent: return "Miss exponent in number";
118
- case from_json_error::terminated: return "Parsing was terminated";
119
- case from_json_error::unspecific_syntax_error: return "Unspecific syntax error";
120
- // clang-format on
121
-
122
- default: return "unknown";
123
- }
124
- }
125
-
126
- constexpr inline std::string_view convert_json_error(int e) {
127
- return convert_json_error(static_cast<from_json_error>(e));
128
- }
129
-
130
- inline from_json_error convert_error(rapidjson::ParseErrorCode err) {
131
- switch (err) {
132
- // clang-format off
133
- case rapidjson::kParseErrorNone: return from_json_error::no_error;
134
- case rapidjson::kParseErrorDocumentEmpty: return from_json_error::document_empty;
135
- case rapidjson::kParseErrorDocumentRootNotSingular: return from_json_error::document_root_not_singular;
136
- case rapidjson::kParseErrorValueInvalid: return from_json_error::value_invalid;
137
- case rapidjson::kParseErrorObjectMissName: return from_json_error::object_miss_name;
138
- case rapidjson::kParseErrorObjectMissColon: return from_json_error::object_miss_colon;
139
- case rapidjson::kParseErrorObjectMissCommaOrCurlyBracket: return from_json_error::object_miss_comma_or_curly_bracket;
140
- case rapidjson::kParseErrorArrayMissCommaOrSquareBracket: return from_json_error::array_miss_comma_or_square_bracket;
141
- case rapidjson::kParseErrorStringUnicodeEscapeInvalidHex: return from_json_error::string_unicode_escape_invalid_hex;
142
- case rapidjson::kParseErrorStringUnicodeSurrogateInvalid: return from_json_error::string_unicode_surrogate_invalid;
143
- case rapidjson::kParseErrorStringEscapeInvalid: return from_json_error::string_escape_invalid;
144
- case rapidjson::kParseErrorStringMissQuotationMark: return from_json_error::string_miss_quotation_mark;
145
- case rapidjson::kParseErrorStringInvalidEncoding: return from_json_error::string_invalid_encoding;
146
- case rapidjson::kParseErrorNumberTooBig: return from_json_error::number_too_big;
147
- case rapidjson::kParseErrorNumberMissFraction: return from_json_error::number_miss_fraction;
148
- case rapidjson::kParseErrorNumberMissExponent: return from_json_error::number_miss_exponent;
149
- case rapidjson::kParseErrorTermination: return from_json_error::terminated;
150
- case rapidjson::kParseErrorUnspecificSyntaxError: return from_json_error::unspecific_syntax_error;
151
- // clang-format on
152
-
153
- default: return from_json_error::unspecific_syntax_error;
154
- }
155
- }
156
-
157
-
158
- inline auto convert_error_to_string_view(rapidjson::ParseErrorCode err) {
159
- return convert_json_error(convert_error(err));
160
- }
161
-
162
- enum class json_token_type {
163
- type_unread,
164
- type_null,
165
- type_bool,
166
- type_string,
167
- type_start_object,
168
- type_key,
169
- type_end_object,
170
- type_start_array,
171
- type_end_array,
172
- };
173
-
174
- struct json_token {
175
- json_token_type type = {};
176
- std::string_view key = {};
177
- bool value_bool = {};
178
- std::string_view value_string = {};
179
- };
180
-
181
- class json_token_stream : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, json_token_stream> {
182
- private:
183
- rapidjson::Reader reader;
184
- rapidjson::InsituStringStream ss;
185
-
186
- public:
187
- json_token current_token;
188
-
189
- // This modifies json
190
- json_token_stream(char* json) : ss{ json } { reader.IterativeParseInit(); }
191
-
192
- bool complete() { return reader.IterativeParseComplete(); }
193
-
194
- std::reference_wrapper<const json_token> peek_token() {
195
- if (current_token.type != json_token_type::type_unread)
196
- return current_token;
197
- check( reader.IterativeParseNext<rapidjson::kParseInsituFlag | rapidjson::kParseValidateEncodingFlag |
198
- rapidjson::kParseIterativeFlag | rapidjson::kParseNumbersAsStringsFlag>(ss, *this),
199
- convert_error_to_string_view(reader.GetParseErrorCode()) );
200
- return current_token;
201
- }
202
-
203
- void eat_token() { current_token.type = json_token_type::type_unread; }
204
-
205
- void get_end() {
206
- check( current_token.type == json_token_type::type_unread && complete(),
207
- convert_json_error(from_json_error::expected_end) );
208
- }
209
- bool get_null_pred() {
210
- auto t = peek_token();
211
- if(t.get().type != json_token_type::type_null)
212
- return false;
213
- eat_token();
214
- return true;
215
- }
216
- void get_null() {
217
- check(get_null_pred(),
218
- convert_json_error(from_json_error::expected_null) );
219
- }
220
-
221
- bool get_bool() {
222
- auto t = peek_token();
223
- check(t.get().type == json_token_type::type_bool,
224
- convert_json_error(from_json_error::expected_bool) );
225
- eat_token();
226
- return t.get().value_bool;
227
- }
228
-
229
- std::string_view get_string() {
230
- auto t = peek_token();
231
- check(t.get().type == json_token_type::type_string,
232
- convert_json_error(from_json_error::expected_string) );
233
- eat_token();
234
- return t.get().value_string;
235
- }
236
-
237
- void get_start_object() {
238
- auto t = peek_token();
239
- check(t.get().type == json_token_type::type_start_object,
240
- convert_json_error(from_json_error::expected_start_object) );
241
- eat_token();
242
- }
243
-
244
- std::string_view get_key() {
245
- auto t = peek_token();
246
- check(t.get().type == json_token_type::type_key,
247
- convert_json_error(from_json_error::expected_key) );
248
- eat_token();
249
- return t.get().key;
250
- }
251
-
252
- std::optional<std::string_view> maybe_get_key() {
253
- auto t = peek_token();
254
- if(t.get().type != json_token_type::type_key)
255
- return {};
256
- eat_token();
257
- return t.get().key;
258
- }
259
-
260
- bool get_end_object_pred() {
261
- auto t = peek_token();
262
- if(t.get().type != json_token_type::type_end_object)
263
- return false;
264
- eat_token();
265
- return true;
266
- }
267
-
268
- void get_end_object() {
269
- auto t = peek_token();
270
- check(t.get().type == json_token_type::type_end_object,
271
- convert_json_error(from_json_error::expected_end_object) );
272
- eat_token();
273
- }
274
-
275
- bool get_start_array_pred() {
276
- auto t = peek_token();
277
- if(t.get().type != json_token_type::type_start_array)
278
- return false;
279
- eat_token();
280
- return true;
281
- }
282
-
283
- bool get_end_array_pred() {
284
- auto t = peek_token();
285
- if(t.get().type != json_token_type::type_end_array)
286
- return false;
287
- eat_token();
288
- return true;
289
- }
290
- void get_start_array() {
291
- check(get_start_array_pred(),
292
- convert_json_error(from_json_error::expected_start_array) );
293
- }
294
-
295
- void get_end_array() {
296
- check(get_end_array_pred(),
297
- convert_json_error(from_json_error::expected_end_array));
298
- }
299
-
300
- // BaseReaderHandler methods
301
- bool Null() {
302
- current_token.type = json_token_type::type_null;
303
- return true;
304
- }
305
- bool Bool(bool v) {
306
- current_token.type = json_token_type::type_bool;
307
- current_token.value_bool = v;
308
- return true;
309
- }
310
- bool RawNumber(const char* v, rapidjson::SizeType length, bool copy) { return String(v, length, copy); }
311
- bool Int(int v) { return false; }
312
- bool Uint(unsigned v) { return false; }
313
- bool Int64(int64_t v) { return false; }
314
- bool Uint64(uint64_t v) { return false; }
315
- bool Double(double v) { return false; }
316
- bool String(const char* v, rapidjson::SizeType length, bool) {
317
- current_token.type = json_token_type::type_string;
318
- current_token.value_string = { v, length };
319
- return true;
320
- }
321
- bool StartObject() {
322
- current_token.type = json_token_type::type_start_object;
323
- return true;
324
- }
325
- bool Key(const char* v, rapidjson::SizeType length, bool) {
326
- current_token.key = { v, length };
327
- current_token.type = json_token_type::type_key;
328
- return true;
329
- }
330
- bool EndObject(rapidjson::SizeType) {
331
- current_token.type = json_token_type::type_end_object;
332
- return true;
333
- }
334
- bool StartArray() {
335
- current_token.type = json_token_type::type_start_array;
336
- return true;
337
- }
338
- bool EndArray(rapidjson::SizeType) {
339
- current_token.type = json_token_type::type_end_array;
340
- return true;
341
- }
342
- }; // json_token_stream
343
-
344
- template <typename SrcIt, typename DestIt>
345
- [[nodiscard]] bool unhex(DestIt dest, SrcIt begin, SrcIt end) {
346
- auto get_digit = [&](uint8_t& nibble) {
347
- if (*begin >= '0' && *begin <= '9')
348
- nibble = *begin++ - '0';
349
- else if (*begin >= 'a' && *begin <= 'f')
350
- nibble = *begin++ - 'a' + 10;
351
- else if (*begin >= 'A' && *begin <= 'F')
352
- nibble = *begin++ - 'A' + 10;
353
- else
354
- return false;
355
- return true;
356
- };
357
- while (begin != end) {
358
- uint8_t h, l;
359
- if (!get_digit(h) || !get_digit(l))
360
- return false;
361
- *dest++ = (h << 4) | l;
362
- }
363
- return true;
364
- }
365
-
366
- /// \exclude
367
- template <typename T, typename S>
368
- void from_json(T& result, S& stream);
369
-
370
- /// \group from_json_explicit Parse JSON (Explicit Types)
371
- /// Parse JSON and convert to `result`. These overloads handle specified types.
372
- template <typename S>
373
- void from_json(std::string_view& result, S& stream) {
374
- auto r = stream.get_string();
375
- result = r;
376
- }
377
-
378
- /// \group from_json_explicit Parse JSON (Explicit Types)
379
- /// Parse JSON and convert to `result`. These overloads handle specified types.
380
- template <typename S>
381
- void from_json(std::string& result, S& stream) {
382
- result = stream.get_string();
383
- }
384
-
385
- /// \exclude
386
- template <typename T, typename S>
387
- void from_json_int(T& result, S& stream) {
388
- auto r = stream.get_string();
389
- auto pos = r.data();
390
- auto end = pos + r.size();
391
- bool found = false;
392
- result = 0;
393
- T limit;
394
- T sign;
395
- if (std::is_signed_v<T> && pos != end && *pos == '-') {
396
- ++pos;
397
- sign = -1;
398
- limit = std::numeric_limits<T>::min();
399
- } else {
400
- sign = 1;
401
- limit = std::numeric_limits<T>::max();
402
- }
403
- while (pos != end && *pos >= '0' && *pos <= '9') {
404
- T digit = (*pos++ - '0');
405
- // abs(result) can overflow. Use -abs(result) instead.
406
- // TODO refactor this logic, don't have time now
407
- check(!(std::is_signed_v<T> && (-sign * limit + digit) / 10 > -sign * result),
408
- convert_json_error(from_json_error::number_out_of_range) );
409
- check(!(!std::is_signed_v<T> && (limit - digit) / 10 < result),
410
- convert_json_error(from_json_error::number_out_of_range) );
411
- result = result * 10 + sign * digit;
412
- found = true;
413
- }
414
- check( pos == end && found, convert_json_error(from_json_error::expected_int) );
415
- }
416
-
417
- /// \group from_json_explicit
418
- template <typename S>
419
- void from_json(uint8_t& result, S& stream) {
420
- from_json_int(result, stream);
421
- }
422
-
423
- /// \group from_json_explicit
424
- template <typename S>
425
- void from_json(uint16_t& result, S& stream) {
426
- from_json_int(result, stream);
427
- }
428
-
429
- /// \group from_json_explicit
430
- template <typename S>
431
- void from_json(uint32_t& result, S& stream) {
432
- from_json_int(result, stream);
433
- }
434
-
435
- /// \group from_json_explicit
436
- template <typename S>
437
- void from_json(uint64_t& result, S& stream) {
438
- from_json_int(result, stream);
439
- }
440
-
441
- /// \group from_json_explicit
442
- #ifndef ABIEOS_NO_INT128
443
- template <typename S>
444
- void from_json(unsigned __int128& result, S& stream) {
445
- from_json_int(result, stream);
446
- }
447
- #endif
448
-
449
- /// \group from_json_explicit
450
- template <typename S>
451
- void from_json(int8_t& result, S& stream) {
452
- from_json_int(result, stream);
453
- }
454
-
455
- /// \group from_json_explicit
456
- template <typename S>
457
- void from_json(int16_t& result, S& stream) {
458
- from_json_int(result, stream);
459
- }
460
-
461
- /// \group from_json_explicit
462
- template <typename S>
463
- void from_json(int32_t& result, S& stream) {
464
- from_json_int(result, stream);
465
- }
466
-
467
- /// \group from_json_explicit
468
- template <typename S>
469
- void from_json(int64_t& result, S& stream) {
470
- from_json_int(result, stream);
471
- }
472
-
473
- /// \group from_json_explicit
474
- #ifndef ABIEOS_NO_INT128
475
- template <typename S>
476
- void from_json(__int128& result, S& stream) {
477
- from_json_int(result, stream);
478
- }
479
- #endif
480
-
481
- template <typename S>
482
- void from_json(float& result, S& stream) {
483
- auto sv = stream.get_string();
484
- check( !sv.empty(), convert_json_error(from_json_error::expected_number) );
485
- std::string s(sv); // strtof expects a null-terminated string
486
- errno = 0;
487
- char* end;
488
- result = std::strtof(s.c_str(), &end);
489
- check( !errno && end == s.c_str() + s.size(),
490
- convert_json_error(from_json_error::expected_number) );
491
- }
492
-
493
- template <typename S>
494
- void from_json(double& result, S& stream) {
495
- auto sv = stream.get_string();
496
- check( !sv.empty(), convert_json_error(from_json_error::expected_number) );
497
- std::string s(sv);
498
- errno = 0;
499
- char* end;
500
- result = std::strtod(s.c_str(), &end);
501
- check( !errno && end == s.c_str() + s.size(), convert_json_error(from_json_error::expected_number) );
502
- }
503
-
504
- /*
505
- /// \group from_json_explicit
506
- template<typename S>
507
- result<void> from_json(int32_t& result, S& stream) {
508
- bool in_str = false;
509
- if (pos != end && *pos == '"') {
510
- in_str = true;
511
- from_json_skip_space(pos, end);
512
- }
513
- bool neg = false;
514
- if (pos != end && *pos == '-') {
515
- neg = true;
516
- ++pos;
517
- }
518
- bool found = false;
519
- result = 0;
520
- while (pos != end && *pos >= '0' && *pos <= '9') {
521
- result = result * 10 + *pos++ - '0';
522
- found = true;
523
- }
524
- check(found, "expected integer");
525
- from_json_skip_space(pos, end);
526
- if (in_str) {
527
- from_json_expect(pos, end, '"', "expected integer");
528
- from_json_skip_space(pos, end);
529
- }
530
- if (neg)
531
- result = -result;
532
- }
533
- */
534
- /// \group from_json_explicit
535
- template <typename S>
536
- void from_json(bool& result, S& stream) {
537
- result = stream.get_bool();
538
- }
539
-
540
- /// \group from_json_explicit
541
- template <typename T, typename S>
542
- void from_json(std::vector<T>& result, S& stream) {
543
- stream.get_start_array();
544
- while (true) {
545
- auto t = stream.peek_token();
546
- if (t.get().type == json_token_type::type_end_array)
547
- break;
548
- result.emplace_back();
549
- from_json(result.back(), stream);
550
- }
551
- stream.get_end_array();
552
- }
553
-
554
- /// \group from_json_explicit
555
- template <typename T, typename S>
556
- void from_json(std::optional<T>& result, S& stream) {
557
- if(stream.get_null_pred()) {
558
- result = std::nullopt;
559
- } else {
560
- result.emplace();
561
- from_json(*result, stream);
562
- }
563
- }
564
-
565
- /// \output_section Parse JSON
566
- /// Parse JSON and convert to `map`. This overload works with
567
- /// [reflected objects](standardese://reflection/).
568
- template <typename Key, typename Val, typename S>
569
- void from_json(std::map<Key, Val>& result, S& stream) {
570
- from_json_object(stream, [&](std::string_view key) {
571
- from_json(result[Key(key)], stream);
572
- });
573
- }
574
-
575
-
576
- template <int N = 0, typename... T>
577
- void set_variant_impl(std::variant<T...>& result, uint32_t type) {
578
- if (type == N) {
579
- result.template emplace<N>();
580
- } else if constexpr (N + 1 < sizeof...(T)) {
581
- set_variant_impl<N + 1>(result, type);
582
- }
583
- }
584
-
585
- /// \group from_json_explicit
586
- template <typename... T, typename S>
587
- void from_json(std::variant<T...>& result, S& stream) {
588
- stream.get_start_array();
589
- std::string_view type;
590
- from_json(type, stream);
591
- const char* const type_names[] = { get_type_name((T*)nullptr)... };
592
- uint32_t type_idx = std::find(type_names, type_names + sizeof...(T), type) - type_names;
593
- check( type_idx < sizeof...(T), convert_json_error(from_json_error::invalid_type_for_variant) );
594
- set_variant_impl(result, type_idx);
595
- std::visit([&](auto& x) { from_json(x, stream); }, result);
596
- stream.get_end_array();
597
- }
598
-
599
- /// \group from_json_explicit
600
- template <typename S>
601
- void from_json_hex(std::vector<char>& result, S& stream) {
602
- auto s = stream.get_string();
603
- check( !(s.size() & 1), convert_json_error(from_json_error::expected_hex_string) );
604
- result.clear();
605
- result.reserve(s.size() / 2);
606
- check( unhex(std::back_inserter(result), s.begin(), s.end()),
607
- convert_json_error(from_json_error::expected_hex_string) );
608
- }
609
-
610
- #ifdef __eosio_cdt__
611
-
612
- template <typename S> void from_json(long double& result, S& stream) {
613
- auto s = stream.get_string();
614
- check( s.size() == 32, convert_json_error(from_json_error::expected_hex_string) );
615
- check( unhex(reinterpret_cast<char*>(&result), s.begin(), s.end()),
616
- convert_json_error(from_json_error::expected_hex_string) );
617
- }
618
-
619
- #endif
620
-
621
- /// \exclude
622
- template <typename S, typename F>
623
- inline void from_json_object(S& stream, F f) {
624
- stream.get_start_object();
625
- while (true) {
626
- auto t = stream.peek_token();
627
- if (t.get().type == json_token_type::type_end_object)
628
- break;
629
- auto k = stream.get_key();
630
- f(k);
631
- }
632
- stream.get_end_object();
633
- }
634
-
635
- template <typename S>
636
- void from_json_skip_value(S& stream) {
637
- uint64_t depth = 0;
638
- do {
639
- auto t = stream.peek_token();
640
- auto type = t.get().type;
641
- if (type == json_token_type::type_start_object || type == json_token_type::type_start_array)
642
- ++depth;
643
- else if (type == json_token_type::type_end_object || type == json_token_type::type_end_array)
644
- --depth;
645
- stream.eat_token();
646
- } while (depth);
647
- }
648
-
649
- /// \output_section Parse JSON (Reflected Objects)
650
- /// Parse JSON and convert to `obj`. This overload works with
651
- /// [reflected objects](standardese://reflection/).
652
- template <typename T, typename S>
653
- void from_json(T& obj, S& stream) {
654
- from_json_object(stream, [&](std::string_view key) {
655
- bool found = false;
656
- eosio::for_each_field<T>([&](std::string_view member_name, auto member) {
657
- if (!found && key == member_name) {
658
- from_json(member(&obj), stream);
659
- found = true;
660
- }
661
- });
662
- if (!found)
663
- from_json_skip_value(stream);
664
- });
665
- }
666
-
667
- template <typename First, typename Second, typename S>
668
- void from_json(std::pair<First, Second>& obj, S& stream) {
669
- check( false, convert_json_error(from_json_error::from_json_no_pair) );
670
- }
671
-
672
- /*
673
- /// \output_section Convenience Wrappers
674
- /// Parse JSON and return result. This overload wraps the other `to_json` overloads.
675
- template <typename T>
676
- T from_json(const std::vector<char>& v) {
677
- const char* pos = v.data();
678
- const char* end = pos + v.size();
679
- from_json_skip_space(pos, end);
680
- T result;
681
- from_json(result, pos, end);
682
- from_json_expect_end(pos, end);
683
- return result;
684
- }
685
-
686
- /// Parse JSON and return result. This overload wraps the other `to_json` overloads.
687
- template <typename T>
688
- T from_json(std::string_view s) {
689
- const char* pos = s.data();
690
- const char* end = pos + s.size();
691
- from_json_skip_space(pos, end);
692
- T result;
693
- from_json(result, pos, end);
694
- from_json_expect_end(pos, end);
695
- return result;
696
- }
697
- */
698
-
699
- /// Parse JSON and return result. This overload wraps the other `to_json` overloads.
700
- template <typename T, typename S>
701
- T from_json(S& stream) {
702
- T x;
703
- from_json(x, stream);
704
- return x;
705
- }
706
-
707
- /*
708
- /// \exclude
709
- template <size_t I, tagged_variant_options Options, typename... NamedTypes>
710
- __attribute__((noinline)) void parse_named_variant_impl(tagged_variant<Options, NamedTypes...>& v, size_t i,
711
- const char*& pos, const char* end) {
712
- if constexpr (I < sizeof...(NamedTypes)) {
713
- if (i == I) {
714
- auto& q = v.value;
715
- auto& x = q.template emplace<I>();
716
- if constexpr (!is_named_empty_type_v<std::decay_t<decltype(x)>>) {
717
- from_json_expect(pos, end, ',', "expected ,");
718
- from_json(x, pos, end);
719
- }
720
- } else {
721
- return parse_named_variant_impl<I + 1>(v, i, pos, end);
722
- }
723
- } else {
724
- check(false, "invalid variant index");
725
- }
726
- }
727
-
728
- /// \group from_json_explicit
729
- template <tagged_variant_options Options, typename... NamedTypes>
730
- __attribute__((noinline)) result<void> from_json(tagged_variant<Options, NamedTypes...>& result, const char*& pos,
731
- const char* end) {
732
- from_json_skip_space(pos, end);
733
- from_json_expect(pos, end, '[', "expected array");
734
-
735
- eosio::name name;
736
- from_json(name, pos, end);
737
-
738
- for (size_t i = 0; i < sizeof...(NamedTypes); ++i) {
739
- if (name == tagged_variant<Options, NamedTypes...>::keys[i]) {
740
- parse_named_variant_impl<0>(result, i, pos, end);
741
- from_json_expect(pos, end, ']', "expected ]");
742
- return;
743
- }
744
- }
745
- check(false, "invalid variant index name");
746
- }
747
- */
748
-
749
- } // namespace eosio