QJsonDocument::rawData in Qt6?
-
Hi,
I am currently upgrading some code that was using Qt5 to Qt6, I noticed QJsonDocument::rawData does not exist anymore, is there an alternative way to do the following code in Qt6?
QJsonDocument doc(payload); // payload is an external QJsonObject int docsize = 0; doc.rawData(&docsize); -
from the Qt 5.15 doc:
Note: Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.
-
@BrianL said in QJsonDocument::rawData in Qt6?:
would it be the same as
docsize = doc.toJson().size()?I don't think it is.
What's the use case for this function in your code?
For binary serialization, as the documentation content above mentioned, your should convert Qt JSON types to Qt CBOR types.
Or if you really need to be compatible with old codes, for example, loading from binary files that were written by old Qt5 codes, you can use QBinaryJson from Qt 5 Compatibility module (it needs to be installed in Additional Libraries if you use Qt online installer).
But since in your code the binary representation is not even saved, so I suppose that's not the case. -
@BrianL said in QJsonDocument::rawData in Qt6?:
would it be the same as
docsize = doc.toJson().size()?No.
- The size reported by rawData() is related to the amount of RAM taken by the QJsonDocument object.
- This is affected by Qt version: Qt 5.12 can give you a different size from Qt 5.15.
- The size reported by toJson().size() is related to the number of characters in your plain-text output.
- This is affected by format:
QJsonDocument::Indentedgives you a much bigger size thanQJsonDocument::Compact.
- This is affected by format:
So, I second @Bonnie's question: What is your purpose of finding the size?
- The size reported by rawData() is related to the amount of RAM taken by the QJsonDocument object.
-
@BrianL said in QJsonDocument::rawData in Qt6?:
would it be the same as
docsize = doc.toJson().size()?No.
- The size reported by rawData() is related to the amount of RAM taken by the QJsonDocument object.
- This is affected by Qt version: Qt 5.12 can give you a different size from Qt 5.15.
- The size reported by toJson().size() is related to the number of characters in your plain-text output.
- This is affected by format:
QJsonDocument::Indentedgives you a much bigger size thanQJsonDocument::Compact.
- This is affected by format:
So, I second @Bonnie's question: What is your purpose of finding the size?
This is not my code, but it was used for QNetworkRequest setting "Content-Length" field
QNetworkReply* HttpService::sendRequest(const Url& url, const QByteArray& verb, const Token& token, const QJsonObject& payload) { QNetworkRequest request(url.url()); request.setRawHeader("Authorization", token.authorization()); request.setRawHeader("User-Agent", user_agent_.toUtf8()); if (payload.empty()) { request.setRawHeader("Content-Length", "0"); return network_access_manager_.sendCustomRequest(request, verb); } else { QJsonDocument doc(payload); int docsize = 0; doc.rawData(&docsize); request.setRawHeader("Content-Type", "application/json"); request.setRawHeader("Content-Length", QString::number(docsize).toLocal8Bit()); QBuffer* buf = new QBuffer; buf->open(QBuffer::WriteOnly); buf->write(QJsonDocument(payload).toJson(QJsonDocument::Compact)); buf->close(); buf->open(QBuffer::ReadOnly); QNetworkReply* reply = network_access_manager_.sendCustomRequest(request, verb, buf); connect(reply, &QNetworkReply::finished, [buf] { delete buf; }); return reply; } } - The size reported by rawData() is related to the amount of RAM taken by the QJsonDocument object.
-
This is not my code, but it was used for QNetworkRequest setting "Content-Length" field
QNetworkReply* HttpService::sendRequest(const Url& url, const QByteArray& verb, const Token& token, const QJsonObject& payload) { QNetworkRequest request(url.url()); request.setRawHeader("Authorization", token.authorization()); request.setRawHeader("User-Agent", user_agent_.toUtf8()); if (payload.empty()) { request.setRawHeader("Content-Length", "0"); return network_access_manager_.sendCustomRequest(request, verb); } else { QJsonDocument doc(payload); int docsize = 0; doc.rawData(&docsize); request.setRawHeader("Content-Type", "application/json"); request.setRawHeader("Content-Length", QString::number(docsize).toLocal8Bit()); QBuffer* buf = new QBuffer; buf->open(QBuffer::WriteOnly); buf->write(QJsonDocument(payload).toJson(QJsonDocument::Compact)); buf->close(); buf->open(QBuffer::ReadOnly); QNetworkReply* reply = network_access_manager_.sendCustomRequest(request, verb, buf); connect(reply, &QNetworkReply::finished, [buf] { delete buf; }); return reply; } }@BrianL said in QJsonDocument::rawData in Qt6?:
"Content-Length" field
Which is wrong as @JKSH wrote. Fix the code and pass the correct content-length value.
-
@BrianL said in QJsonDocument::rawData in Qt6?:
"Content-Length" field
Which is wrong as @JKSH wrote. Fix the code and pass the correct content-length value.
@Christian-Ehrlicher which is what I am trying to make sure, since it's writing toJson(QJsonDocument::Compact) to the buffer, this sounds like that is the size of it. I just wasn't sure why the original code didn't just use .size() but .rawData() instead
-
@Christian-Ehrlicher which is what I am trying to make sure, since it's writing toJson(QJsonDocument::Compact) to the buffer, this sounds like that is the size of it. I just wasn't sure why the original code didn't just use .size() but .rawData() instead
@BrianL
I think the original was simply incorrect. But it probably didn't matter (not sure receiver has to respectContent-Lengthvalue, or maybe the value was greater than the string length and that was OK). Passing the length of the serialized JSON being sent is the correct thing to do. -
FYI
QRestAccessManagerwill do that with less code, you can directly pass a QJsonDocument and it will set the correct headers. -
B BrianL has marked this topic as solved