diff options
author | Juha Vuolle <[email protected]> | 2024-01-05 05:58:53 +0200 |
---|---|---|
committer | Juha Vuolle <[email protected]> | 2024-01-12 06:37:11 +0200 |
commit | 1702a37a3955f449210fe3874cfcd35bb08417ad (patch) | |
tree | bd58357c8dde7712313ada0f973e92800fa83969 /src/network/access/qrestreply.cpp | |
parent | bba26d72207304e02098d1436232357dd452de2a (diff) |
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 <[email protected]>
Diffstat (limited to 'src/network/access/qrestreply.cpp')
-rw-r--r-- | src/network/access/qrestreply.cpp | 54 |
1 files changed, 10 insertions, 44 deletions
diff --git a/src/network/access/qrestreply.cpp b/src/network/access/qrestreply.cpp index a7366607869..8c575733dc5 100644 --- a/src/network/access/qrestreply.cpp +++ b/src/network/access/qrestreply.cpp @@ -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" |