Line data Source code
1 : /*
2 : * Protobuf serialization library - JSON deserialize library.
3 : *
4 : * Copyright (c) 2024 Antonin Kriz <antonin.kriz@gmail.com>
5 : * Released under the MIT License. See LICENSE in the project root.
6 : */
7 :
8 : #pragma once
9 :
10 : #include "../bits.h"
11 : #include "../concepts.h"
12 : #include "../to_from_chars.h"
13 : #include "../utf8.h"
14 : #include "base64.h"
15 : #include "field.hpp"
16 : #include <algorithm>
17 : #include <cctype>
18 : #include <cstddef>
19 : #include <cstdint>
20 : #include <cstring>
21 : #include <memory>
22 : #include <spb/io/buffer-io.hpp>
23 : #include <spb/io/io.hpp>
24 : #include <stdexcept>
25 : #include <string>
26 : #include <string_view>
27 : #include <type_traits>
28 : #include <variant>
29 :
30 : namespace spb::json::detail
31 : {
32 : using namespace std::literals;
33 :
34 : static const auto escape = '\\';
35 :
36 : /**
37 : * @brief helper for std::variant visit
38 : * https://en.cppreference.com/w/cpp/utility/variant/visit
39 : *
40 : */
41 : template <class... Ts> struct overloaded : Ts...
42 : {
43 : using Ts::operator()...;
44 : };
45 : // explicit deduction guide (not needed as of C++20)
46 : template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
47 :
48 : /**
49 : * @brief djb2_hash for strings
50 : * http://www.cse.yorku.ca/~oz/hash.html
51 : *
52 : * @param str
53 : * @return uint32_t
54 : */
55 3550 : static constexpr inline auto djb2_hash(std::string_view str) noexcept -> uint32_t
56 : {
57 3550 : uint32_t hash = 5381U;
58 :
59 34158 : for (auto c : str)
60 : {
61 30608 : hash = ((hash << 5U) + hash) + uint8_t(c); /* hash * 33 + c */
62 : }
63 :
64 3550 : return hash;
65 : }
66 :
67 : static constexpr inline auto fnv1a_hash(std::string_view str) noexcept -> uint64_t
68 : {
69 : uint64_t hash = 14695981039346656037ULL;
70 : const uint64_t prime = 1099511628211ULL;
71 :
72 : for (auto c : str)
73 : {
74 : hash *= prime;
75 : hash ^= c;
76 : }
77 :
78 : return hash;
79 : }
80 :
81 2 : template <spb::detail::proto_field_bytes T> void clear(T &container)
82 : {
83 : if constexpr (spb::detail::proto_field_bytes_resizable<T>)
84 : {
85 2 : container.clear();
86 : }
87 : else
88 : {
89 0 : std::fill(container.begin(), container.end(), typename T::value_type());
90 : }
91 2 : }
92 :
93 : struct istream_buffer
94 : {
95 : const uint8_t *p_start;
96 : const uint8_t *p_end;
97 :
98 : istream_buffer(const void *start, const void *end) noexcept
99 : : p_start((uint8_t *)start), p_end((uint8_t *)end)
100 : {
101 : assert(start <= end);
102 : }
103 3983 : istream_buffer(const void *start, size_t size) noexcept : p_start((uint8_t *)start), p_end(p_start + size)
104 : {
105 3983 : }
106 22076 : [[nodiscard]] size_t size() const noexcept
107 : {
108 22076 : return p_end - p_start;
109 : }
110 7795 : [[nodiscard]] bool empty() const noexcept
111 : {
112 7795 : return p_start >= p_end;
113 : }
114 7660 : void skip(size_t chars)
115 : {
116 7660 : assert(size() >= chars);
117 7660 : p_start += chars;
118 7660 : }
119 12850 : [[nodiscard]] bool consume(char c)
120 : {
121 12850 : if (current_char() != c)
122 4953 : return false;
123 :
124 7897 : ++p_start;
125 7897 : return true;
126 : }
127 :
128 832 : [[nodiscard]] bool consume(std::string_view token)
129 : {
130 832 : assert(!token.empty());
131 :
132 832 : if (size() < token.size()) [[unlikely]]
133 99 : return false;
134 :
135 733 : if (memcmp(token.data(), p_start, token.size()) != 0)
136 666 : return false;
137 :
138 67 : skip(token.size());
139 67 : return true;
140 : }
141 :
142 14073 : [[nodiscard]] int current_char() const
143 : {
144 14073 : return (p_start < p_end) ? *p_start : 0;
145 : }
146 :
147 7045 : void skip_white_spaces() noexcept
148 : {
149 7475 : while (!empty() && isspace(*p_start))
150 : {
151 430 : ++p_start;
152 : }
153 7045 : }
154 :
155 320 : void consume_current_char()
156 : {
157 320 : if (empty()) [[unlikely]]
158 1 : throw std::runtime_error("unexpected end of stream");
159 :
160 319 : ++p_start;
161 319 : }
162 :
163 7590 : [[nodiscard]] auto view(size_t min_size, size_t max_size) -> std::string_view
164 : {
165 7590 : if (size() < min_size) [[unlikely]]
166 2581 : throw std::runtime_error("unexpected end of stream");
167 :
168 5009 : return {(char *)p_start, std::min(size(), max_size)};
169 : }
170 : };
171 :
172 : struct istream_reader
173 : {
174 : private:
175 : spb::io::buffered_reader reader;
176 : size_t m_consumed_size = 0;
177 :
178 5607 : void consume_bytes(size_t size) noexcept
179 : {
180 5607 : m_consumed_size += size;
181 5607 : reader.skip(size);
182 5607 : }
183 :
184 : public:
185 388 : istream_reader(spb::io::reader reader) : reader(reader)
186 : {
187 388 : }
188 :
189 388 : size_t consumed_size() const noexcept
190 : {
191 388 : return m_consumed_size;
192 : }
193 :
194 3275 : [[nodiscard]] auto current_char() -> char
195 : {
196 3275 : auto view = reader.view(1);
197 3275 : if (view.empty())
198 0 : return 0;
199 :
200 3275 : return view[0];
201 : }
202 :
203 2941 : [[nodiscard]] auto consume(char c) -> bool
204 : {
205 2941 : if (current_char() != c)
206 943 : return false;
207 :
208 1998 : skip(1);
209 1998 : return true;
210 : }
211 :
212 253 : [[nodiscard]] auto consume(std::string_view token) -> bool
213 : {
214 253 : assert(!token.empty());
215 :
216 253 : const auto token_view = reader.view(token.size());
217 253 : if (!token_view.starts_with(token))
218 241 : return false;
219 :
220 12 : skip(token.size());
221 12 : return true;
222 : }
223 :
224 2776 : void skip_white_spaces()
225 : {
226 : for (;;)
227 : {
228 2776 : auto view = reader.view(1);
229 2776 : if (view.empty())
230 0 : return;
231 :
232 2776 : size_t spaces = 0;
233 2776 : for (auto c : view)
234 : {
235 2776 : if (!isspace(c))
236 : {
237 2776 : consume_bytes(spaces);
238 2776 : return;
239 : }
240 0 : spaces += 1;
241 : }
242 0 : consume_bytes(spaces);
243 0 : }
244 : }
245 :
246 783 : [[nodiscard]] auto view(size_t min_size, size_t max_size) -> std::string_view
247 : {
248 783 : auto result = reader.view(max_size);
249 783 : if (result.size() < min_size) [[unlikely]]
250 0 : throw std::runtime_error("unexpected end of stream");
251 :
252 783 : if (result.size() > max_size)
253 359 : result = result.substr(0, max_size);
254 :
255 783 : return result;
256 : }
257 :
258 16 : void consume_current_char()
259 : {
260 16 : if (current_char() == 0)
261 0 : throw std::runtime_error("unexpected end of stream");
262 :
263 16 : consume_bytes(1);
264 16 : }
265 :
266 2815 : void skip(size_t size) noexcept
267 : {
268 2815 : consume_bytes(size);
269 2815 : }
270 : };
271 :
272 : [[nodiscard]] bool eof(const auto &stream)
273 : {
274 : return stream.current_char() == 0;
275 : }
276 :
277 70 : template <field_attributes> void deserialize(auto &stream, spb::detail::proto_enum auto &value)
278 : {
279 70 : stream.skip_white_spaces();
280 70 : deserialize_value(stream, value);
281 68 : stream.skip_white_spaces();
282 68 : }
283 :
284 26 : void ignore_string_until_double_quota(auto &stream)
285 : {
286 0 : for (;;)
287 : {
288 26 : auto view = stream.view(1, UINT32_MAX);
289 26 : auto pos = view.find_first_of(R"(\")");
290 26 : if (pos == view.npos)
291 : {
292 0 : stream.skip(view.size());
293 0 : continue;
294 : }
295 :
296 26 : if (view[pos] == '"') [[likely]]
297 : {
298 : // +1 for '""
299 26 : stream.skip(pos + 1);
300 26 : return;
301 : }
302 :
303 0 : stream.skip(pos + 1);
304 : // +1 for char behind '\'
305 0 : (void)stream.view(1, UINT32_MAX);
306 0 : stream.skip(1);
307 : }
308 : }
309 :
310 19 : void ignore_string(auto &stream)
311 : {
312 19 : if (!stream.consume('"')) [[unlikely]]
313 1 : throw std::runtime_error(R"(expecting '"')");
314 :
315 18 : if (stream.consume('"')) [[unlikely]]
316 0 : return;
317 :
318 18 : ignore_string_until_double_quota(stream);
319 : }
320 :
321 1323 : auto deserialize_string_to_buffer(auto &stream, size_t min_size, size_t max_size, char *buffer)
322 : -> std::string_view
323 : {
324 1323 : assert(max_size < io::buffered_reader::BUFFER_SIZE);
325 :
326 1323 : if (!stream.consume('"')) [[unlikely]]
327 1 : throw std::runtime_error(R"(expecting '"')");
328 :
329 : // +1 for "
330 1322 : auto view = stream.view(1, max_size + 1);
331 1322 : size_t start = 0;
332 0 : for (;;)
333 : {
334 1322 : const auto pos = view.find_first_of(R"(\")", start);
335 1322 : if (pos == view.npos) [[unlikely]]
336 : {
337 8 : stream.skip(view.size());
338 8 : break;
339 : }
340 :
341 1314 : if (view[pos] == '"') [[likely]]
342 : {
343 1314 : memcpy(buffer, view.data(), pos);
344 : // +1 for '""
345 1314 : stream.skip(pos + 1);
346 1314 : stream.skip_white_spaces();
347 1314 : return (pos >= min_size) ? std::string_view{buffer, pos} : std::string_view{};
348 : }
349 :
350 : // +2 for \ and char behind
351 0 : start = pos + 2;
352 : }
353 8 : ignore_string_until_double_quota(stream);
354 8 : return {};
355 : }
356 :
357 14 : auto unicode_from_hex(auto &stream) -> uint16_t
358 : {
359 14 : const auto esc_size = 4U;
360 14 : auto unicode_view = stream.view(esc_size, esc_size);
361 11 : if (unicode_view.size() < esc_size) [[unlikely]]
362 0 : throw std::runtime_error("invalid escape sequence");
363 :
364 11 : auto value = uint16_t(0);
365 11 : auto result = spb_std_emu::from_chars(unicode_view.data(), unicode_view.data() + esc_size, value, 16);
366 11 : if (result.ec != std::errc{} || result.ptr != unicode_view.data() + esc_size) [[unlikely]]
367 2 : throw std::runtime_error("invalid escape sequence");
368 :
369 9 : stream.skip(esc_size);
370 9 : return value;
371 : }
372 :
373 10 : auto unescape_unicode(auto &stream, char utf8[4]) -> uint32_t
374 : {
375 9 : auto value = uint32_t(unicode_from_hex(stream));
376 7 : if (value >= 0xD800 && value <= 0xDBFF && stream.view(2, 2).starts_with("\\u"sv))
377 : {
378 4 : stream.skip(2);
379 4 : auto low = unicode_from_hex(stream);
380 :
381 2 : if (low < 0xDC00 || low > 0xDFFF) [[unlikely]]
382 2 : throw std::invalid_argument("invalid escape sequence");
383 :
384 0 : value = ((value - 0xD800) << 10) + (low - 0xDC00) + 0x10000;
385 : }
386 3 : if (auto result = spb::detail::utf8::encode_point(value, utf8); result != 0)
387 3 : return result;
388 :
389 0 : throw std::runtime_error("invalid escape sequence");
390 : }
391 :
392 319 : auto unescape(auto &stream, char utf8[4]) -> uint32_t
393 : {
394 319 : auto c = stream.current_char();
395 319 : stream.consume_current_char();
396 318 : switch (c)
397 : {
398 12 : case '"':
399 12 : utf8[0] = '"';
400 12 : return 1;
401 7 : case '\\':
402 7 : utf8[0] = '\\';
403 7 : return 1;
404 1 : case '/':
405 1 : utf8[0] = '/';
406 1 : return 1;
407 5 : case 'b':
408 5 : utf8[0] = '\b';
409 5 : return 1;
410 5 : case 'f':
411 5 : utf8[0] = '\f';
412 5 : return 1;
413 9 : case 'n':
414 9 : utf8[0] = '\n';
415 9 : return 1;
416 8 : case 'r':
417 8 : utf8[0] = '\r';
418 8 : return 1;
419 15 : case 't':
420 15 : utf8[0] = '\t';
421 15 : return 1;
422 10 : case 'u':
423 9 : return unescape_unicode(stream, utf8);
424 246 : default:
425 246 : throw std::runtime_error("invalid escape sequence");
426 : }
427 : }
428 :
429 : template <field_attributes attributes>
430 579 : void deserialize(auto &stream, spb::detail::proto_field_string auto &value)
431 : {
432 579 : if (!stream.consume('"')) [[unlikely]]
433 7 : throw std::runtime_error(R"(expecting '"')");
434 :
435 : if constexpr (spb::detail::proto_field_string_resizable<decltype(value)>)
436 : {
437 545 : value.clear();
438 : }
439 572 : auto index = size_t(0);
440 1286 : auto append_to_value = [&](const char *str, size_t size)
441 : {
442 : if constexpr (attributes.max_size)
443 40 : check_size(value.size() + size, attributes.max_size);
444 :
445 : if constexpr (spb::detail::proto_field_string_resizable<decltype(value)>)
446 : {
447 535 : value.append(str, size);
448 : }
449 : else
450 : {
451 126 : if (auto space_left = value.size() - index; size <= space_left) [[likely]]
452 : {
453 61 : memcpy(value.data() + index, str, size);
454 61 : index += size;
455 : }
456 : else
457 : {
458 2 : throw std::runtime_error("invalid string size");
459 : }
460 : }
461 : };
462 :
463 66 : for (;;)
464 : {
465 638 : auto view = stream.view(1, UINT32_MAX);
466 637 : auto found = view.find_first_of(R"("\)");
467 637 : if (found == view.npos) [[unlikely]]
468 : {
469 1 : append_to_value(view.data(), view.size());
470 1 : stream.skip(view.size());
471 1 : continue;
472 : }
473 :
474 636 : append_to_value(view.data(), found);
475 : // +1 for '"' or '\'
476 626 : stream.skip(found + 1);
477 626 : if (view[found] == '"') [[likely]]
478 : {
479 : if constexpr (!spb::detail::proto_field_string_resizable<decltype(value)>)
480 : {
481 25 : if (index != value.size()) [[unlikely]]
482 2 : throw std::runtime_error("invalid string size");
483 : }
484 305 : return;
485 : }
486 : char utf8_buffer[4];
487 319 : auto utf8_size = unescape(stream, utf8_buffer);
488 65 : append_to_value(utf8_buffer, utf8_size);
489 : }
490 : spb::detail::utf8::validate(std::string_view(value.data(), value.size()));
491 : }
492 :
493 1106 : template <field_attributes> void deserialize(auto &stream, spb::detail::proto_field_int_or_float auto &value)
494 : {
495 1106 : if (stream.current_char() == '"') [[unlikely]]
496 : {
497 : //- https://protobuf.dev/programming-guides/proto2/#json
498 : //- number can be a string
499 : char buffer[32];
500 42 : auto view = deserialize_string_to_buffer(stream, 1, 32, buffer);
501 42 : auto result = spb_std_emu::from_chars(view.data(), view.data() + view.size(), value);
502 42 : if (result.ec != std::errc{} || result.ptr != (view.data() + view.size())) [[unlikely]]
503 12 : throw std::runtime_error("invalid number");
504 :
505 30 : return;
506 : }
507 1064 : auto view = stream.view(1, 32);
508 1051 : auto result = spb_std_emu::from_chars(view.data(), view.data() + view.size(), value);
509 1051 : if (result.ec != std::errc{}) [[unlikely]]
510 29 : throw std::runtime_error("invalid number");
511 :
512 1022 : stream.skip(result.ptr - view.data());
513 : }
514 :
515 69 : template <field_attributes> void deserialize(auto &stream, bool &value)
516 : {
517 69 : if (stream.consume("true"sv))
518 : {
519 34 : value = true;
520 : }
521 35 : else if (stream.consume("false"sv))
522 : {
523 27 : value = false;
524 : }
525 8 : else [[unlikely]]
526 : {
527 8 : throw std::runtime_error("expecting 'true' or 'false'");
528 : }
529 61 : }
530 :
531 : template <field_attributes> void deserialize(auto &stream, spb::detail::proto_message auto &value);
532 : template <field_attributes> void deserialize(auto &stream, spb::detail::proto_map auto &value);
533 :
534 : template <field_attributes attributes, spb::detail::proto_label_optional Container>
535 : void deserialize(auto &stream, Container &p_value);
536 :
537 : template <field_attributes attributes, spb::detail::proto_label_repeated Container>
538 380 : void deserialize(auto &stream, Container &value)
539 : {
540 380 : if (stream.consume("null"sv))
541 : {
542 5 : value.clear();
543 5 : return;
544 : }
545 :
546 375 : if (!stream.consume('[')) [[unlikely]]
547 10 : throw std::runtime_error("expecting '['");
548 :
549 365 : if (stream.consume(']'))
550 5 : return;
551 :
552 : do
553 : {
554 752 : stream.skip_white_spaces();
555 : if constexpr (attributes.max_count)
556 325 : check_size(value.size() + 1, attributes.max_count);
557 :
558 : if constexpr (std::is_same_v<typename Container::value_type, bool>)
559 : {
560 22 : auto b = false;
561 22 : deserialize<attributes>(stream, b);
562 20 : value.push_back(b);
563 : }
564 : else
565 : {
566 721 : deserialize<attributes>(stream, value.emplace_back());
567 : }
568 725 : stream.skip_white_spaces();
569 725 : } while (stream.consume(','));
570 :
571 333 : stream.skip_white_spaces();
572 333 : if (!stream.consume(']')) [[unlikely]]
573 2 : throw std::runtime_error("expecting ']'");
574 : }
575 :
576 : template <field_attributes attributes, spb::detail::proto_label_repeated_fixed_size Container>
577 14 : void deserialize(auto &stream, Container &value)
578 : {
579 14 : if (stream.consume("null"sv))
580 : {
581 0 : typename Container::value_type tmp = {};
582 0 : for (size_t i = 0; i < value.size(); i++)
583 : {
584 0 : value[i] = tmp;
585 : }
586 0 : return;
587 : }
588 :
589 14 : if (!stream.consume('[')) [[unlikely]]
590 0 : throw std::runtime_error("expecting '['");
591 :
592 112 : for (size_t i = 0; i < value.size(); i++)
593 : {
594 56 : stream.skip_white_spaces();
595 : typename Container::value_type tmp;
596 56 : deserialize<attributes>(stream, tmp);
597 52 : value[i] = tmp;
598 104 : if (i + 1 >= value.size())
599 10 : break;
600 :
601 42 : stream.skip_white_spaces();
602 80 : while (stream.consume(','))
603 : {
604 38 : stream.skip_white_spaces();
605 : }
606 : }
607 :
608 10 : if (!stream.consume(']')) [[unlikely]]
609 4 : throw std::runtime_error("expecting ']'");
610 : }
611 :
612 : template <field_attributes attributes>
613 183 : void deserialize(auto &stream, spb::detail::proto_field_bytes auto &value)
614 : {
615 183 : if (stream.consume("null"sv))
616 : {
617 2 : clear(value);
618 2 : return;
619 : }
620 :
621 181 : base64_decode_string(value, stream, attributes.max_size);
622 : }
623 :
624 49 : template <field_attributes attributes, typename T> void deserialize_map_key(auto &stream, T &map_key)
625 : {
626 : if constexpr (std::is_same_v<T, std::string>)
627 : {
628 26 : deserialize<attributes>(stream, map_key);
629 : }
630 : else
631 : {
632 : char buffer[128];
633 23 : auto str_key_map = deserialize_string_to_buffer(stream, 1, 128, buffer);
634 23 : auto key_stream = istream_buffer(str_key_map.data(), str_key_map.size());
635 23 : deserialize<attributes>(key_stream, map_key);
636 : }
637 47 : }
638 :
639 55 : template <field_attributes attributes> void deserialize(auto &stream, spb::detail::proto_map auto &value)
640 : {
641 : using map_type = std::remove_cvref_t<decltype(value)>;
642 : using key_type = typename map_type::key_type;
643 : using mapped_type = typename map_type::mapped_type;
644 :
645 55 : if (stream.consume("null"sv))
646 : {
647 4 : value.clear();
648 4 : return;
649 : }
650 :
651 51 : if (!stream.consume('{')) [[unlikely]]
652 5 : throw std::runtime_error("expecting '{'");
653 :
654 46 : if (stream.consume('}'))
655 4 : return;
656 :
657 : do
658 : {
659 49 : stream.skip_white_spaces();
660 49 : auto map_key = key_type();
661 49 : deserialize_map_key<attributes>(stream, map_key);
662 47 : stream.skip_white_spaces();
663 47 : if (!stream.consume(':')) [[unlikely]]
664 1 : throw std::runtime_error("expecting ':'");
665 :
666 46 : stream.skip_white_spaces();
667 46 : auto map_value = mapped_type();
668 46 : deserialize<attributes>(stream, map_value);
669 82 : value.emplace(std::move(map_key), std::move(map_value));
670 41 : stream.skip_white_spaces();
671 48 : } while (stream.consume(','));
672 :
673 34 : if (!stream.consume('}')) [[unlikely]]
674 1 : throw std::runtime_error("expecting '}'");
675 : }
676 :
677 : template <field_attributes attributes, spb::detail::proto_label_optional Container>
678 334 : void deserialize(auto &stream, Container &p_value)
679 : {
680 334 : if (stream.consume("null"sv))
681 : {
682 4 : p_value.reset();
683 4 : return;
684 : }
685 :
686 330 : if (p_value.has_value())
687 : {
688 4 : deserialize<attributes>(stream, *p_value);
689 : }
690 : else
691 : {
692 372 : deserialize<attributes>(stream, p_value.emplace(typename Container::value_type()));
693 : }
694 : }
695 :
696 13 : template <field_attributes attributes, typename T> void deserialize(auto &stream, std::unique_ptr<T> &value)
697 : {
698 13 : if (stream.consume("null"sv))
699 : {
700 2 : value.reset();
701 2 : return;
702 : }
703 :
704 11 : if (value)
705 : {
706 5 : deserialize<attributes>(stream, *value);
707 : }
708 : else
709 : {
710 6 : value = std::make_unique<T>();
711 6 : deserialize<attributes>(stream, *value);
712 : }
713 : }
714 :
715 : void ignore_value(auto &stream);
716 10 : void ignore_key_and_value(auto &stream)
717 : {
718 10 : ignore_string(stream);
719 9 : stream.skip_white_spaces();
720 9 : if (!stream.consume(':')) [[unlikely]]
721 1 : throw std::runtime_error("expecting ':'");
722 8 : ignore_value(stream);
723 7 : }
724 :
725 8 : void ignore_object(auto &stream)
726 : {
727 : //- '{' was already checked by caller
728 8 : stream.consume_current_char();
729 8 : stream.skip_white_spaces();
730 :
731 8 : if (stream.consume('}'))
732 1 : return;
733 :
734 : do
735 : {
736 10 : stream.skip_white_spaces();
737 10 : ignore_key_and_value(stream);
738 7 : stream.skip_white_spaces();
739 7 : } while (stream.consume(','));
740 :
741 4 : if (!stream.consume('}'))
742 1 : throw std::runtime_error("expecting '}'");
743 : }
744 :
745 9 : void ignore_array(auto &stream)
746 : {
747 : //- '[' was already checked by caller
748 9 : stream.consume_current_char();
749 9 : stream.skip_white_spaces();
750 :
751 9 : if (stream.consume(']'))
752 2 : return;
753 :
754 : do
755 : {
756 9 : ignore_value(stream);
757 6 : } while (stream.consume(','));
758 :
759 4 : if (!stream.consume(']')) [[unlikely]]
760 1 : throw std::runtime_error("expecting ']");
761 : }
762 :
763 13 : void ignore_number(auto &stream)
764 : {
765 : double value;
766 13 : deserialize<field_attributes{}>(stream, value);
767 9 : }
768 :
769 5 : void ignore_bool(auto &stream)
770 : {
771 : bool value;
772 5 : deserialize<field_attributes{}>(stream, value);
773 3 : }
774 :
775 2 : void ignore_null(auto &stream)
776 : {
777 2 : if (!stream.consume("null"sv)) [[unlikely]]
778 1 : throw std::runtime_error("expecting 'null'");
779 1 : }
780 :
781 46 : void ignore_value(auto &stream)
782 : {
783 46 : stream.skip_white_spaces();
784 46 : switch (stream.current_char())
785 : {
786 8 : case '{':
787 8 : return ignore_object(stream);
788 9 : case '[':
789 9 : return ignore_array(stream);
790 9 : case '"':
791 9 : return ignore_string(stream);
792 2 : case 'n':
793 2 : return ignore_null(stream);
794 5 : case 't':
795 : case 'f':
796 5 : return ignore_bool(stream);
797 13 : default:
798 13 : return ignore_number(stream);
799 : }
800 : }
801 :
802 237 : template <field_attributes attributes, typename T> T deserialize_bitfield(auto &stream, uint32_t bits)
803 : {
804 237 : auto value = T();
805 237 : deserialize<attributes>(stream, value);
806 237 : spb::detail::check_if_value_fit_in_bits(value, bits);
807 236 : return value;
808 : }
809 :
810 : template <field_attributes attributes, size_t ordinal, typename T>
811 16 : void deserialize_variant(auto &stream, T &variant)
812 : {
813 16 : deserialize<attributes>(stream, variant.template emplace<ordinal>());
814 16 : }
815 :
816 1386 : template <field_attributes> void deserialize(auto &stream, spb::detail::proto_message auto &value)
817 : {
818 1386 : if (!stream.consume('{')) [[unlikely]]
819 2 : throw std::runtime_error("expecting '{'");
820 :
821 1384 : stream.skip_white_spaces();
822 1384 : if (stream.consume('}'))
823 224 : return;
824 :
825 : for (;;)
826 : {
827 1191 : stream.skip_white_spaces();
828 : //- generated by the sprotoc
829 1191 : deserialize_value(stream, value);
830 1126 : stream.skip_white_spaces();
831 :
832 1126 : if (stream.consume(','))
833 31 : continue;
834 :
835 1095 : if (stream.consume('}'))
836 1094 : return;
837 :
838 1 : throw std::runtime_error("expecting '}' or ','");
839 : }
840 : }
841 :
842 1191 : auto deserialize_key(auto &stream, size_t min_size, size_t max_size, char *buffer) -> std::string_view
843 : {
844 1191 : stream.skip_white_spaces();
845 1191 : auto key = deserialize_string_to_buffer(stream, min_size, max_size, buffer);
846 1190 : if (!stream.consume(':')) [[unlikely]]
847 1 : throw std::runtime_error("expecting ':'");
848 :
849 1189 : stream.skip_white_spaces();
850 1189 : return key;
851 : }
852 :
853 70 : auto deserialize_string_or_int(auto &stream, size_t min_size, size_t max_size, char *buffer)
854 : -> std::variant<std::string_view, int32_t>
855 : {
856 70 : stream.skip_white_spaces();
857 :
858 70 : if (stream.current_char() == '"')
859 67 : return deserialize_string_to_buffer(stream, min_size, max_size, buffer);
860 :
861 3 : return deserialize_int(stream);
862 : }
863 :
864 3 : auto deserialize_int(auto &stream) -> int32_t
865 : {
866 : int32_t result;
867 3 : deserialize<field_attributes{}>(stream, result);
868 2 : return result;
869 : }
870 :
871 29 : void skip_value(auto &stream)
872 : {
873 29 : ignore_value(stream);
874 18 : }
875 :
876 : } // namespace spb::json::detail
|