tgbotxx
1.1.9.2
Telegram Bot C++ Library
Loading...
Searching...
No Matches
Object.hpp
Go to the documentation of this file.
1
#pragma once
3
#include <type_traits>
4
#include <cstdint>
5
#include <ctime>
6
#include <iostream>
7
#include <memory>
8
#include <nlohmann/json.hpp>
9
#include <sstream>
10
#include <string>
11
#include <
tgbotxx/Exception.hpp
>
12
#include <
tgbotxx/utils/Ptr.hpp
>
13
#include <vector>
14
namespace
nl = nlohmann;
15
17
19
#define OBJECT_SERIALIZE_FIELD(json, json_field, field) \
20
json[json_field] = field;
21
22
#define OBJECT_SERIALIZE_FIELD_PTR(json, json_field, field) \
23
if (field) { \
24
json[json_field] = (field)->toJson(); \
25
}
26
27
#define OBJECT_SERIALIZE_FIELD_PTR_ARRAY(json, json_field, array_field) \
28
json[json_field] = nl::json::array(); \
29
for (const auto& e: array_field) \
30
json[json_field].push_back(e->toJson());
31
32
#define OBJECT_SERIALIZE_FIELD_PTR_ARRAY_ARRAY(json, json_field, array_array_field) \
33
json[json_field] = nl::json::array(); \
34
for (const auto& array: array_array_field) { \
35
nl::json arr = nl::json::array(); \
36
for (const auto& e: array) { \
37
arr.push_back(e->toJson()); \
38
} \
39
json[json_field].push_back(arr); \
40
}
41
42
#define OBJECT_SERIALIZE_FIELD_ENUM(json, enum_name, json_field, field) \
43
json[json_field] = enum_name##ToString(field)
44
46
#define OBJECT_DESERIALIZE_FIELD(json, json_field, field, default_value, optional) \
47
if (json.contains(json_field)) { \
48
try { \
49
using T = std::remove_reference_t<std::remove_const_t<decltype(field)>>; \
50
field = json[json_field].get<T>(); \
51
} catch (const std::exception& e) { \
52
std::ostringstream err{}; \
53
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Failed to deserialize \"" \
54
<< json_field << "\" from json object: " << json.dump(2) << "\nReason: " << e.what(); \
55
throw Exception(err.str()); \
56
} catch (...) { \
57
std::ostringstream err{}; \
58
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Failed to deserialize \"" \
59
<< json_field << "\" from json object: " << json.dump(2); \
60
throw Exception(err.str()); \
61
} \
62
} else { \
63
if (not(optional)) { \
64
std::ostringstream err{}; \
65
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
66
<< json_field << "\" from json object: " << json.dump(2); \
67
throw Exception(err.str()); \
68
} \
69
field = default_value; \
70
}
71
72
#define OBJECT_DESERIALIZE_FIELD_PTR(json, json_field, field, optional) \
73
static_assert(!std::is_const_v<decltype(field)>, "OBJECT_DESERIALIZE_FIELD_PTR: 'field' must not be const"); \
74
static_assert(std::is_same_v<decltype(optional), bool>, "OBJECT_DESERIALIZE_FIELD_PTR: 'optional' must be boolean"); \
75
if (json.contains(json_field) and json[json_field].is_object() and not json[json_field].empty()) { \
76
using T = std::remove_reference_t<decltype(field)>; \
77
using E = T::element_type; \
78
field.reset(new (E)(json[json_field])); \
79
} else { \
80
if (not(optional)) { \
81
std::ostringstream err{}; \
82
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
83
<< json_field << "\" from json object: " << json.dump(2); \
84
throw Exception(err.str()); \
85
} \
86
field = nullptr; \
87
}
88
89
#define OBJECT_DESERIALIZE_FIELD_PTR_ARRAY(json, json_field, array_field, optional) \
90
static_assert(!std::is_const_v<decltype(array_field)>, "OBJECT_DESERIALIZE_FIELD_PTR_ARRAY: 'field' must not be const"); \
91
static_assert(std::is_same_v<decltype(optional), bool>, "OBJECT_DESERIALIZE_FIELD_PTR_ARRAY: 'optional' must be boolean"); \
92
if ((json.contains(json_field)) and (json[json_field].is_array())) { \
93
using T = std::remove_reference_t<std::remove_const_t<decltype(array_field)::value_type>>; \
94
using E = T::element_type; \
95
array_field.reserve(json[json_field].size()); \
96
for (const nl::json& obj: json[json_field]) { \
97
array_field.emplace_back(new E(obj)); \
98
} \
99
} else { \
100
if (not(optional)) { \
101
std::ostringstream err{}; \
102
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
103
<< json_field << "\" from json object: " << json.dump(2); \
104
throw Exception(err.str()); \
105
} \
106
array_field.clear(); \
107
}
108
109
110
#define OBJECT_DESERIALIZE_FIELD_PTR_ARRAY_ARRAY(json, json_field, array_array_field, optional) \
111
static_assert(!std::is_const_v<decltype(array_array_field)>, "OBJECT_DESERIALIZE_FIELD_PTR_ARRAY_ARRAY: 'field' must not be const"); \
112
static_assert(std::is_same_v<decltype(optional), bool>, "OBJECT_DESERIALIZE_FIELD_PTR_ARRAY_ARRAY: 'optional' must be boolean"); \
113
if ((json.contains(json_field)) and (json[json_field].is_array())) { \
114
using ArrayArray = decltype(array_array_field); \
115
using Array = ArrayArray::value_type;
/* e.g vector<vector<int> <- > */
\
116
using T = ArrayArray::value_type::value_type;
/* e.g vector<vector<int>> <- */
\
117
using E = T::element_type; \
118
array_array_field.reserve(json[json_field].size()); \
119
for (const nl::json& array: json[json_field]) { \
120
Array arr; \
121
arr.reserve(array.size()); \
122
for (const nl::json& obj: array) { \
123
arr.emplace_back(new E(obj)); \
124
} \
125
array_array_field.push_back(std::move(arr)); \
126
} \
127
} else { \
128
if (not(optional)) { \
129
std::ostringstream err{}; \
130
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
131
<< json_field << "\" from json object: " << json.dump(2); \
132
throw Exception(err.str()); \
133
} \
134
array_array_field.clear(); \
135
}
136
137
#define OBJECT_DESERIALIZE_FIELD_ENUM(json, enum_name, json_field, field, default_value, optional) \
138
static_assert(std::is_enum_v<decltype(field)>, "OBJECT_DESERIALIZE_FIELD_ENUM: 'field' must be an enum"); \
139
static_assert(!std::is_const_v<decltype(field)>, "OBJECT_DESERIALIZE_FIELD_ENUM: 'field' must not be const"); \
140
if (json.contains(json_field)) { \
141
try { \
142
if (auto opt = StringTo##enum_name(json[json_field])) \
143
field = *opt; \
144
else \
145
throw Exception("Could not convert string \"" + json[json_field].get<std::string>() + "\" to enum"); \
146
} catch (const std::exception& e) { \
147
std::ostringstream err{}; \
148
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Failed to deserialize \"" \
149
<< json_field << "\" from json object: " << json.dump(2) << "\nReason: " << e.what(); \
150
throw Exception(err.str()); \
151
} catch (...) { \
152
std::ostringstream err{}; \
153
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Failed to deserialize \"" \
154
<< json_field << "\" from json object: " << json.dump(2); \
155
throw Exception(err.str()); \
156
} \
157
} else { \
158
if (not(optional)) { \
159
std::ostringstream err{}; \
160
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
161
<< json_field << "\" from json object: " << json.dump(2); \
162
throw Exception(err.str()); \
163
} \
164
field = static_cast<enum_name>(default_value); \
165
}
Exception.hpp
Ptr.hpp
include
tgbotxx
objects
Object.hpp
Generated on Sat Oct 25 2025 10:01:53 for tgbotxx by
1.9.8