Change QRestReply json return type to QJsonDocument
The json return type and function naming has gone back and forth. Let's go with QJsonDocument after all, and add new overloads in future if necessary. Pick-to: 6.7 Task-number: QTBUG-119002 Change-Id: I3f9de0e6cba7d5c52d016d252d65b81f345af050 Reviewed-by: Marc Mutz <marc.mutz@qt.io>bb10
parent
bba26d7220
commit
1702a37a39
|
|
@ -153,8 +153,7 @@ void QRestReply::abort()
|
|||
}
|
||||
|
||||
/*!
|
||||
Returns the received data as a QJsonObject. Requires the reply to be
|
||||
finished.
|
||||
Returns the received data as a QJsonDocument.
|
||||
|
||||
The returned value is wrapped in \c std::optional. If the conversion
|
||||
from the received data fails (empty data or JSON parsing error),
|
||||
|
|
@ -166,44 +165,24 @@ void QRestReply::abort()
|
|||
This function returns \c {std::nullopt} and will not consume
|
||||
any data if the reply is not finished.
|
||||
|
||||
\sa jsonArray(), body(), text(), finished(), isFinished()
|
||||
\sa body(), text(), finished(), isFinished()
|
||||
*/
|
||||
std::optional<QJsonObject> QRestReply::json()
|
||||
std::optional<QJsonDocument> QRestReply::json()
|
||||
{
|
||||
Q_D(QRestReply);
|
||||
if (!isFinished()) {
|
||||
qCWarning(lcQrest, "Attempt to read json() of an unfinished reply, ignoring.");
|
||||
return std::nullopt;
|
||||
}
|
||||
const QJsonDocument json = d->replyDataToJson();
|
||||
return json.isObject() ? std::optional{json.object()} : std::nullopt;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the received data as a QJsonArray. Requires the reply to be
|
||||
finished.
|
||||
|
||||
The returned value is wrapped in \c std::optional. If the conversion
|
||||
from the received data fails (empty data or JSON parsing error),
|
||||
\c std::nullopt is returned.
|
||||
|
||||
Calling this function consumes the received data, and any further calls
|
||||
to get response data will return empty.
|
||||
|
||||
This function returns \c {std::nullopt} and will not consume
|
||||
any data if the reply is not finished.
|
||||
|
||||
\sa json(), body(), text(), finished(), isFinished()
|
||||
*/
|
||||
std::optional<QJsonArray> QRestReply::jsonArray()
|
||||
{
|
||||
Q_D(QRestReply);
|
||||
if (!isFinished()) {
|
||||
qCWarning(lcQrest, "Attempt to read jsonArray() of an unfinished reply, ignoring.");
|
||||
QJsonParseError parseError;
|
||||
const QByteArray data = d->networkReply->readAll();
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
qCDebug(lcQrest) << "Response data not JSON:" << parseError.errorString()
|
||||
<< "at" << parseError.offset << data;
|
||||
return std::nullopt;
|
||||
}
|
||||
const QJsonDocument json = d->replyDataToJson();
|
||||
return json.isArray() ? std::optional{json.array()} : std::nullopt;
|
||||
return doc;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
@ -472,19 +451,6 @@ bool QRestReplyPrivate::hasNonHttpError() const
|
|||
return networkReply->error() != QNetworkReply::NoError;
|
||||
}
|
||||
|
||||
QJsonDocument QRestReplyPrivate::replyDataToJson()
|
||||
{
|
||||
QJsonParseError parseError;
|
||||
const QByteArray data = networkReply->readAll();
|
||||
const QJsonDocument json = QJsonDocument::fromJson(data, &parseError);
|
||||
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
qCDebug(lcQrest) << "Response data not JSON:" << parseError.errorString()
|
||||
<< "at" << parseError.offset << data;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#include "moc_qrestreply.cpp"
|
||||
|
|
|
|||
|
|
@ -19,8 +19,7 @@ public:
|
|||
|
||||
QNetworkReply *networkReply() const;
|
||||
|
||||
std::optional<QJsonObject> json();
|
||||
std::optional<QJsonArray> jsonArray();
|
||||
std::optional<QJsonDocument> json();
|
||||
QByteArray body();
|
||||
QString text();
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,6 @@ public:
|
|||
|
||||
QByteArray contentCharset() const;
|
||||
bool hasNonHttpError() const;
|
||||
QJsonDocument replyDataToJson();
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ manager->post(request, myJson, this, [this](QRestReply *reply) {
|
|||
if (!reply->isSuccess()) {
|
||||
// ...
|
||||
}
|
||||
if (std::optional<QJsonObject> json = reply->json()) {
|
||||
if (std::optional json = reply->json()) {
|
||||
// use *json
|
||||
}
|
||||
});
|
||||
|
|
@ -37,7 +37,7 @@ manager->post(request, myJson, this, [this](QRestReply *reply) {
|
|||
manager->get(request, this, [this](QRestReply *reply) {
|
||||
if (!reply->isSuccess())
|
||||
// handle error
|
||||
if (std::optional<QJsonObject> json = reply->json())
|
||||
if (std::optional json = reply->json())
|
||||
// use *json
|
||||
});
|
||||
//! [3]
|
||||
|
|
|
|||
|
|
@ -739,8 +739,7 @@ void tst_QRestAccessManager::json()
|
|||
QTRY_VERIFY(server.isListening());
|
||||
QNetworkRequest request(server.url());
|
||||
QRestReply *replyFromServer = nullptr;
|
||||
QJsonObject responseJsonObject;
|
||||
QJsonArray responseJsonArray;
|
||||
QJsonDocument responseJsonDocument;
|
||||
|
||||
HttpData serverSideRequest; // The request data the server received
|
||||
HttpData serverSideResponse; // The response data the server responds with
|
||||
|
|
@ -754,9 +753,12 @@ void tst_QRestAccessManager::json()
|
|||
serverSideResponse.body = "{\"key1\":\"value1\",""\"key2\":\"value2\"}\n"_ba;
|
||||
manager.get(request, this, [&](QRestReply *reply) { replyFromServer = reply; });
|
||||
QTRY_VERIFY(replyFromServer);
|
||||
responseJsonObject = *replyFromServer->json();
|
||||
QCOMPARE(responseJsonObject["key1"], "value1");
|
||||
QCOMPARE(responseJsonObject["key2"], "value2");
|
||||
std::optional json = replyFromServer->json();
|
||||
QVERIFY(json);
|
||||
responseJsonDocument = *json;
|
||||
QVERIFY(responseJsonDocument.isObject());
|
||||
QCOMPARE(responseJsonDocument["key1"], "value1");
|
||||
QCOMPARE(responseJsonDocument["key2"], "value2");
|
||||
replyFromServer->deleteLater();
|
||||
replyFromServer = nullptr;
|
||||
|
||||
|
|
@ -772,10 +774,13 @@ void tst_QRestAccessManager::json()
|
|||
serverSideResponse.body = "[\"foo\", \"bar\"]\n"_ba;
|
||||
manager.get(request, this, [&](QRestReply *reply) { replyFromServer = reply; });
|
||||
QTRY_VERIFY(replyFromServer);
|
||||
responseJsonArray = *replyFromServer->jsonArray();
|
||||
QCOMPARE(responseJsonArray.size(), 2);
|
||||
QCOMPARE(responseJsonArray[0].toString(), "foo"_L1);
|
||||
QCOMPARE(responseJsonArray[1].toString(), "bar"_L1);
|
||||
json = replyFromServer->json();
|
||||
QVERIFY(json);
|
||||
responseJsonDocument = *json;
|
||||
QVERIFY(responseJsonDocument.isArray());
|
||||
QCOMPARE(responseJsonDocument.array().size(), 2);
|
||||
QCOMPARE(responseJsonDocument[0].toString(), "foo"_L1);
|
||||
QCOMPARE(responseJsonDocument[1].toString(), "bar"_L1);
|
||||
replyFromServer->deleteLater();
|
||||
replyFromServer = nullptr;
|
||||
|
||||
|
|
@ -783,7 +788,7 @@ void tst_QRestAccessManager::json()
|
|||
serverSideResponse.body = "foobar"_ba;
|
||||
manager.get(request, this, [&](QRestReply *reply) { replyFromServer = reply; });
|
||||
QTRY_VERIFY(replyFromServer);
|
||||
QVERIFY(!replyFromServer->jsonArray().has_value()); // std::nullopt returned
|
||||
QVERIFY(!replyFromServer->json().has_value()); // std::nullopt returned
|
||||
replyFromServer->deleteLater();
|
||||
replyFromServer = nullptr;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue