Skip to content

Commit

Permalink
Merge pull request #3329 from telefonicaid/hardening/1298_json_vector…
Browse files Browse the repository at this point in the history
…_helper

Add JsonVectorHelper class and some related refactors
  • Loading branch information
AlvaroVega authored Oct 5, 2018
2 parents c7fff87 + 03e44cf commit ca2eac9
Show file tree
Hide file tree
Showing 42 changed files with 367 additions and 409 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Fix: metadata filter was lost during csub cache refresh (#3290)
- Fix: NGSIv1-like error responses were used in some NGSIv2 operations
- Remove: isDomain field in NGSIv1 registrations (it was never used)
2 changes: 1 addition & 1 deletion src/lib/apiTypesV2/Attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ std::string Attribute::toJson
{
if (renderFormat == NGSI_V2_KEYVALUES)
{
JsonHelper jh;
JsonObjectHelper jh;
jh.addRaw(pcontextAttribute->name, pcontextAttribute->toJsonValue());
out = jh.str();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/apiTypesV2/EntID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace ngsiv2
*/
std::string EntID::toJson()
{
JsonHelper jh;
JsonObjectHelper jh;

if (!this->id.empty())
{
Expand Down
29 changes: 9 additions & 20 deletions src/lib/apiTypesV2/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void Entity::filterAndOrderAttrs

/* ****************************************************************************
*
* Entity::render -
* Entity::toJsonV1 -
*
* This method was ported from old ContextElement class. It was name render() there
*
Expand Down Expand Up @@ -303,22 +303,15 @@ std::string Entity::toJson
*/
std::string Entity::toJsonValues(const std::vector<ContextAttribute*>& orderedAttrs)
{
std::string out = "[";
JsonVectorHelper jh;

for (unsigned int ix = 0; ix < orderedAttrs.size(); ix++)
{
ContextAttribute* caP = orderedAttrs[ix];
out += caP->toJsonValue();

if (ix != orderedAttrs.size() - 1)
{
out += ",";
}
jh.addRaw(caP->toJsonValue());
}

out += "]";

return out;
return jh.str();
}


Expand All @@ -329,7 +322,7 @@ std::string Entity::toJsonValues(const std::vector<ContextAttribute*>& orderedAt
*/
std::string Entity::toJsonUniqueValues(const std::vector<ContextAttribute*>& orderedAttrs)
{
std::string out = "[";
JsonVectorHelper jh;

std::map<std::string, bool> uniqueMap;

Expand All @@ -346,16 +339,12 @@ std::string Entity::toJsonUniqueValues(const std::vector<ContextAttribute*>& ord
}
else
{
out += value;
jh.addRaw(value);
uniqueMap[value] = true;
}

out += ",";
}

// The substring trick replaces final "," by "]". It is not very smart, but it saves
// a second pass on the vector, once the "unicity" has been calculated in the hashmap
return out.substr(0, out.length() - 1 ) + "]";
return jh.str();
}


Expand All @@ -366,7 +355,7 @@ std::string Entity::toJsonUniqueValues(const std::vector<ContextAttribute*>& ord
*/
std::string Entity::toJsonKeyvalues(const std::vector<ContextAttribute*>& orderedAttrs)
{
JsonHelper jh;
JsonObjectHelper jh;

if (renderId)
{
Expand All @@ -393,7 +382,7 @@ std::string Entity::toJsonKeyvalues(const std::vector<ContextAttribute*>& ordere
*/
std::string Entity::toJsonNormalized(const std::vector<ContextAttribute*>& orderedAttrs, const std::vector<std::string>& metadataFilter)
{
JsonHelper jh;
JsonObjectHelper jh;

if (renderId)
{
Expand Down
18 changes: 5 additions & 13 deletions src/lib/apiTypesV2/EntityVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "common/globals.h"
#include "common/tag.h"
#include "common/JsonHelper.h"
#include "alarmMgr/alarmMgr.h"

#include "ngsi/Request.h"
Expand All @@ -52,23 +53,14 @@ std::string EntityVector::toJson
const std::vector<std::string>& metadataFilter
)
{
if (vec.size() == 0)
{
return "[]";
}

std::string out;
JsonVectorHelper jh;

out += "[" + vec[0]->toJson(renderFormat, attrsFilter, blacklist, metadataFilter);

for (unsigned int ix = 1; ix < vec.size(); ++ix)
for (unsigned int ix = 0; ix < vec.size(); ++ix)
{
out += "," + vec[ix]->toJson(renderFormat, attrsFilter, blacklist, metadataFilter);
jh.addRaw(vec[ix]->toJson(renderFormat, attrsFilter, blacklist, metadataFilter));
}

out += "]";

return out;
return jh.str();
}


Expand Down
2 changes: 1 addition & 1 deletion src/lib/apiTypesV2/HttpInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ HttpInfo::HttpInfo(const std::string& _url) : url(_url), verb(NOVERB), custom(fa
*/
std::string HttpInfo::toJson()
{
JsonHelper jh;
JsonObjectHelper jh;

jh.addString("url", this->url);

Expand Down
13 changes: 7 additions & 6 deletions src/lib/apiTypesV2/Registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Registration::~Registration()
*/
std::string Registration::toJson(void)
{
JsonHelper jh;
JsonObjectHelper jh;

jh.addString("id", id);

Expand Down Expand Up @@ -101,7 +101,7 @@ std::string Registration::toJson(void)
*/
std::string DataProvided::toJson(void)
{
JsonHelper jh;
JsonObjectHelper jh;

jh.addRaw("entities", vectorToJson(entities));
jh.addRaw("attrs", vectorToJson(attributes));
Expand All @@ -117,10 +117,11 @@ std::string DataProvided::toJson(void)
*/
std::string Provider::toJson(void)
{
JsonHelper jh;
std::string urlAsJson = "{\"url\": \"" + http.url + "\"}";
JsonObjectHelper jhUrl;
jhUrl.addString("url", http.url);

jh.addRaw("http", urlAsJson);
JsonObjectHelper jh;
jh.addRaw("http", jhUrl.str());
jh.addString("supportedForwardingMode", forwardingModeToString(supportedForwardingMode));
jh.addBool("legacyForwarding", legacyForwardingMode? "true" : "false");

Expand All @@ -135,7 +136,7 @@ std::string Provider::toJson(void)
*/
std::string ForwardingInformation::toJson()
{
JsonHelper jh;
JsonObjectHelper jh;

jh.addNumber("timesSent", timesSent);

Expand Down
10 changes: 5 additions & 5 deletions src/lib/apiTypesV2/Subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Subscription::~Subscription()
*/
std::string Subscription::toJson(void)
{
JsonHelper jh;
JsonObjectHelper jh;

jh.addString("id", this->id);

Expand Down Expand Up @@ -111,7 +111,7 @@ std::string Subscription::toJson(void)
*/
std::string Notification::toJson(const std::string& attrsFormat)
{
JsonHelper jh;
JsonObjectHelper jh;

if (this->timesSent > 0)
{
Expand Down Expand Up @@ -169,7 +169,7 @@ std::string Notification::toJson(const std::string& attrsFormat)
*/
std::string Subject::toJson()
{
JsonHelper jh;
JsonObjectHelper jh;

jh.addRaw("entities", vectorToJson(this->entities));
jh.addRaw("condition", this->condition.toJson());
Expand All @@ -185,11 +185,11 @@ std::string Subject::toJson()
*/
std::string Condition::toJson()
{
JsonHelper jh;
JsonObjectHelper jh;

jh.addRaw("attrs", vectorToJson(this->attributes));

JsonHelper jhe;
JsonObjectHelper jhe;

if (this->expression.q != "") jhe.addString("q", this->expression.q);
if (this->expression.mq != "") jhe.addString("mq", this->expression.mq);
Expand Down
Loading

0 comments on commit ca2eac9

Please sign in to comment.