/src/jsoncons/include/jsoncons_ext/msgpack/msgpack_reader.hpp
Line | Count | Source |
1 | | // Copyright 2013-2025 Daniel Parker |
2 | | // Distributed under the Boost license, Version 1.0. |
3 | | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
4 | | |
5 | | // See https://github.com/danielaparker/jsoncons for latest version |
6 | | |
7 | | #ifndef JSONCONS_EXT_MSGPACK_MSGPACK_READER_HPP |
8 | | #define JSONCONS_EXT_MSGPACK_MSGPACK_READER_HPP |
9 | | |
10 | | #include <cstddef> |
11 | | #include <memory> |
12 | | #include <system_error> |
13 | | #include <utility> // std::move |
14 | | |
15 | | #include <jsoncons/config/compiler_support.hpp> |
16 | | #include <jsoncons/item_event_visitor.hpp> |
17 | | #include <jsoncons/json_exception.hpp> |
18 | | #include <jsoncons/json_visitor.hpp> |
19 | | #include <jsoncons/source.hpp> |
20 | | |
21 | | #include <jsoncons_ext/msgpack/msgpack_parser.hpp> |
22 | | |
23 | | namespace jsoncons { |
24 | | namespace msgpack { |
25 | | |
26 | | template <typename Source,typename Allocator=std::allocator<char>> |
27 | | class basic_msgpack_reader |
28 | | { |
29 | | using char_type = char; |
30 | | |
31 | | basic_msgpack_parser<Source,Allocator> parser_; |
32 | | basic_item_event_visitor_to_json_visitor<char_type,Allocator> adaptor_; |
33 | | item_event_visitor& visitor_; |
34 | | public: |
35 | | template <typename Sourceable> |
36 | | basic_msgpack_reader(Sourceable&& source, |
37 | | json_visitor& visitor, |
38 | | const Allocator& alloc) |
39 | | : basic_msgpack_reader(std::forward<Sourceable>(source), |
40 | | visitor, |
41 | | msgpack_decode_options(), |
42 | | alloc) |
43 | | { |
44 | | } |
45 | | |
46 | | template <typename Sourceable> |
47 | | basic_msgpack_reader(Sourceable&& source, |
48 | | json_visitor& visitor, |
49 | | const msgpack_decode_options& options = msgpack_decode_options(), |
50 | | const Allocator& alloc=Allocator()) |
51 | 17.4k | : parser_(std::forward<Sourceable>(source), options, alloc), |
52 | 17.4k | adaptor_(visitor, alloc), visitor_(adaptor_) |
53 | 17.4k | { |
54 | 17.4k | } jsoncons::msgpack::basic_msgpack_reader<jsoncons::stream_source<unsigned char, std::__1::allocator<unsigned char> >, std::__1::allocator<char> >::basic_msgpack_reader<std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>(std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, jsoncons::basic_json_visitor<char>&, jsoncons::msgpack::msgpack_decode_options const&, std::__1::allocator<char> const&) Line | Count | Source | 51 | 10.7k | : parser_(std::forward<Sourceable>(source), options, alloc), | 52 | 10.7k | adaptor_(visitor, alloc), visitor_(adaptor_) | 53 | 10.7k | { | 54 | 10.7k | } |
jsoncons::msgpack::basic_msgpack_reader<jsoncons::stream_source<unsigned char, std::__1::allocator<unsigned char> >, std::__1::allocator<char> >::basic_msgpack_reader<std::__1::basic_istream<char, std::__1::char_traits<char> >&>(std::__1::basic_istream<char, std::__1::char_traits<char> >&, jsoncons::basic_json_visitor<char>&, jsoncons::msgpack::msgpack_decode_options const&, std::__1::allocator<char> const&) Line | Count | Source | 51 | 6.74k | : parser_(std::forward<Sourceable>(source), options, alloc), | 52 | 6.74k | adaptor_(visitor, alloc), visitor_(adaptor_) | 53 | 6.74k | { | 54 | 6.74k | } |
|
55 | | template <typename Sourceable> |
56 | | basic_msgpack_reader(Sourceable&& source, |
57 | | item_event_visitor& visitor, |
58 | | const Allocator& alloc) |
59 | | : basic_msgpack_reader(std::forward<Sourceable>(source), |
60 | | visitor, |
61 | | msgpack_decode_options(), |
62 | | alloc) |
63 | | { |
64 | | } |
65 | | |
66 | | template <typename Sourceable> |
67 | | basic_msgpack_reader(Sourceable&& source, |
68 | | item_event_visitor& visitor, |
69 | | const msgpack_decode_options& options = msgpack_decode_options(), |
70 | | const Allocator& alloc=Allocator()) |
71 | | : parser_(std::forward<Sourceable>(source), options, alloc), |
72 | | visitor_(visitor) |
73 | | { |
74 | | } |
75 | | |
76 | | void read() |
77 | | { |
78 | | std::error_code ec; |
79 | | read(ec); |
80 | | if (JSONCONS_UNLIKELY(ec)) |
81 | | { |
82 | | JSONCONS_THROW(ser_error(ec,line(),column())); |
83 | | } |
84 | | } |
85 | | |
86 | | void read(std::error_code& ec) |
87 | 17.4k | { |
88 | 17.4k | parser_.reset(); |
89 | 17.4k | parser_.parse(visitor_, ec); |
90 | 17.4k | if (JSONCONS_UNLIKELY(ec)) |
91 | 12.7k | { |
92 | 12.7k | return; |
93 | 12.7k | } |
94 | 17.4k | } |
95 | | |
96 | | std::size_t line() const |
97 | 4.34k | { |
98 | 4.34k | return parser_.line(); |
99 | 4.34k | } |
100 | | |
101 | | std::size_t column() const |
102 | 4.34k | { |
103 | 4.34k | return parser_.column(); |
104 | 4.34k | } |
105 | | }; |
106 | | |
107 | | using msgpack_stream_reader = basic_msgpack_reader<jsoncons::binary_stream_source>; |
108 | | |
109 | | using msgpack_bytes_reader = basic_msgpack_reader<jsoncons::bytes_source>; |
110 | | |
111 | | } // namespace msgpack |
112 | | } // namespace jsoncons |
113 | | |
114 | | #endif // JSONCONS_EXT_MSGPACK_MSGPACK_READER_HPP |