/* * Copyright (c) 2015 Oleg Morozenkov * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software", "to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include "tgbot/TgTypeParser.h" using namespace std; using namespace boost::property_tree; namespace TgBot { TgTypeParser& TgTypeParser::getInstance() { static TgTypeParser result; return result; } Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { Chat::Ptr result(new Chat); result->id = data.get("id"); string type = data.get("type"); if (type == "private") { result->type = Chat::Type::Private; } else if (type == "group") { result->type = Chat::Type::Group; } else if (type == "supergroup") { result->type = Chat::Type::Supergroup; } else if (type == "channel") { result->type = Chat::Type::Channel; } result->title = data.get("title", ""); result->username = data.get("username", ""); result->firstName = data.get("first_name", ""); result->lastName = data.get("last_name", ""); return result; } string TgTypeParser::parseChat(const Chat::Ptr& object) const { if (!object) { return ""; } string result; result += '{'; appendToJson(result, "id", object->id); if (object->type == Chat::Type::Private) { appendToJson(result, "type", "private"); } else if (object->type == Chat::Type::Group) { appendToJson(result, "type", "group"); } else if (object->type == Chat::Type::Supergroup) { appendToJson(result, "type", "supergroup"); } else if (object->type == Chat::Type::Channel) { appendToJson(result, "type", "channel"); } appendToJson(result, "title", object->title); appendToJson(result, "username", object->username); appendToJson(result, "first_name", object->firstName); appendToJson(result, "last_name", object->lastName); result.erase(result.length() - 1); result += '}'; return result; } User::Ptr TgTypeParser::parseJsonAndGetUser(const ptree& data) const { User::Ptr result(new User); result->id = data.get("id"); result->firstName = data.get("first_name"); result->lastName = data.get("last_name", ""); result->username = data.get("username", ""); return result; } string TgTypeParser::parseUser(const User::Ptr& object) const { if (!object) { return ""; } string result; result += '{'; appendToJson(result, "id", object->id); appendToJson(result, "first_name", object->firstName); appendToJson(result, "last_name", object->lastName); appendToJson(result, "username", object->username); result.erase(result.length() - 1); result += '}'; return result; } Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { Message::Ptr result(new Message); result->messageId = data.get("message_id"); result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); result->date = data.get("date"); result->chat = parseJsonAndGetChat(data.find("chat")->second); result->forwardFrom = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); result->forwardDate = data.get("forward_date", 0); result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); result->text = data.get("text", ""); result->audio = tryParseJson