yatbcpp  0.0.1
Yet another Telegram Bot CPP Library
User_fromJson.cc
Go to the documentation of this file.
1 // From JSON Specialication //
4 #include <jsoncpp/json/json.h>
5 #include "types/telegram_type.h"
7 #include "types/User.h"
8 
9 using namespace std;
10 
11 namespace yatbcpp {
17  template<> User fromJson<User>(Json::Value Data) {
18 //User fromJson(Json::Value Data) {
19  //Checking for essential fields, method somehow? check <"first_name">
20  if (!Data.isMember("id")) {
21  throw essential_key_missing("User::id is missing");
22  }
23  if (!Data.isMember("is_bot")) {
24  throw essential_key_missing("User::is_bot is missing");
25  }
26  if (!Data.isMember("first_name")) {
27  throw essential_key_missing("User::first_name is missing");
28  }
29  //Extracting the essential information
30  std::int32_t id = Data["id"].asInt();
31  bool is_bot = Data["is_bot"].asBool();
32  std::string first_name = Data["first_name"].asString();
33  //Creating the for return created object
34  User ret(id, is_bot, first_name);
35  //Adding Optional/Additional Information
36  if (Data.isMember("last_name")) {
37  ret.setLast_name(Data["last_name"].asString());
38  }
39  if (Data.isMember("username")) {
40  ret.setUsername(Data["username"].asString());
41  }
42  if (Data.isMember("language_code")) {
43  ret.setLanguage_code(Data["language_code"].asString());
44  }
45  return ret;
46  }
47 }
void setLanguage_code(const std::optional< std::string > &language_code)
Definition: User.cc:37
Definition: Bot.h:27
User fromJson< User >(Json::Value Data)
void setUsername(const std::optional< std::string > &username)
Definition: User.cc:33
void setLast_name(const std::optional< std::string > &last_name)
Definition: User.cc:29