Skip to content

Commit acb2688

Browse files
authored
[Sdk 893, 889] Json exception handling (#70)
* parsing potential issue fixed! * Json parse exception handling added using json removed * added missing namespace * auto -> explicit datatype * curl resonse parsing exception handled * invalid json warning added
1 parent 5253824 commit acb2688

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

include/countly.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#endif
1818

1919
#include "nlohmann/json.hpp"
20-
using json = nlohmann::json;
2120

2221
#ifdef _WIN32
2322
#undef ERROR
@@ -54,7 +53,7 @@ class Countly {
5453

5554
struct HTTPResponse {
5655
bool success;
57-
json data;
56+
nlohmann::json data;
5857
};
5958

6059
void setSha256(cly::SHA256Function fun);
@@ -120,7 +119,7 @@ class Countly {
120119

121120
void updateRemoteConfig();
122121

123-
json getRemoteConfigValue(const std::string& key);
122+
nlohmann::json getRemoteConfigValue(const std::string& key);
124123

125124
void updateRemoteConfigFor(std::string *keys, size_t key_count);
126125

@@ -152,15 +151,15 @@ class Countly {
152151
template<typename T>
153152
void addSegmentation(const std::string& key, T value) {
154153
if (object.find("segmentation") == object.end()) {
155-
object["segmentation"] = json::object();
154+
object["segmentation"] = nlohmann::json::object();
156155
}
157156

158157
object["segmentation"][key] = value;
159158
}
160159

161160
std::string serialize() const;
162161
private:
163-
json object;
162+
nlohmann::json object;
164163
bool timer_running;
165164
std::chrono::system_clock::time_point timestamp;
166165
};
@@ -272,7 +271,7 @@ class Countly {
272271

273272
std::chrono::system_clock::time_point last_sent_session_request;
274273

275-
json session_params;
274+
nlohmann::json session_params;
276275
std::string salt;
277276

278277
std::unique_ptr<std::thread> thread;
@@ -292,7 +291,7 @@ class Countly {
292291
#endif
293292

294293
bool remote_config_enabled = false;
295-
json remote_config;
294+
nlohmann::json remote_config;
296295
};
297296

298297
#endif

src/countly.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212

1313
#include "countly.hpp"
1414

15-
#include "nlohmann/json.hpp"
16-
using json = nlohmann::json;
17-
1815
#ifndef COUNTLY_USE_CUSTOM_HTTP
1916
#ifdef _WIN32
2017
#include "Windows.h"
@@ -97,7 +94,7 @@ void Countly::setSha256(cly::SHA256Function fun) {
9794

9895
void Countly::setMetrics(const std::string& os, const std::string& os_version, const std::string& device,
9996
const std::string& resolution, const std::string& carrier, const std::string& app_version) {
100-
json metrics = json::object();
97+
nlohmann::json metrics = nlohmann::json::object();
10198

10299
if (!os.empty()) {
103100
metrics["_os"] = os;
@@ -604,14 +601,14 @@ bool Countly::updateSession() {
604601
began_session = true;
605602
}
606603

607-
json events = json::array();
604+
nlohmann::json events = nlohmann::json::array();
608605
bool no_events;
609606

610607
#ifndef COUNTLY_USE_SQLITE
611608
no_events = event_queue.empty();
612609
if (!no_events) {
613610
for (const auto& event_json: event_queue) {
614-
events.push_back(json::parse(event_json));
611+
events.push_back(nlohmann::json::parse(event_json));
615612
}
616613
}
617614
#else
@@ -641,7 +638,7 @@ bool Countly::updateSession() {
641638

642639
for (int event_index = 1; event_index < row_count+1; event_index++) {
643640
event_id_stream << table[event_index * column_count] << ',';
644-
events.push_back(json::parse(table[(event_index * column_count) + 1]));
641+
events.push_back(nlohmann::json::parse(table[(event_index * column_count) + 1]));
645642
}
646643

647644
event_id_stream.seekp(-1, event_id_stream.cur);
@@ -938,7 +935,15 @@ Countly::HTTPResponse Countly::sendHTTP(std::string path, std::string data) {
938935
} while (n_bytes_available > 0);
939936

940937
if (!body.empty()) {
941-
response.data = json::parse(body);
938+
const nlohmann::json& parseResult = nlohmann::json::parse(body, nullptr, false);
939+
if (parseResult.is_discarded())
940+
{
941+
log(Countly::LogLevel::WARNING, "[Countly][sendHTTP] Returned response from the server was not a valid JSON.");
942+
}
943+
else
944+
{
945+
response.data = parseResult;
946+
}
942947
}
943948
}
944949
}
@@ -984,8 +989,17 @@ Countly::HTTPResponse Countly::sendHTTP(std::string path, std::string data) {
984989
long status_code;
985990
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status_code);
986991
response.success = (status_code >= 200 && status_code < 300);
992+
987993
if (!body.empty()) {
988-
response.data = json::parse(body);
994+
const nlohmann::json& parseResult = nlohmann::json::parse(body, nullptr, false);
995+
if (parseResult.is_discarded())
996+
{
997+
log(Countly::LogLevel::WARNING, "[Countly][sendHTTP] Returned response from the server was not a valid JSON.");
998+
}
999+
else
1000+
{
1001+
response.data = parseResult;
1002+
}
9891003
}
9901004
}
9911005
curl_easy_cleanup(curl);
@@ -1056,9 +1070,9 @@ void Countly::updateRemoteConfig() {
10561070
}
10571071
}
10581072

1059-
json Countly::getRemoteConfigValue(const std::string& key) {
1073+
nlohmann::json Countly::getRemoteConfigValue(const std::string& key) {
10601074
mutex.lock();
1061-
json value = remote_config[key];
1075+
nlohmann::json value = remote_config[key];
10621076
mutex.unlock();
10631077
return value;
10641078
}
@@ -1071,7 +1085,7 @@ void Countly::updateRemoteConfigFor(std::string *keys, size_t key_count) {
10711085
};
10721086

10731087
{
1074-
json keys_json = json::array();
1088+
nlohmann::json keys_json = nlohmann::json::array();
10751089
for (size_t key_index = 0; key_index < key_count; key_index++) {
10761090
keys_json.push_back(keys[key_index]);
10771091
}
@@ -1096,7 +1110,7 @@ void Countly::updateRemoteConfigExcept(std::string *keys, size_t key_count) {
10961110
};
10971111

10981112
{
1099-
json keys_json = json::array();
1113+
nlohmann::json keys_json = nlohmann::json::array();
11001114
for (size_t key_index = 0; key_index < key_count; key_index++) {
11011115
keys_json.push_back(keys[key_index]);
11021116
}

0 commit comments

Comments
 (0)