From 1e43989b77e52df803fa58184bfbd5910dea46d0 Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Fri, 30 Sep 2016 00:46:33 +0300 Subject: [PATCH] JSON-parsing functions moved to namespace telebotxx. --- src/BotApi.cpp | 126 +++++++++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 62 deletions(-) diff --git a/src/BotApi.cpp b/src/BotApi.cpp index dea192d..7522463 100644 --- a/src/BotApi.cpp +++ b/src/BotApi.cpp @@ -13,70 +13,72 @@ #include +namespace telebotxx +{ + const rapidjson::Value& parseResponse(const rapidjson::Document& doc) + { + using namespace rapidjson; + if (!doc.IsObject()) + throw ParseError("Object expected"); + + // Get status + if (!doc.HasMember("ok") || !doc["ok"].IsBool()) + throw ParseError("Field 'ok' not found or has invalid type"); + bool ok = doc["ok"].GetBool(); + + if (ok) + { + if (!doc.HasMember("result") || !doc["result"].IsObject()) + throw ParseError("Field 'result' not found or has invalid type"); + return doc["result"]; + } else + { + if (!doc.HasMember("error_code") || !doc["error_code"].IsInt()) + throw ParseError("Field 'error_code' not found or has invalid type"); + int code = doc["error_code"].GetInt(); + + if (!doc.HasMember("description") || !doc["description"].IsString()) + throw ParseError("Field 'description' not found or has invalid type"); + std::string description(doc["description"].GetString()); + + throw ApiError(code, description); + } + } + + User parseUser(const rapidjson::Value& obj) + { + if (!obj.HasMember("id") || !obj["id"].IsInt()) + throw ParseError("Field 'id' not found or has invalid type"); + int id = obj["id"].GetInt(); + + if (!obj.HasMember("first_name") || !obj["first_name"].IsString()) + throw ParseError("Field 'first_name' not found or has invalid type"); + std::string firstName(obj["first_name"].GetString()); + + std::string lastName; + if (obj.HasMember("last_name")) + { + if (obj["last_name"].IsString()) + lastName = obj["last_name"].GetString(); + else + throw ParseError("Field 'last_name' has invalid type"); + } + + std::string username; + if (obj.HasMember("username")) + { + if (obj["username"].IsString()) + username = obj["username"].GetString(); + else + throw ParseError("Field 'username' has invalid type"); + } + + return User(id, firstName, lastName, username); + } +} + using namespace telebotxx; -const rapidjson::Value& parseResponse(const rapidjson::Document& doc) -{ - using namespace rapidjson; - if (!doc.IsObject()) - throw ParseError("Object expected"); - - // Get status - if (!doc.HasMember("ok") || !doc["ok"].IsBool()) - throw ParseError("Field 'ok' not found or has invalid type"); - bool ok = doc["ok"].GetBool(); - - if (ok) - { - if (!doc.HasMember("result") || !doc["result"].IsObject()) - throw ParseError("Field 'result' not found or has invalid type"); - return doc["result"]; - } - else - { - if (!doc.HasMember("error_code") || !doc["error_code"].IsInt()) - throw ParseError("Field 'error_code' not found or has invalid type"); - int code = doc["error_code"].GetInt(); - - if (!doc.HasMember("description") || !doc["description"].IsString()) - throw ParseError("Field 'description' not found or has invalid type"); - std::string description(doc["description"].GetString()); - - throw ApiError(code, description); - } -} - -User parseUser(const rapidjson::Value& obj) -{ - if (!obj.HasMember("id") || !obj["id"].IsInt()) - throw ParseError("Field 'id' not found or has invalid type"); - int id = obj["id"].GetInt(); - - if (!obj.HasMember("first_name") || !obj["first_name"].IsString()) - throw ParseError("Field 'first_name' not found or has invalid type"); - std::string firstName(obj["first_name"].GetString()); - - std::string lastName; - if (obj.HasMember("last_name")) - { - if (obj["last_name"].IsString()) - lastName = obj["last_name"].GetString(); - else - throw ParseError("Field 'last_name' has invalid type"); - } - - std::string username; - if (obj.HasMember("username")) - { - if (obj["username"].IsString()) - username = obj["username"].GetString(); - else - throw ParseError("Field 'username' has invalid type"); - } - - return User(id, firstName, lastName, username); -} - class BotApi::Impl { public: