Conversion question
-
Hi,
I have a large (megabytes) string in a QJsonValue, that I need to convert to QByteArray, as I am sending the string as data with a QNetworkRequest.
Currently I am doing this:
myQJsonObject["myQJsonValue"].toString().toUtf8()
^^Would this incur copying the same data to memory many times for some reason? If so, how would you go about implementing this without unnecessary copyings?
Cheers,
-A888 -
Thanks, great to know. Do you think that with the particular expression here, data would be copied once or twice? Just curious. I would presume and hope that it can be converted to QString from QJsonValue with no cost, is this true?
Also it is curious that you say that the data needs to be re-encoded. I have been in the understanding that utf-8 is somewhat the standard internal encoding in Qt and QML, is this not true?
-
Thanks, great to know. Do you think that with the particular expression here, data would be copied once or twice? Just curious. I would presume and hope that it can be converted to QString from QJsonValue with no cost, is this true?
Also it is curious that you say that the data needs to be re-encoded. I have been in the understanding that utf-8 is somewhat the standard internal encoding in Qt and QML, is this not true?
I have been in the understanding that utf-8 is somewhat the standard internal encoding in Qt and QML, is this not true?
Qt uses UTF-16 internally.
-
Thanks, great to know. Do you think that with the particular expression here, data would be copied once or twice? Just curious. I would presume and hope that it can be converted to QString from QJsonValue with no cost, is this true?
Also it is curious that you say that the data needs to be re-encoded. I have been in the understanding that utf-8 is somewhat the standard internal encoding in Qt and QML, is this not true?
Source code files are utf8 encoded, but QString itself uses 16bit unicode.
http://doc.qt.io/qt-5/qstring.html#details
QJsonValue holds a reference to the string data, so no copy needs to be done by toString().
-
Also for anyone interested, I asked the same question at StackOverflow, and it was suggested that I use QJsonDocument for such operations, which is probably good advice:
http://stackoverflow.com/questions/35515065/qt-c-conversion-from-qjsonvalue-to-qbytearray
I simply needed the QByteArray, I had no idea that toUtf8() had this overhead. QJsonDocument seems to be able to convert straight to QByteArray with QByteArray QJsonDocument::toBinaryData() const