tgbot-cpp
Loading...
Searching...
No Matches
Json.h
Go to the documentation of this file.
1#pragma once
2
3#include <nlohmann/json.hpp>
4
5#include <charconv>
6#include <cstddef>
7#include <cstdint>
8#include <limits>
9#include <memory>
10#include <optional>
11#include <stdexcept>
12#include <string>
13#include <system_error>
14#include <type_traits>
15#include <utility>
16#include <variant>
17#include <vector>
18
20
21namespace TgBot::Json {
22
23// Some Telegram integer fields, such as draft_id, can arrive as decimal strings.
24inline void decodeInt64(const nlohmann::json& json, std::int64_t& value) {
25 if (json.is_string()) {
26 const auto& text = json.get_ref<const std::string&>();
27 const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), value);
28 if (error == std::errc { } && end == text.data() + text.size()) {
29 return;
30 }
31 } else if (json.is_number_integer()) {
32 if (!json.is_number_unsigned() || json.get<std::uint64_t>() <= std::numeric_limits<std::int64_t>::max()) {
33 value = json.get<std::int64_t>();
34 return;
35 }
36 }
37 throw std::invalid_argument("Telegram value is not a signed 64-bit integer");
38}
39
40template<typename T>
41void decode(const nlohmann::json& json, T& value);
42
43template<typename T>
44void decode(const nlohmann::json& json, std::shared_ptr<T>& value);
45
46template<typename T>
47void decode(const nlohmann::json& json, std::optional<T>& value);
48
49template<typename T>
50void decode(const nlohmann::json& json, std::vector<T>& value);
51
52template<std::size_t Index = 0, typename... T>
53void decode(const nlohmann::json& json, std::variant<T...>& value);
54
55template<typename T>
56bool matchesDiscriminator(const nlohmann::json& json) {
57 if constexpr (requires { typename T::element_type; }) {
58 using Element = typename T::element_type;
59 if constexpr (requires { Element::TYPE; }) {
60 return json.is_object() && json.value("type", std::string { }) == Element::TYPE;
61 }
62 if constexpr (requires { Element::STATUS; }) {
63 return json.is_object() && json.value("status", std::string { }) == Element::STATUS;
64 }
65 if constexpr (requires { Element::SOURCE; }) {
66 return json.is_object() && json.value("source", std::string { }) == Element::SOURCE;
67 }
68 }
69
70 return true;
71}
72
73template<typename T>
74void decode(const nlohmann::json& json, T& value) {
75 if constexpr (std::is_unsigned_v<T> && !std::is_same_v<T, bool>) {
76 if (!json.is_number_integer() || (!json.is_number_unsigned() && json.get<std::int64_t>() < 0)
77 || json.get<std::uint64_t>() > std::numeric_limits<T>::max()) {
78 throw std::invalid_argument("Telegram unsigned integer is out of range or is not an integer");
79 }
80 } else if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
81 if (!json.is_number_integer()
82 || (json.is_number_unsigned()
83 ? json.get<std::uint64_t>() > static_cast<std::uint64_t>(std::numeric_limits<T>::max())
84 : json.get<std::int64_t>() < std::numeric_limits<T>::min()
85 || json.get<std::int64_t>() > std::numeric_limits<T>::max())) {
86 throw std::invalid_argument("Telegram signed integer is out of range or is not an integer");
87 }
88 }
89 json.get_to(value);
90}
91
92template<typename T>
93void decode(const nlohmann::json& json, std::shared_ptr<T>& value) {
94 if (json.is_null()) {
95 throw std::invalid_argument("Required Telegram object is null");
96 }
97 value = std::make_shared<T>();
98 from_json(json, *value);
99}
100
101template<typename T>
102void decode(const nlohmann::json& json, std::optional<T>& value) {
103 if (json.is_null()) {
104 value.reset();
105 return;
106 }
107 value.emplace();
108 decode(json, *value);
109}
110
111template<typename T>
112void decode(const nlohmann::json& json, std::vector<T>& value) {
113 if (!json.is_array()) {
114 throw std::invalid_argument("Telegram array field is not an array");
115 }
116 value.clear();
117 value.reserve(json.size());
118 for (const auto& item : json) {
119 T decoded { };
120 decode(item, decoded);
121 value.push_back(std::move(decoded));
122 }
123}
124
125template<std::size_t Index, typename... T>
126void decode(const nlohmann::json& json, std::variant<T...>& value) {
127 if constexpr (Index == sizeof...(T)) {
128 throw std::invalid_argument("JSON value does not match Telegram union");
129 } else {
130 try {
131 using Candidate = std::variant_alternative_t<Index, std::variant<T...>>;
132 if (!matchesDiscriminator<Candidate>(json)) {
133 throw std::invalid_argument("Telegram union discriminator does not match");
134 }
135 Candidate candidate { };
136 decode(json, candidate);
137 value = std::move(candidate);
138 } catch (const std::exception&) {
139 decode<Index + 1>(json, value);
140 }
141 }
142}
143
144template<typename T>
145nlohmann::json encode(const T& value);
146
147template<typename T>
148nlohmann::json encode(const std::shared_ptr<T>& value);
149
150template<typename T>
151nlohmann::json encode(const std::optional<T>& value);
152
153template<typename T>
154nlohmann::json encode(const std::vector<T>& value);
155
156template<typename... T>
157nlohmann::json encode(const std::variant<T...>& value);
158
159template<typename T>
160nlohmann::json encode(const T& value) {
161 return nlohmann::json(value);
162}
163
164template<typename T>
165nlohmann::json encode(const std::shared_ptr<T>& value) {
166 if (!value) {
167 return nullptr;
168 }
169 nlohmann::json json;
170 to_json(json, *value);
171
172 return json;
173}
174
175template<typename T>
176nlohmann::json encode(const std::optional<T>& value) {
177 return value ? encode(*value) : nlohmann::json(nullptr);
178}
179
180template<typename T>
181nlohmann::json encode(const std::vector<T>& value) {
182 nlohmann::json json = nlohmann::json::array();
183 for (const auto& item : value) {
184 json.push_back(encode(item));
185 }
186
187 return json;
188}
189
190template<typename... T>
191nlohmann::json encode(const std::variant<T...>& value) {
192 return std::visit(
193 [](const auto& item) {
194 return encode(item);
195 },
196 value);
197}
198
199template<typename T>
200void readRequiredField(const nlohmann::json& json, const char* name, T& value) {
201 decode(json.at(name), value);
202}
203
204template<typename T>
205void readOptionalField(const nlohmann::json& json, const char* name, T& value) {
206 const auto item = json.find(name);
207 if (item == json.end() || item->is_null()) {
208 value = T { };
209 return;
210 }
211 decode(*item, value);
212}
213
214template<typename T>
215void writeRequiredField(nlohmann::json& json, const char* name, const T& value) {
216 json[name] = encode(value);
217}
218
219template<typename T>
220void writeOptionalField(nlohmann::json& json, const char* name, const std::optional<T>& value) {
221 if (value) {
222 json[name] = encode(value);
223 }
224}
225
226template<typename T>
227void writeOptionalField(nlohmann::json& json, const char* name, const std::shared_ptr<T>& value) {
228 if (value) {
229 json[name] = encode(value);
230 }
231}
232
233} // namespace TgBot::Json
234
TGBOT_API void to_json(nlohmann::json &json, const AcceptedGiftTypes &value)
TGBOT_API void from_json(const nlohmann::json &json, AcceptedGiftTypes &value)