diff options
author | Alexander Tkachev | 2016-07-22 18:27:49 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | cccfe7c247fed9b00c100e64433439386d6ef077 (patch) | |
tree | 91316ef86d15857b03aa5be8bafc1321cadc1b8e | |
parent | 43c940c98579e669826de838eff9dcb372d453f7 (diff) | |
download | scummvm-rg350-cccfe7c247fed9b00c100e64433439386d6ef077.tar.gz scummvm-rg350-cccfe7c247fed9b00c100e64433439386d6ef077.tar.bz2 scummvm-rg350-cccfe7c247fed9b00c100e64433439386d6ef077.zip |
CLOUD: Update BoxListDirectoryByIdRequest
It now uses special CurlJsonRequest static methods to check whether JSON
is an object, has a string or integer parameter.
-rw-r--r-- | backends/cloud/box/boxlistdirectorybyidrequest.cpp | 61 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.cpp | 41 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.h | 4 |
3 files changed, 51 insertions, 55 deletions
diff --git a/backends/cloud/box/boxlistdirectorybyidrequest.cpp b/backends/cloud/box/boxlistdirectorybyidrequest.cpp index 3e518066f2..776ee71d64 100644 --- a/backends/cloud/box/boxlistdirectorybyidrequest.cpp +++ b/backends/cloud/box/boxlistdirectorybyidrequest.cpp @@ -125,63 +125,16 @@ void BoxListDirectoryByIdRequest::responseCallback(Networking::JsonResponse resp Common::JSONArray items = responseObject.getVal("entries")->asArray(); for (uint32 i = 0; i < items.size(); ++i) { - if (!items[i]->isObject()) { - warning("BoxListDirectoryByIdRequest: \"entries\" item is not an object!"); - debug(9, "%s", items[i]->stringify(true).c_str()); - continue; - } + if (!Networking::CurlJsonRequest::jsonIsObject(items[i], "BoxListDirectoryByIdRequest")) continue; Common::JSONObject item = items[i]->asObject(); - if (!item.contains("id") || !item.getVal("id")->isString()) { - warning("BoxListDirectoryByIdRequest: \"entries\" item's \"id\"!"); - if (item.contains("id")) { - debug(9, "%s", item.getVal("id")->stringify(true).c_str()); - } else { - debug(9, "(not available)"); - } - continue; - } - - if (!item.contains("name") || !item.getVal("name")->isString()) { - warning("BoxListDirectoryByIdRequest: \"entries\" item's \"name\"!"); - if (item.contains("name")) { - debug(9, "%s", item.getVal("name")->stringify(true).c_str()); - } else { - debug(9, "(not available)"); - } - continue; - } - - if (!item.contains("type") || !item.getVal("type")->isString()) { - warning("BoxListDirectoryByIdRequest: \"entries\" item's \"type\"!"); - if (item.contains("type")) { - debug(9, "%s", item.getVal("type")->stringify(true).c_str()); - } else { - debug(9, "(not available)"); - } - continue; - } - - if (!item.contains("size") || (!item.getVal("size")->isString() && !item.getVal("size")->isIntegerNumber())) { - warning("BoxListDirectoryByIdRequest: \"entries\" item's \"size\"!"); - if (item.contains("size")) { - debug(9, "%s", item.getVal("size")->stringify(true).c_str()); - } else { - debug(9, "(not available)"); - } - continue; - } - - if (!item.contains("modified_at") || !item.getVal("modified_at")->isString()) { - warning("BoxListDirectoryByIdRequest: \"entries\" item's \"modified_at\"!"); - if (item.contains("modified_at")) { - debug(9, "%s", item.getVal("modified_at")->stringify(true).c_str()); - } else { - debug(9, "(not available)"); - } - continue; - } + if (!Networking::CurlJsonRequest::jsonContainsString(item, "id", "BoxListDirectoryByIdRequest")) continue; + if (!Networking::CurlJsonRequest::jsonContainsString(item, "name", "BoxListDirectoryByIdRequest")) continue; + if (!Networking::CurlJsonRequest::jsonContainsString(item, "type", "BoxListDirectoryByIdRequest")) continue; + if (!Networking::CurlJsonRequest::jsonContainsString(item, "modified_at", "BoxListDirectoryByIdRequest")) continue; + if (!Networking::CurlJsonRequest::jsonContainsString(item, "size", "BoxListDirectoryByIdRequest") && + !Networking::CurlJsonRequest::jsonContainsIntegerNumber(item, "size", "BoxListDirectoryByIdRequest")) continue; Common::String id = item.getVal("id")->asString(); Common::String name = item.getVal("name")->asString(); diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp index 875f5e7fbd..7f7cbca2e0 100644 --- a/backends/networking/curl/curljsonrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -31,7 +31,7 @@ namespace Networking { -CurlJsonRequest::CurlJsonRequest(JsonCallback cb, ErrorCallback ecb, Common::String url): +CurlJsonRequest::CurlJsonRequest(JsonCallback cb, ErrorCallback ecb, Common::String url) : CurlRequest(nullptr, ecb, url), _jsonCallback(cb), _contentsStream(DisposeAfterUse::YES), _buffer(new byte[CURL_JSON_REQUEST_BUFFER_SIZE]) {} @@ -97,4 +97,43 @@ void CurlJsonRequest::finishJson(Common::JSONValue *json) { else delete json; } +bool CurlJsonRequest::jsonIsObject(Common::JSONValue *item, const char *warningPrefix) { + if (item == nullptr) { + warning("%s: passed item is NULL", warningPrefix); + return false; + } + + if (item->isObject()) return true; + + warning("%s: passed item is not an object!", warningPrefix); + debug(9, "%s", item->stringify(true).c_str()); + return false; +} + +bool CurlJsonRequest::jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix) { + if (!item.contains(key)) { + warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key); + return false; + } + + if (item.getVal(key)->isString()) return true; + + warning("%s: passed item's \"%s\" attribute is not a string!", warningPrefix, key); + debug(9, "%s", item.getVal(key)->stringify(true).c_str()); + return false; +} + +bool CurlJsonRequest::jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix) { + if (!item.contains(key)) { + warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key); + return false; + } + + if (item.getVal(key)->isIntegerNumber()) return true; + + warning("%s: passed item's \"%s\" attribute is not an integer!", warningPrefix, key); + debug(9, "%s", item.getVal(key)->stringify(true).c_str()); + return false; +} + } // End of namespace Networking diff --git a/backends/networking/curl/curljsonrequest.h b/backends/networking/curl/curljsonrequest.h index 7858c89b80..1d1409efc8 100644 --- a/backends/networking/curl/curljsonrequest.h +++ b/backends/networking/curl/curljsonrequest.h @@ -52,6 +52,10 @@ public: virtual void handle(); virtual void restart(); + + static bool jsonIsObject(Common::JSONValue *item, const char *warningPrefix); + static bool jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix); + static bool jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix); }; } // End of namespace Networking |