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

QJsonDocument: Avoid QJsonObject and QJsonArray in interface #24

Open
wants to merge 2 commits into
base: QTTPv1.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/httpdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ HttpResponse& HttpData::getResponse()
return m_HttpResponse;
}

void HttpData::setResponse(const QJsonObject& json)
void HttpData::setResponse(const QJsonDocument& json)
Copy link
Owner

@supamii supamii Aug 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and tried against Qt 5.7 and this will throw up.

/Users/dev/personal/qttpPR/test/qttptest/qttptest.h:23: error: non-const lvalue reference to type 'QJsonObject' cannot bind to a value of unrelated type 'QJsonDocument' QJsonObject& json = data.getResponse().getJson(); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As a compromise, perhaps a MACRO can toggle between the types - however the cascading problem here will be that the object assignments will also be affected.

I am installing 5.9 now and I assume it'll run without a hitch.

Did I do something wrong? Your thoughts on how to proceed?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So looks like we need to update test files :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry. I had trouble getting my local Qt version with MSVC17 to work in QtCreator. That is why I didn't build the tests.

I'll take a look at them right away.

{
getResponse().setJson(json);
}
Expand All @@ -51,7 +51,7 @@ void HttpData::setErrorResponse(const QString& msg)
void HttpData::setErrorResponse(const QString &msg, HttpError code)
{
getResponse().setStatus(static_cast<HttpStatus>(code));
getResponse().getJson()["error"] = msg;
getResponse().getJson().setObject({{"error", msg}});
}

void HttpData::setErrorResponse(const QJsonObject& json)
Expand All @@ -62,7 +62,7 @@ void HttpData::setErrorResponse(const QJsonObject& json)
void HttpData::setErrorResponse(const QJsonObject &json, HttpError code)
{
getResponse().setStatus(static_cast<HttpStatus>(code));
getResponse().setJson(json);
getResponse().setJson(QJsonDocument(json));
}

const QUuid& HttpData::getUid() const
Expand Down
2 changes: 1 addition & 1 deletion src/httpdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class QTTPSHARED_EXPORT HttpData
*
* This is the same as getResponse().setJson();
*/
void setResponse(const QJsonObject& json);
void setResponse(const QJsonDocument &json);

//! Quick and easy way to set error messages.
void setErrorResponse(const QString& msg);
Expand Down
6 changes: 4 additions & 2 deletions src/httprequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ HttpMethod HttpRequest::getMethod(bool strictComparison) const
return m_MethodEnum;
}

const QJsonObject& HttpRequest::getJson() const
const QJsonDocument& HttpRequest::getJson() const
{
if(!m_Json.isEmpty())
{
Expand All @@ -86,7 +86,9 @@ const QJsonObject& HttpRequest::getJson() const
QList<QPair<QString, QString> > list = getQuery().queryItems();
for(auto i = list.begin(); i != list.end(); ++i)
{
m_Json.insert(i->first, i->second);
QJsonObject old = m_Json.object();
old.insert(i->first, i->second);
m_Json.setObject(old);
}

return m_Json;
Expand Down
4 changes: 2 additions & 2 deletions src/httprequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class QTTPSHARED_EXPORT HttpRequest
* should be called before dispatching to multiple threads (if there's some
* reason for multiple threads).
*/
const QJsonObject& getJson() const;
const QJsonDocument& getJson() const;

const QByteArray& getBody() const;

Expand Down Expand Up @@ -84,7 +84,7 @@ class QTTPSHARED_EXPORT HttpRequest
native::http::QttpRequest * m_Request;
HttpUrl m_HttpUrl;
mutable HttpMethod m_MethodEnum;
mutable QJsonObject m_Json;
mutable QJsonDocument m_Json;
QUrlQuery m_Query;
};

Expand Down
13 changes: 6 additions & 7 deletions src/httpresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ HttpStatus HttpResponse::getStatus() const
return m_Status;
}

QJsonObject& HttpResponse::getJson()
QJsonDocument& HttpResponse::getJson()
{
return m_Json;
}

const QJsonObject& HttpResponse::getJson() const
const QJsonDocument& HttpResponse::getJson() const
{
return m_Json;
}

void HttpResponse::setJson(const QJsonObject& json)
void HttpResponse::setJson(const QJsonDocument& json)
{
m_Json = json;
}
Expand All @@ -80,16 +80,15 @@ bool HttpResponse::finish(const string& body)
return m_Response->close();
}

bool HttpResponse::finish(const QJsonObject& json)
bool HttpResponse::finish(const QJsonDocument& json)
{
LOG_TRACE;
setHeader("Content-Type", "application/json");
QJsonDocument doc(json);

#ifdef QTTP_FORMAT_JSON_RESPONSE
return finish(doc.toJson(QJsonDocument::Indented));
return finish(json.toJson(QJsonDocument::Indented));
#else
return finish(doc.toJson(QJsonDocument::Compact));
return finish(json.toJson(QJsonDocument::Compact));
#endif
}

Expand Down
10 changes: 5 additions & 5 deletions src/httpresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class QTTPSHARED_EXPORT HttpResponse
* As an alternative, the caller may optionally complete the transaction
* with finishResponse().
*/
QJsonObject& getJson();
const QJsonObject& getJson() const;
void setJson(const QJsonObject& json);
QJsonDocument &getJson();
const QJsonDocument& getJson() const;
void setJson(const QJsonDocument &json);

/**
* @brief Preferred method when working with the json object. Populate
Expand All @@ -63,7 +63,7 @@ class QTTPSHARED_EXPORT HttpResponse
*/
bool finish(const std::string& body);
bool finish(const QByteArray& bytes);
bool finish(const QJsonObject& json);
bool finish(const QJsonDocument &json);

/**
* @return Boolean indicating if finishResponse() has been called.
Expand Down Expand Up @@ -113,7 +113,7 @@ class QTTPSHARED_EXPORT HttpResponse

native::http::QttpResponse * m_Response;
HttpStatus m_Status;
QJsonObject m_Json;
QJsonDocument m_Json;
quint32 m_ControlFlag;
};

Expand Down
26 changes: 13 additions & 13 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ function<void(HttpEvent*)> HttpServer::defaultEventCallback() const
default:
STATS_INC("http:method:unknown");
response.setStatus(HttpStatus::BAD_REQUEST);
QJsonObject& json = data.getResponse().getJson();
json["error"] = QSTR("Invalid HTTP method");
QJsonDocument& json = data.getResponse().getJson();
json.setObject({{"error", QSTR("Invalid HTTP method")}});
return;
}

Expand All @@ -597,8 +597,8 @@ function<void(HttpEvent*)> HttpServer::defaultEventCallback() const
{
LOG_ERROR("Invalid route");
response.setStatus(HttpStatus::INTERNAL_SERVER_ERROR);
QJsonObject& json = data.getResponse().getJson();
json["error"] = QSTR("Internal error");
QJsonDocument& json = data.getResponse().getJson();
json.setObject({{"error", QSTR("Internal error")}});
return;
}

Expand Down Expand Up @@ -682,8 +682,8 @@ function<void(HttpEvent*)> HttpServer::defaultEventCallback() const
if(!searchAndServeFile(data))
{
response.setStatus(HttpStatus::BAD_REQUEST);
QJsonObject& json = data.getResponse().getJson();
json["error"] = QSTR("Invalid request");
QJsonDocument& json = data.getResponse().getJson();
json.setObject({{"error", QSTR("Invalid request")}});
performPostprocessing(data);
}
}
Expand All @@ -694,20 +694,20 @@ function<void(HttpEvent*)> HttpServer::defaultEventCallback() const
{
LOG_ERROR("Exception caught" << e.what());
response.setStatus(HttpStatus::INTERNAL_SERVER_ERROR);
QJsonObject& json = data.getResponse().getJson();
json["error"] = e.what();
QJsonDocument& json = data.getResponse().getJson();
json.setObject({{"error", e.what()}});
}
catch(const QJsonObject& e)
{
LOG_ERROR("JSON caught" << e);
response.setStatus(HttpStatus::INTERNAL_SERVER_ERROR);
data.getResponse().getJson() = e;
data.getResponse().getJson().setObject(e);
}
catch(...)
{
response.setStatus(HttpStatus::INTERNAL_SERVER_ERROR);
QJsonObject& json = data.getResponse().getJson();
json["error"] = QSTR("Internal server error");
QJsonDocument& json = data.getResponse().getJson();
json.setObject({{"error", QSTR("Internal server error")}});
}

if(!response.isFinished())
Expand All @@ -734,8 +734,8 @@ function<void(HttpEvent*)> HttpServer::defaultEventCallback() const
obj["timeElapsedMs"] = (qreal)(uv_hrtime() - request.getTimestamp()) /
(qreal)1000000.00;

QJsonObject& json = data.getResponse().getJson();
json["requestMetadata"] = obj;
QJsonDocument& json = data.getResponse().getJson();
json.setObject({{"requestMetadata", obj}});
}

if( !response.finish())
Expand Down
2 changes: 1 addition & 1 deletion src/swagger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void Swagger::onGet(HttpData& data)
{
if(m_IsEnabled)
{
data.getResponse().setJson(m_Response);
data.getResponse().setJson(QJsonDocument(m_Response));
}
else
{
Expand Down
27 changes: 6 additions & 21 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,34 +292,19 @@ class QTTPSHARED_EXPORT Utils
return Utils::toByteArray(buffer.str());
}

static inline QJsonObject toJson(const std::stringstream& buffer, QJsonParseError* error = 0)
static inline QJsonDocument toJson(const std::stringstream& buffer, QJsonParseError* error = 0)
{
return QJsonDocument::fromJson(toByteArray(buffer), error).object();
return QJsonDocument::fromJson(toByteArray(buffer), error);
}

static inline QJsonObject toJson(const std::string& str, QJsonParseError* error = 0)
static inline QJsonDocument toJson(const std::string& str, QJsonParseError* error = 0)
{
return QJsonDocument::fromJson(toByteArray(str), error).object();
return QJsonDocument::fromJson(toByteArray(str), error);
}

static inline QJsonObject toJson(QByteArray bytes, QJsonParseError* error = 0)
static inline QJsonDocument toJson(QByteArray bytes, QJsonParseError* error = 0)
{
return QJsonDocument::fromJson(bytes, error).object();
}

static inline QJsonArray toArray(const std::stringstream& buffer, QJsonParseError* error = 0)
{
return QJsonDocument::fromJson(toByteArray(buffer), error).array();
}

static inline QJsonArray toArray(const std::string& str, QJsonParseError* error = 0)
{
return QJsonDocument::fromJson(toByteArray(str), error).array();
}

static inline QJsonArray toArray(QByteArray bytes, QJsonParseError* error = 0)
{
return QJsonDocument::fromJson(bytes, error).array();
return QJsonDocument::fromJson(bytes, error);
}
};

Expand Down