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 QRestReply::json() +std::optional 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 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 json(); - std::optional jsonArray(); + std::optional 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 diff --git a/src/network/doc/snippets/code/src_network_access_qrestaccessmanager.cpp b/src/network/doc/snippets/code/src_network_access_qrestaccessmanager.cpp index fcc8dbaef5..4ef077d0c2 100644 --- a/src/network/doc/snippets/code/src_network_access_qrestaccessmanager.cpp +++ b/src/network/doc/snippets/code/src_network_access_qrestaccessmanager.cpp @@ -26,7 +26,7 @@ manager->post(request, myJson, this, [this](QRestReply *reply) { if (!reply->isSuccess()) { // ... } - if (std::optional 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 json = reply->json()) + if (std::optional json = reply->json()) // use *json }); //! [3] diff --git a/tests/auto/network/access/qrestaccessmanager/tst_qrestaccessmanager.cpp b/tests/auto/network/access/qrestaccessmanager/tst_qrestaccessmanager.cpp index 89c5c09659..f46cb6fc11 100644 --- a/tests/auto/network/access/qrestaccessmanager/tst_qrestaccessmanager.cpp +++ b/tests/auto/network/access/qrestaccessmanager/tst_qrestaccessmanager.cpp @@ -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; }