diff --git a/src/common/network/json/json.h b/src/common/network/json/json.h index 177f2bba..7a9a2f64 100644 --- a/src/common/network/json/json.h +++ b/src/common/network/json/json.h @@ -17,6 +17,7 @@ #define __JSON_H__ #include "common/Defines.h" +#include "common/Log.h" #include #include @@ -171,6 +172,8 @@ namespace json template const T &get() const; template T &get(); + template const T getDefault(T def) const; + template T getDefault(T def); template void set(const T &); #if PICOJSON_USE_RVALUE_REFERENCE @@ -376,12 +379,22 @@ namespace json #define GET(ctype, var) \ template <> inline const ctype &value::get() const { \ + if (!is()) ::LogError(LOG_HOST, "JSON ERROR: type mismatch! call is() before get(), type = %s, %s", to_type().c_str(), to_str().c_str()); \ PICOJSON_ASSERT("type mismatch! call is() before get()" && is()); \ return var; \ } \ template <> inline ctype &value::get() { \ + if (!is()) ::LogError(LOG_HOST, "JSON ERROR: type mismatch! call is() before get(), type = %s, %s", to_type().c_str(), to_str().c_str()); \ PICOJSON_ASSERT("type mismatch! call is() before get()" && is()); \ return var; \ + } \ + template <> inline const ctype value::getDefault(ctype def) const { \ + if (!is()) return def; \ + return var; \ + } \ + template <> inline ctype value::getDefault(ctype def) { \ + if (!is()) return def; \ + return var; \ } GET(bool, u_.boolean_) diff --git a/src/sysview/AffListWnd.h b/src/sysview/AffListWnd.h index 08c9b6c3..ad5ca1ff 100644 --- a/src/sysview/AffListWnd.h +++ b/src/sysview/AffListWnd.h @@ -111,12 +111,12 @@ public: json::array fneAffils = rsp["affiliations"].get(); for (auto entry : fneAffils) { json::object peerAffils = entry.get(); - uint32_t peerId = peerAffils["peerId"].get(); + uint32_t peerId = peerAffils["peerId"].getDefault(0U); json::array affils = peerAffils["affiliations"].get(); for (auto pentry : affils) { json::object peerEntry = pentry.get(); - uint32_t dstId = peerEntry["dstId"].get(); - uint32_t srcId = peerEntry["srcId"].get(); + uint32_t dstId = peerEntry["dstId"].getDefault(0U); + uint32_t srcId = peerEntry["srcId"].getDefault(0U); // pad peer IDs properly std::ostringstream peerOss; diff --git a/src/sysview/PeerListWnd.h b/src/sysview/PeerListWnd.h index 2dececd6..68b96577 100644 --- a/src/sysview/PeerListWnd.h +++ b/src/sysview/PeerListWnd.h @@ -111,11 +111,11 @@ public: json::array fnePeers = rsp["peers"].get(); for (auto entry : fnePeers) { json::object peerObj = entry.get(); - uint32_t peerId = peerObj["peerId"].get(); - std::string peerAddress = peerObj["address"].get(); - uint16_t port = (uint16_t)peerObj["port"].get(); - bool connected = peerObj["connected"].get(); - uint32_t connectionState = (uint32_t)peerObj["connectionState"].get(); + uint32_t peerId = peerObj["peerId"].getDefault(0U); + std::string peerAddress = peerObj["address"].getDefault(""); + uint16_t port = (uint16_t)peerObj["port"].getDefault(0U); + bool connected = peerObj["connected"].getDefault(false); + uint32_t connectionState = (uint32_t)peerObj["connectionState"].getDefault(0U); std::string strConnState = ""; switch (connectionState) { case network::NET_CONN_STATUS::NET_STAT_RUNNING: @@ -135,25 +135,25 @@ public: break; } - uint32_t pingsReceived = (uint32_t)peerObj["pingsReceived"].get(); - uint64_t lastPing = (uint64_t)peerObj["lastPing"].get(); + uint32_t pingsReceived = (uint32_t)peerObj["pingsReceived"].getDefault(0U); + uint64_t lastPing = (uint64_t)peerObj["lastPing"].getDefault(0U); - uint32_t ccPeerId = (uint32_t)peerObj["controlChannel"].get(); + uint32_t ccPeerId = (uint32_t)peerObj["controlChannel"].getDefault(0U); json::array voiceChannels = peerObj["voiceChannels"].get(); std::vector voiceChannelPeers; for (auto vcEntry : voiceChannels) { - uint32_t vcPeerId = vcEntry.get(); + uint32_t vcPeerId = vcEntry.getDefault(0U); voiceChannelPeers.push_back(vcPeerId); } json::object peerConfig = peerObj["config"].get(); - std::string identity = peerConfig["identity"].get(); - std::string software = peerConfig["software"].get(); + std::string identity = peerConfig["identity"].getDefault(""); + std::string software = peerConfig["software"].getDefault(""); json::object channel = peerConfig["channel"].get(); - uint32_t chNo = (uint32_t)channel["channelNo"].get(); - uint8_t chId = channel["channelId"].get(); + uint32_t chNo = (uint32_t)channel["channelNo"].getDefault(1); + uint8_t chId = channel["channelId"].getDefault(0U); g_peerIdentityNameMap[peerId] = std::string(identity);