/src/jsoncons/include/jsoncons_ext/ubjson/ubjson_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_UBJSON_UBJSON_READER_HPP |
8 | | #define JSONCONS_EXT_UBJSON_UBJSON_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/config/jsoncons_config.hpp> |
17 | | #include <jsoncons/json_visitor.hpp> |
18 | | #include <jsoncons/source.hpp> |
19 | | #include <jsoncons_ext/ubjson/ubjson_error.hpp> |
20 | | #include <jsoncons_ext/ubjson/ubjson_parser.hpp> |
21 | | #include <jsoncons_ext/ubjson/ubjson_type.hpp> |
22 | | |
23 | | namespace jsoncons { |
24 | | namespace ubjson { |
25 | | |
26 | | template <typename Source,typename Allocator=std::allocator<char>> |
27 | | class basic_ubjson_reader |
28 | | { |
29 | | basic_ubjson_parser<Source,Allocator> parser_; |
30 | | json_visitor& visitor_; |
31 | | public: |
32 | | template <typename Sourceable> |
33 | | basic_ubjson_reader(Sourceable&& source, |
34 | | json_visitor& visitor, |
35 | | const Allocator& alloc) |
36 | | : basic_ubjson_reader(std::forward<Sourceable>(source), |
37 | | visitor, |
38 | | ubjson_decode_options(), |
39 | | alloc) |
40 | | { |
41 | | } |
42 | | |
43 | | template <typename Sourceable> |
44 | | basic_ubjson_reader(Sourceable&& source, |
45 | | json_visitor& visitor, |
46 | | const ubjson_decode_options& options = ubjson_decode_options(), |
47 | | const Allocator& alloc=Allocator()) |
48 | 744 | : parser_(std::forward<Sourceable>(source), options, alloc), |
49 | 744 | visitor_(visitor) |
50 | 744 | { |
51 | 744 | } jsoncons::ubjson::basic_ubjson_reader<jsoncons::stream_source<unsigned char, std::__1::allocator<unsigned char> >, std::__1::allocator<char> >::basic_ubjson_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::ubjson::ubjson_decode_options const&, std::__1::allocator<char> const&) Line | Count | Source | 48 | 279 | : parser_(std::forward<Sourceable>(source), options, alloc), | 49 | 279 | visitor_(visitor) | 50 | 279 | { | 51 | 279 | } |
jsoncons::ubjson::basic_ubjson_reader<jsoncons::stream_source<unsigned char, std::__1::allocator<unsigned char> >, std::__1::allocator<char> >::basic_ubjson_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::ubjson::ubjson_decode_options const&, std::__1::allocator<char> const&) Line | Count | Source | 48 | 465 | : parser_(std::forward<Sourceable>(source), options, alloc), | 49 | 465 | visitor_(visitor) | 50 | 465 | { | 51 | 465 | } |
|
52 | | |
53 | | void read() |
54 | | { |
55 | | std::error_code ec; |
56 | | read(ec); |
57 | | if (JSONCONS_UNLIKELY(ec)) |
58 | | { |
59 | | JSONCONS_THROW(ser_error(ec,line(),column())); |
60 | | } |
61 | | } |
62 | | |
63 | | void read(std::error_code& ec) |
64 | 744 | { |
65 | 744 | parser_.reset(); |
66 | 744 | parser_.parse(visitor_, ec); |
67 | 744 | if (JSONCONS_UNLIKELY(ec)) |
68 | 603 | { |
69 | 603 | return; |
70 | 603 | } |
71 | 744 | } |
72 | | |
73 | | std::size_t line() const |
74 | 340 | { |
75 | 340 | return parser_.line(); |
76 | 340 | } |
77 | | |
78 | | std::size_t column() const |
79 | 340 | { |
80 | 340 | return parser_.column(); |
81 | 340 | } |
82 | | }; |
83 | | |
84 | | using ubjson_stream_reader = basic_ubjson_reader<jsoncons::binary_stream_source>; |
85 | | |
86 | | using ubjson_bytes_reader = basic_ubjson_reader<jsoncons::bytes_source>; |
87 | | |
88 | | } // namespace ubjson |
89 | | } // namespace jsoncons |
90 | | |
91 | | #endif // JSONCONS_EXT_UBJSON_UBJSON_READER_HPP |