Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify json serialization by using rapidjson #2960

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed some problems with pretty printing jsons and fine tuning throug…
…h updating/passing the tests
aarranz committed Jul 20, 2017
commit b6185eff20a21ab2c88c0cba1ba96b66c5b29940
7 changes: 2 additions & 5 deletions src/lib/apiTypesV2/Attribute.cpp
Original file line number Diff line number Diff line change
@@ -25,8 +25,6 @@
#include <string>
#include <vector>

#include "rapidjson/prettywriter.h"

#include "common/errorMessages.h"
#include "common/RenderFormat.h"
#include "common/string.h"
@@ -63,12 +61,11 @@ std::string Attribute::render
}
else
{
rapidjson::StringBuffer sb;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
if (indent < 0)
{
indent = DEFAULT_JSON_INDENT;
}
JsonHelper writer(indent);

std::vector<std::string> metadataFilter;

@@ -83,7 +80,7 @@ std::string Attribute::render

writer.EndObject();

return sb.GetString();
return writer.str();
}
}

10 changes: 3 additions & 7 deletions src/lib/apiTypesV2/Entities.cpp
Original file line number Diff line number Diff line change
@@ -26,8 +26,6 @@
#include <vector>
#include <map>

#include "rapidjson/prettywriter.h"

#include "logMsg/traceLevels.h"
#include "logMsg/logMsg.h"
#include "ngsi10/QueryContextResponse.h"
@@ -69,15 +67,13 @@ std::string Entities::render
int indent
)
{
rapidjson::StringBuffer sb;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
if (indent < 0)
{
indent = DEFAULT_JSON_INDENT;
}
writer.SetIndent(' ', indent);
JsonHelper writer(indent);
toJson(writer, uriParamOptions, uriParam);
return sb.GetString();
return writer.str();
}


@@ -88,7 +84,7 @@ std::string Entities::render
*/
void Entities::toJson
(
rapidjson::Writer<rapidjson::StringBuffer>& writer,
JsonHelper& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam
)
15 changes: 7 additions & 8 deletions src/lib/apiTypesV2/Entities.h
Original file line number Diff line number Diff line change
@@ -29,8 +29,7 @@
#include <vector>
#include <map>

#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "common/JsonHelper.h"

#include "apiTypesV2/EntityVector.h"
#include "rest/OrionError.h"
@@ -58,12 +57,12 @@ class Entities
Entities();
~Entities();

void toJson(rapidjson::Writer<rapidjson::StringBuffer>& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam);
std::string render(std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam,
int indent = -1);
void toJson(JsonHelper& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam);
std::string render(std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam,
int indent = -1);

std::string check(ApiVersion apiVersion, RequestType requestType);
void present(const std::string& indent);
19 changes: 5 additions & 14 deletions src/lib/apiTypesV2/Entity.cpp
Original file line number Diff line number Diff line change
@@ -26,8 +26,6 @@
#include <vector>
#include <map>

#include "rapidjson/prettywriter.h"

#include "logMsg/traceLevels.h"
#include "logMsg/logMsg.h"
#include "common/string.h"
@@ -80,15 +78,9 @@ std::string Entity::render
int indent
)
{
rapidjson::StringBuffer sb;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
if (indent < 0)
{
indent = DEFAULT_JSON_INDENT;
}
writer.SetIndent(' ', indent);
JsonHelper writer(indent);
toJson(writer, uriParamOptions, uriParam);
return sb.GetString();
return writer.str();
}


@@ -99,7 +91,7 @@ std::string Entity::render
*/
void Entity::toJson
(
rapidjson::Writer<rapidjson::StringBuffer>& writer,
JsonHelper& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam
)
@@ -155,14 +147,13 @@ void Entity::toJson

if (renderId)
{
writer.Key("id");
writer.String(id.c_str());
writer.String("id", id);

writer.Key("type");
/* This is needed for entities coming from NGSIv1 (which allows empty or missing types) */
if (type != "")
{
writer.String(type.c_str());
writer.String(type);
}
else
{
15 changes: 7 additions & 8 deletions src/lib/apiTypesV2/Entity.h
Original file line number Diff line number Diff line change
@@ -29,8 +29,7 @@
#include <vector>
#include <map>

#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "common/JsonHelper.h"

#include "ngsi/ContextAttributeVector.h"
#include "rest/OrionError.h"
@@ -69,12 +68,12 @@ class Entity
Entity();
~Entity();

void toJson(rapidjson::Writer<rapidjson::StringBuffer>& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam);
std::string render(std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam,
int indent = -1);
void toJson(JsonHelper& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam);
std::string render(std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam,
int indent = -1);

std::string check(ApiVersion apiVersion, RequestType requestType);
void present(const std::string& indent);
17 changes: 5 additions & 12 deletions src/lib/apiTypesV2/EntityVector.cpp
Original file line number Diff line number Diff line change
@@ -28,8 +28,6 @@
#include <vector>
#include <map>

#include "rapidjson/prettywriter.h"

#include "logMsg/logMsg.h"
#include "logMsg/traceLevels.h"

@@ -52,14 +50,9 @@ std::string EntityVector::render
int indent
)
{
rapidjson::StringBuffer sb;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
if (indent < 0) {
indent = DEFAULT_JSON_INDENT;
}
writer.SetIndent(' ', indent);
JsonHelper writer(indent);
toJson(writer, uriParamOptions, uriParam);
return sb.GetString();
return writer.str();
}


@@ -69,9 +62,9 @@ std::string EntityVector::render
*/
void EntityVector::toJson
(
rapidjson::Writer<rapidjson::StringBuffer>& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam
JsonHelper& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam
)
{
writer.StartArray();
11 changes: 5 additions & 6 deletions src/lib/apiTypesV2/EntityVector.h
Original file line number Diff line number Diff line change
@@ -29,8 +29,7 @@
#include <vector>
#include <map>

#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "common/JsonHelper.h"

#include "apiTypesV2/Entity.h"

@@ -44,10 +43,10 @@ typedef struct EntityVector
{
std::vector<Entity*> vec;

void toJson(rapidjson::Writer<rapidjson::StringBuffer>& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam);
std::string render(std::map<std::string, bool>& uriParamOptions,
void toJson(JsonHelper& writer,
std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam);
std::string render(std::map<std::string, bool>& uriParamOptions,
std::map<std::string, std::string>& uriParam,
int indent = -1);

18 changes: 10 additions & 8 deletions src/lib/apiTypesV2/HttpInfo.cpp
Original file line number Diff line number Diff line change
@@ -70,36 +70,38 @@ HttpInfo::HttpInfo(const std::string& _url) : url(_url), verb(NOVERB), custom(fa
*
* HttpInfo::toJson -
*/
std::string HttpInfo::toJson()
void HttpInfo::toJson(JsonHelper& writer)
{
JsonHelper jh;
writer.StartObject();

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

if (custom)
{
if (this->payload != "")
{
jh.addString("payload", this->payload);
writer.String("payload", this->payload);
}

if (this->verb != NOVERB)
{
jh.addString("method", verbName(this->verb));
writer.String("method", verbName(this->verb));
}

if (qs.size() != 0)
{
jh.addRaw("qs", objectToJson(qs));
writer.Key("qs");
objectToJson(writer, qs);
}

if (headers.size() != 0)
{
jh.addRaw("headers", objectToJson(headers));
writer.Key("headers");
objectToJson(writer, headers);
}
}

return jh.str();
writer.EndObject();
}


4 changes: 3 additions & 1 deletion src/lib/apiTypesV2/HttpInfo.h
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@
#include <string>
#include <map>

#include "common/JsonHelper.h"

#include "mongo/client/dbclient.h"
#include "rest/Verb.h"

@@ -51,7 +53,7 @@ struct HttpInfo
HttpInfo();
explicit HttpInfo(const std::string& _url);

std::string toJson();
void toJson(JsonHelper& writer);
void fill(const mongo::BSONObj& bo);
};
}
Loading