From e05c5ca197d7ccac7ea20db78c1e101090ff629a Mon Sep 17 00:00:00 2001 From: Naveen Date: Mon, 23 Nov 2015 10:08:38 +0000 Subject: Add Chat class --- src/TgTypeParser.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/TgTypeParser.cpp') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 6586ec6..df80ca7 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -32,6 +32,35 @@ TgTypeParser& TgTypeParser::getInstance() { return result; } +Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { + Chat::Ptr result(new Chat); + result->id = data.get("id"); + result->type = data.get("type"); + 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); + appendToJson(result, "type", object->type); + 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"); @@ -81,7 +110,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { result->messageId = data.get("message_id"); result->from = parseJsonAndGetUser(data.find("from")->second); result->date = data.get("date"); - result->chat = parseJsonAndGetGenericChat(data.find("chat")->second); + result->chat = tryParseJson(&TgTypeParser::parseJsonAndGetChat, data, "chat"); result->forwardFrom = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); result->forwardDate = data.get("forward_date", 0); result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); @@ -112,7 +141,7 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const { appendToJson(result, "message_id", object->messageId); appendToJson(result, "from", parseUser(object->from)); appendToJson(result, "date", object->date); - appendToJson(result, "chat", parseGenericChat(object->chat)); + appendToJson(result, "chat", parseChat(object->chat)); appendToJson(result, "forward_from", parseUser(object->forwardFrom)); appendToJson(result, "forward_date", object->forwardDate); appendToJson(result, "reply_to_message", parseMessage(object->replyToMessage)); -- cgit v1.2.3