From bd0108ce7547e8531d9ae379f58d0a7e5a6b9b23 Mon Sep 17 00:00:00 2001 From: Fermin Galan Marquez Date: Wed, 10 Oct 2018 15:26:26 +0200 Subject: [PATCH] REFACTOR related with #1298 --- src/lib/common/tag.h | 7 -- src/lib/ngsi/Metadata.cpp | 5 -- .../serviceRoutinesV2/entryPointsTreat.cpp | 15 ++--- src/lib/serviceRoutinesV2/semStateTreat.cpp | 65 ++++++------------- 4 files changed, 27 insertions(+), 65 deletions(-) diff --git a/src/lib/common/tag.h b/src/lib/common/tag.h index 94acefba4d..caf90a6832 100644 --- a/src/lib/common/tag.h +++ b/src/lib/common/tag.h @@ -39,13 +39,6 @@ * Macros for JSON rendering */ #define JSON_STR(value) std::string("\"" + std::string(value) + "\"") -#define JSON_NUMBER(value) std::string(value) -#define JSON_BOOL(bvalue) std::string((bvalue == true)? "true" : "false") - -#define JSON_PROP(name) std::string("\"" + std::string(name) + "\":") -#define JSON_VALUE(name, value) std::string(JSON_PROP(name) + JSON_STR(value)) -#define JSON_VALUE_NUMBER(name, value) std::string(JSON_PROP(name) + JSON_NUMBER(value)) -#define JSON_VALUE_BOOL(name, value) std::string(JSON_PROP(name) + ((value == true)? "true" : "false")) diff --git a/src/lib/ngsi/Metadata.cpp b/src/lib/ngsi/Metadata.cpp index 7e6e8e94b7..d8dd7145de 100644 --- a/src/lib/ngsi/Metadata.cpp +++ b/src/lib/ngsi/Metadata.cpp @@ -245,11 +245,6 @@ std::string Metadata::toJsonV1(bool comma) if (compoundValueP != NULL) { - // FIXME P8: We need to to this to avoid repeat the "value" keyword. I don't - // understand why (if you look to the similar code in Metadata::toJson(), it - // is not used there). It comes from the old implementation - compoundValueP->container = compoundValueP; - out += JSON_STR("value") + ":" + compoundValueP->toJson(true); } else if (valueType == orion::ValueTypeString) diff --git a/src/lib/serviceRoutinesV2/entryPointsTreat.cpp b/src/lib/serviceRoutinesV2/entryPointsTreat.cpp index ddcf9a3c0c..7f4beb6c7d 100644 --- a/src/lib/serviceRoutinesV2/entryPointsTreat.cpp +++ b/src/lib/serviceRoutinesV2/entryPointsTreat.cpp @@ -31,6 +31,7 @@ #include "common/string.h" #include "common/globals.h" #include "common/tag.h" +#include "common/JsonHelper.h" #include "ngsi/ParseData.h" #include "rest/ConnectionInfo.h" @@ -49,15 +50,13 @@ std::string entryPointsTreat ParseData* parseDataP ) { - std::string out = "{"; + JsonObjectHelper jh; - out += JSON_VALUE("entities_url", ENTITIES_URL) + ","; - out += JSON_VALUE("types_url", TYPES_URL) + ","; - out += JSON_VALUE("subscriptions_url", SUBSCRIPTIONS_URL) + ","; - out += JSON_VALUE("registrations_url", REGISTRATIONS_URL); - - out += "}"; + jh.addString("entities_url", ENTITIES_URL); + jh.addString("types_url", TYPES_URL); + jh.addString("subscriptions_url", SUBSCRIPTIONS_URL); + jh.addString("registrations_url", REGISTRATIONS_URL); ciP->httpStatusCode = SccOk; - return out; + return jh.str(); } diff --git a/src/lib/serviceRoutinesV2/semStateTreat.cpp b/src/lib/serviceRoutinesV2/semStateTreat.cpp index 6587fa9ed1..0fbabdeadd 100644 --- a/src/lib/serviceRoutinesV2/semStateTreat.cpp +++ b/src/lib/serviceRoutinesV2/semStateTreat.cpp @@ -31,6 +31,7 @@ #include "common/statistics.h" #include "common/clockFunctions.h" #include "common/tag.h" +#include "common/JsonHelper.h" #include "ngsi/ParseData.h" #include "rest/ConnectionInfo.h" @@ -47,21 +48,12 @@ * * semRender - * -* NOTE: in the current implementation, 'toplevel' is always false. -* When the operation "GET /admin/sem/" is implemented, 'toplevel' will -* be set to true for the rendering of the response to that request. */ -static const std::string semRender(const char* name, bool toplevel, const char* state) +static const std::string semRender(const char* state) { - std::string out; + JsonObjectHelper jh; - if (!toplevel) - { - out = JSON_STR(name) + ":"; - } - - out += "{"; - out += JSON_STR("status") + ":" + JSON_STR(state); + jh.addString("status", state); // // FIXME P4 Fill in more fields here in the future (as part of issue #2145): @@ -74,10 +66,7 @@ static const std::string semRender(const char* name, bool toplevel, const char* // "taken": number of taken semaphores (for connectionEndpoints only) // - - out += "}"; - - return out; + return jh.str(); } @@ -94,33 +83,19 @@ std::string semStateTreat ParseData* parseDataP ) { - const char* dbConnectionPoolState = mongoConnectionPoolSemGet(); - const char* dbConnectionState = mongoConnectionSemGet(); - const char* requestState = reqSemGet(); - const char* subCacheState = cacheSemGet(); - const char* transactionState = transSemGet(); - const char* timeStatState = timeStatSemGet(); - const char* logMsgState = lmSemGet(); - const char* alarmMgrState = alarmMgr.semGet(); - const char* connectionContextState = connectionContextSemGet(); - const char* connectionSubContextState = connectionSubContextSemGet(); - const char* metricsMgrState = metricsMgr.semStateGet(); - - std::string out = "{"; - - out += semRender("dbConnectionPool", false, dbConnectionPoolState) + ","; - out += semRender("dbConnection", false, dbConnectionState) + ","; - out += semRender("request", false, requestState) + ","; - out += semRender("subCache", false, subCacheState) + ","; - out += semRender("transaction", false, transactionState) + ","; - out += semRender("timeStat", false, timeStatState) + ","; - out += semRender("logMsg", false, logMsgState) + ","; - out += semRender("alarmMgr", false, alarmMgrState) + ","; - out += semRender("metricsMgr", false, metricsMgrState) + ","; - out += semRender("connectionContext", false, connectionContextState) + ","; - out += semRender("connectionEndpoints", false, connectionSubContextState); - - out += "}"; - - return out; + JsonObjectHelper jh; + + jh.addRaw("dbConnectionPool", semRender(mongoConnectionPoolSemGet())); + jh.addRaw("dbConnection", semRender(mongoConnectionSemGet())); + jh.addRaw("request", semRender(reqSemGet())); + jh.addRaw("subCache", semRender(cacheSemGet())); + jh.addRaw("transaction", semRender(transSemGet())); + jh.addRaw("timeStat", semRender(timeStatSemGet())); + jh.addRaw("logMsg", semRender(lmSemGet())); + jh.addRaw("alarmMgr", semRender(alarmMgr.semGet())); + jh.addRaw("metricsMgr", semRender(metricsMgr.semStateGet())); + jh.addRaw("connectionContext", semRender(connectionSubContextSemGet())); + jh.addRaw("connectionEndpoints", semRender(connectionSubContextSemGet())); + + return jh.str(); }