summaryrefslogtreecommitdiffstats
path: root/src/network/access
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@qt.io>2024-01-05 05:58:53 +0200
committerJuha Vuolle <juha.vuolle@qt.io>2024-01-12 06:37:11 +0200
commit1702a37a3955f449210fe3874cfcd35bb08417ad (patch)
treebd58357c8dde7712313ada0f973e92800fa83969 /src/network/access
parentbba26d72207304e02098d1436232357dd452de2a (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 <marc.mutz@qt.io>
Diffstat (limited to 'src/network/access')
-rw-r--r--src/network/access/qrestreply.cpp54
-rw-r--r--src/network/access/qrestreply.h3
-rw-r--r--src/network/access/qrestreply_p.h1
3 files changed, 11 insertions, 47 deletions
diff --git a/src/network/access/qrestreply.cpp b/src/network/access/qrestreply.cpp
index a736660786..8c575733dc 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"
diff --git a/src/network/access/qrestreply.h b/src/network/access/qrestreply.h
index e022a7b853..02b6d1196d 100644
--- a/src/network/access/qrestreply.h
+++ b/src/network/access/qrestreply.h
@@ -19,8 +19,7 @@ public:
QNetworkReply *networkReply() const;
- std::optional<QJsonObject> json();
- std::optional<QJsonArray> jsonArray();
+ std::optional<QJsonDocument> json();
QByteArray body();
QString text();
diff --git a/src/network/access/qrestreply_p.h b/src/network/access/qrestreply_p.h
index 7fe0842ff3..5e76b54bcb 100644
--- a/src/network/access/qrestreply_p.h
+++ b/src/network/access/qrestreply_p.h
@@ -36,7 +36,6 @@ public:
QByteArray contentCharset() const;
bool hasNonHttpError() const;
- QJsonDocument replyDataToJson();
};
QT_END_NAMESPACE