QRestReply: optionally return the QJsonParseError from json()
... and remove the debug output of the internal QJsonParseError. This allows users of the function to get the details in machine-readable form, and to distinguish between !finished and an actual Json parsing error. Found in API-review. Change-Id: Ia237b192a894d692b965f6bedb4c94d3b6537535 Reviewed-by: Juha Vuolle <juha.vuolle@qt.io> (cherry picked from commit 3a61de282c4740efe4a6fa1672e66efaf7c2b408) Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
parent
5fd6ad5101
commit
a915e2b211
@ -157,31 +157,34 @@ void QRestReply::abort()
|
|||||||
|
|
||||||
The returned value is wrapped in \c std::optional. If the conversion
|
The returned value is wrapped in \c std::optional. If the conversion
|
||||||
from the received data fails (empty data or JSON parsing error),
|
from the received data fails (empty data or JSON parsing error),
|
||||||
\c std::nullopt is returned.
|
\c std::nullopt is returned, and \a error is filled with details.
|
||||||
|
|
||||||
Calling this function consumes the received data, and any further calls
|
Calling this function consumes the received data, and any further calls
|
||||||
to get response data will return empty.
|
to get response data will return empty.
|
||||||
|
|
||||||
This function returns \c {std::nullopt} and will not consume
|
This function returns \c {std::nullopt} and will not consume
|
||||||
any data if the reply is not finished.
|
any data if the reply is not finished. If \a error is passed, it will be
|
||||||
|
set to QJsonParseError::NoError to distinguish this case from an actual
|
||||||
|
error.
|
||||||
|
|
||||||
\sa body(), text(), finished(), isFinished()
|
\sa body(), text(), finished(), isFinished()
|
||||||
*/
|
*/
|
||||||
std::optional<QJsonDocument> QRestReply::json()
|
std::optional<QJsonDocument> QRestReply::json(QJsonParseError *error)
|
||||||
{
|
{
|
||||||
Q_D(QRestReply);
|
Q_D(QRestReply);
|
||||||
if (!isFinished()) {
|
if (!isFinished()) {
|
||||||
qCWarning(lcQrest, "Attempt to read json() of an unfinished reply, ignoring.");
|
qCWarning(lcQrest, "Attempt to read json() of an unfinished reply, ignoring.");
|
||||||
|
if (error)
|
||||||
|
*error = {0, QJsonParseError::ParseError::NoError};
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
QJsonParseError parseError;
|
QJsonParseError parseError;
|
||||||
const QByteArray data = d->networkReply->readAll();
|
const QByteArray data = d->networkReply->readAll();
|
||||||
const QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
|
const QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
|
||||||
if (parseError.error != QJsonParseError::NoError) {
|
if (error)
|
||||||
qCDebug(lcQrest) << "Response data not JSON:" << parseError.errorString()
|
*error = parseError;
|
||||||
<< "at" << parseError.offset << data;
|
if (parseError.error)
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ QT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
class QByteArray;
|
class QByteArray;
|
||||||
class QDebug;
|
class QDebug;
|
||||||
|
struct QJsonParseError;
|
||||||
class QJsonDocument;
|
class QJsonDocument;
|
||||||
class QString;
|
class QString;
|
||||||
|
|
||||||
@ -25,7 +26,7 @@ public:
|
|||||||
|
|
||||||
QNetworkReply *networkReply() const;
|
QNetworkReply *networkReply() const;
|
||||||
|
|
||||||
std::optional<QJsonDocument> json();
|
std::optional<QJsonDocument> json(QJsonParseError *error = nullptr);
|
||||||
QByteArray body();
|
QByteArray body();
|
||||||
QString text();
|
QString text();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user