tgbotxx  1.1.6.9
Telegram Bot C++ Library
Object.hpp File Reference
#include <type_traits>
#include <cstdint>
#include <ctime>
#include <iostream>
#include <memory>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
#include <tgbotxx/Exception.hpp>
#include <tgbotxx/utils/Ptr.hpp>
#include <vector>

Go to the source code of this file.

Macros

#define OBJECT_SERIALIZE_FIELD(json, json_field, field)    json[json_field] = field;
 Available objects: https://core.telegram.org/bots/api#available-types. More...
 
#define OBJECT_SERIALIZE_FIELD_PTR(json, json_field, field)
 
#define OBJECT_SERIALIZE_FIELD_PTR_ARRAY(json, json_field, array_field)
 
#define OBJECT_SERIALIZE_FIELD_PTR_ARRAY_ARRAY(json, json_field, array_array_field)
 
#define OBJECT_DESERIALIZE_FIELD(json, json_field, field, default_value, optional)
 Deserialize. More...
 
#define OBJECT_DESERIALIZE_FIELD_PTR(json, json_field, field, optional)
 
#define OBJECT_DESERIALIZE_FIELD_PTR_ARRAY(json, json_field, array_field, optional)
 
#define OBJECT_DESERIALIZE_FIELD_PTR_ARRAY_ARRAY(json, json_field, array_array_field, optional)
 

Macro Definition Documentation

◆ OBJECT_DESERIALIZE_FIELD

#define OBJECT_DESERIALIZE_FIELD (   json,
  json_field,
  field,
  default_value,
  optional 
)
Value:
if (json.contains(json_field)) { \
try { \
using T = std::remove_reference_t<std::remove_const_t<decltype(field)>>; \
field = json[json_field].get<T>(); \
} catch (const std::exception& e) { \
std::ostringstream err{}; \
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Failed to deserialize \"" \
<< json_field << "\" from json object: " << json.dump(2) << "\nReason: " << e.what(); \
throw Exception(err.str()); \
} catch (...) { \
std::ostringstream err{}; \
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Failed to deserialize \"" \
<< json_field << "\" from json object: " << json.dump(2); \
throw Exception(err.str()); \
} \
} else { \
if (not(optional)) { \
std::ostringstream err{}; \
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
<< json_field << "\" from json object: " << json.dump(2); \
throw Exception(err.str()); \
} \
field = default_value; \
}

Deserialize.

Definition at line 44 of file Object.hpp.

◆ OBJECT_DESERIALIZE_FIELD_PTR

#define OBJECT_DESERIALIZE_FIELD_PTR (   json,
  json_field,
  field,
  optional 
)
Value:
static_assert(!std::is_const_v<decltype(field)>, "OBJECT_DESERIALIZE_FIELD_PTR: 'field' must not be const"); \
static_assert(std::is_same_v<decltype(optional), bool>, "OBJECT_DESERIALIZE_FIELD_PTR: 'optional' must be boolean"); \
if (json.contains(json_field) and json[json_field].is_object() and not json[json_field].empty()) { \
using T = std::remove_reference_t<decltype(field)>; \
using E = T::element_type; \
field.reset(new (E)(json[json_field])); \
} else { \
if (not(optional)) { \
std::ostringstream err{}; \
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
<< json_field << "\" from json object: " << json.dump(2); \
throw Exception(err.str()); \
} \
field = nullptr; \
}

Definition at line 70 of file Object.hpp.

◆ OBJECT_DESERIALIZE_FIELD_PTR_ARRAY

#define OBJECT_DESERIALIZE_FIELD_PTR_ARRAY (   json,
  json_field,
  array_field,
  optional 
)
Value:
static_assert(!std::is_const_v<decltype(array_field)>, "OBJECT_DESERIALIZE_FIELD_PTR_ARRAY: 'field' must not be const"); \
static_assert(std::is_same_v<decltype(optional), bool>, "OBJECT_DESERIALIZE_FIELD_PTR_ARRAY: 'optional' must be boolean"); \
if ((json.contains(json_field)) and (json[json_field].is_array())) { \
using T = std::remove_reference_t<std::remove_const_t<decltype(array_field)::value_type>>; \
using E = T::element_type; \
array_field.reserve(json[json_field].size()); \
for (const nl::json& obj: json[json_field]) { \
array_field.emplace_back(new E(obj)); \
} \
} else { \
if (not(optional)) { \
std::ostringstream err{}; \
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
<< json_field << "\" from json object: " << json.dump(2); \
throw Exception(err.str()); \
} \
array_field.clear(); \
}

Definition at line 87 of file Object.hpp.

◆ OBJECT_DESERIALIZE_FIELD_PTR_ARRAY_ARRAY

#define OBJECT_DESERIALIZE_FIELD_PTR_ARRAY_ARRAY (   json,
  json_field,
  array_array_field,
  optional 
)
Value:
static_assert(!std::is_const_v<decltype(array_array_field)>, "OBJECT_DESERIALIZE_FIELD_PTR_ARRAY_ARRAY: 'field' must not be const"); \
static_assert(std::is_same_v<decltype(optional), bool>, "OBJECT_DESERIALIZE_FIELD_PTR_ARRAY_ARRAY: 'optional' must be boolean"); \
if ((json.contains(json_field)) and (json[json_field].is_array())) { \
using ArrayArray = decltype(array_array_field); \
using Array = ArrayArray::value_type; /* e.g vector<vector<int> <- > */ \
using T = ArrayArray::value_type::value_type; /* e.g vector<vector<int>> <- */ \
using E = T::element_type; \
array_array_field.reserve(json[json_field].size()); \
for (const nl::json& array: json[json_field]) { \
Array arr; \
arr.reserve(array.size()); \
for (const nl::json& obj: array) { \
arr.emplace_back(new E(obj)); \
} \
array_array_field.push_back(std::move(arr)); \
} \
} else { \
if (not(optional)) { \
std::ostringstream err{}; \
err << __FILE__ << ':' << __LINE__ << ": " << __FUNCTION__ << ": Missing required field \"" \
<< json_field << "\" from json object: " << json.dump(2); \
throw Exception(err.str()); \
} \
array_array_field.clear(); \
}

Definition at line 108 of file Object.hpp.

◆ OBJECT_SERIALIZE_FIELD

#define OBJECT_SERIALIZE_FIELD (   json,
  json_field,
  field 
)     json[json_field] = field;

Available objects: https://core.telegram.org/bots/api#available-types.

Helper macros to de/serialize objects Serialize

Definition at line 19 of file Object.hpp.

◆ OBJECT_SERIALIZE_FIELD_PTR

#define OBJECT_SERIALIZE_FIELD_PTR (   json,
  json_field,
  field 
)
Value:
if (field) { \
json[json_field] = field->toJson(); \
}

Definition at line 22 of file Object.hpp.

◆ OBJECT_SERIALIZE_FIELD_PTR_ARRAY

#define OBJECT_SERIALIZE_FIELD_PTR_ARRAY (   json,
  json_field,
  array_field 
)
Value:
json[json_field] = nl::json::array(); \
for (const auto& e: array_field) \
json[json_field].push_back(e->toJson());

Definition at line 27 of file Object.hpp.

◆ OBJECT_SERIALIZE_FIELD_PTR_ARRAY_ARRAY

#define OBJECT_SERIALIZE_FIELD_PTR_ARRAY_ARRAY (   json,
  json_field,
  array_array_field 
)
Value:
json[json_field] = nl::json::array(); \
for (const auto& array: array_array_field) { \
nl::json arr = nl::json::array(); \
for (const auto& e: array) { \
arr.push_back(e->toJson()); \
} \
json[json_field].push_back(arr); \
}

Definition at line 32 of file Object.hpp.