3#include <nlohmann/json.hpp>
13#include <system_error>
21namespace TgBot::Json {
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()) {
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>();
37 throw std::invalid_argument(
"Telegram value is not a signed 64-bit integer");
41void decode(
const nlohmann::json& json, T& value);
44void decode(
const nlohmann::json& json, std::shared_ptr<T>& value);
47void decode(
const nlohmann::json& json, std::optional<T>& value);
50void decode(
const nlohmann::json& json, std::vector<T>& value);
52template<std::size_t Index = 0,
typename... T>
53void decode(
const nlohmann::json& json, std::variant<T...>& value);
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;
62 if constexpr (
requires { Element::STATUS; }) {
63 return json.is_object() && json.value(
"status", std::string { }) == Element::STATUS;
65 if constexpr (
requires { Element::SOURCE; }) {
66 return json.is_object() && json.value(
"source", std::string { }) == Element::SOURCE;
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");
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");
93void decode(
const nlohmann::json& json, std::shared_ptr<T>& value) {
95 throw std::invalid_argument(
"Required Telegram object is null");
97 value = std::make_shared<T>();
102void decode(
const nlohmann::json& json, std::optional<T>& value) {
103 if (json.is_null()) {
108 decode(json, *value);
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");
117 value.reserve(json.size());
118 for (
const auto& item : json) {
120 decode(item, decoded);
121 value.push_back(std::move(decoded));
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");
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");
135 Candidate candidate { };
136 decode(json, candidate);
137 value = std::move(candidate);
138 }
catch (
const std::exception&) {
139 decode<Index + 1>(json, value);
145nlohmann::json encode(
const T& value);
148nlohmann::json encode(
const std::shared_ptr<T>& value);
151nlohmann::json encode(
const std::optional<T>& value);
154nlohmann::json encode(
const std::vector<T>& value);
156template<
typename... T>
157nlohmann::json encode(
const std::variant<T...>& value);
160nlohmann::json encode(
const T& value) {
161 return nlohmann::json(value);
165nlohmann::json encode(
const std::shared_ptr<T>& value) {
176nlohmann::json encode(
const std::optional<T>& value) {
177 return value ? encode(*value) : nlohmann::json(nullptr);
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));
190template<
typename... T>
191nlohmann::json encode(
const std::variant<T...>& value) {
193 [](
const auto& item) {
200void readRequiredField(
const nlohmann::json& json,
const char* name, T& value) {
201 decode(json.at(name), value);
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()) {
211 decode(*item, value);
215void writeRequiredField(nlohmann::json& json,
const char* name,
const T& value) {
216 json[name] = encode(value);
220void writeOptionalField(nlohmann::json& json,
const char* name,
const std::optional<T>& value) {
222 json[name] = encode(value);
227void writeOptionalField(nlohmann::json& json,
const char* name,
const std::shared_ptr<T>& value) {
229 json[name] = encode(value);
TGBOT_API void to_json(nlohmann::json &json, const AcceptedGiftTypes &value)
TGBOT_API void from_json(const nlohmann::json &json, AcceptedGiftTypes &value)