/src/jsoncons/include/jsoncons/ser_common.hpp
Line | Count | Source |
1 | | /// Copyright 2013-2026 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_SER_COMMON_HPP |
8 | | #define JSONCONS_SER_COMMON_HPP |
9 | | |
10 | | #include <cstddef> |
11 | | #include <string> |
12 | | #include <system_error> |
13 | | |
14 | | #include <jsoncons/config/jsoncons_config.hpp> |
15 | | |
16 | | namespace jsoncons { |
17 | | |
18 | | class ser_context |
19 | | { |
20 | | public: |
21 | 4.97k | virtual ~ser_context() = default; |
22 | | |
23 | | virtual size_t line() const |
24 | 0 | { |
25 | 0 | return 0; |
26 | 0 | } |
27 | | |
28 | | virtual size_t column() const |
29 | 0 | { |
30 | 0 | return 0; |
31 | 0 | } |
32 | | |
33 | | virtual size_t position() const |
34 | 0 | { |
35 | 0 | return 0; |
36 | 0 | } |
37 | | |
38 | | virtual size_t begin_position() const |
39 | 0 | { |
40 | 0 | return 0; |
41 | 0 | } |
42 | | |
43 | | virtual size_t end_position() const |
44 | 0 | { |
45 | 0 | return 0; |
46 | 0 | } |
47 | | }; |
48 | | |
49 | | class read_error |
50 | | { |
51 | | std::error_code ec_{}; |
52 | | std::string message_arg_; |
53 | | std::size_t line_{0}; |
54 | | std::size_t column_{0}; |
55 | | |
56 | | public: |
57 | | read_error(std::error_code ec) |
58 | | : ec_{ec} |
59 | 0 | { |
60 | 0 | } |
61 | | |
62 | | read_error(std::error_code ec, const std::string& message_arg) |
63 | | : ec_{ec}, message_arg_(message_arg) |
64 | 0 | { |
65 | 0 | } |
66 | | |
67 | | read_error(std::error_code ec, std::size_t line, std::size_t column) |
68 | 4.96k | : ec_{ec}, line_{line}, column_{column} |
69 | 4.96k | { |
70 | 4.96k | } |
71 | | |
72 | | read_error(std::error_code ec, const std::string& message_arg, std::size_t line, std::size_t column) |
73 | | : ec_{ec}, message_arg_(message_arg), line_{line}, column_{column} |
74 | 0 | { |
75 | 0 | } |
76 | | |
77 | | read_error(const read_error& other) = default; |
78 | | |
79 | | read_error(read_error&& other) = default; |
80 | | |
81 | | read_error& operator=(const read_error& other) = default; |
82 | | |
83 | | read_error& operator=(read_error&& other) = default; |
84 | | |
85 | | const std::error_code& code() const noexcept |
86 | 4.96k | { |
87 | 4.96k | return ec_; |
88 | 4.96k | } |
89 | | const std::string& msg_arg() const noexcept |
90 | 0 | { |
91 | 0 | return message_arg_; |
92 | 0 | } |
93 | | std::size_t line() const noexcept |
94 | 4.96k | { |
95 | 4.96k | return line_; |
96 | 4.96k | } |
97 | | std::size_t column() const noexcept |
98 | 4.96k | { |
99 | 4.96k | return column_; |
100 | 4.96k | } |
101 | | |
102 | | std::string message() const |
103 | 0 | { |
104 | 0 | std::string str(message_arg_); |
105 | 0 | if (!str.empty()) |
106 | 0 | { |
107 | 0 | str.append(": "); |
108 | 0 | } |
109 | 0 | str.append(ec_.message()); |
110 | 0 | if (line_ != 0) |
111 | 0 | { |
112 | 0 | str.append(" at line "); |
113 | 0 | str.append(std::to_string(line_)); |
114 | 0 | } |
115 | 0 | if (line_ != 0 && column_ != 0) |
116 | 0 | { |
117 | 0 | str.append(" and column "); |
118 | 0 | str.append(std::to_string(column_)); |
119 | 0 | } |
120 | 0 | else if (column_ != 0) |
121 | 0 | { |
122 | 0 | str.append(" at position "); |
123 | 0 | str.append(std::to_string(column_)); |
124 | 0 | } |
125 | 0 | return str; |
126 | 0 | } |
127 | | }; |
128 | | |
129 | | template <typename T> |
130 | | using read_result = expected<T,read_error>; |
131 | | |
132 | | using write_result = jsoncons::expected<void, std::error_code>; |
133 | | |
134 | | } // namespace jsoncons |
135 | | |
136 | | #endif // JSONCONS_SER_COMMON_HPP |