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
9895void 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