Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJsonDocument::rawData in Qt6?

QJsonDocument::rawData in Qt6?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 238 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BrianL
    wrote last edited by
    #1

    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);
    
    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote last edited by
      #2

      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.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BrianL
        wrote last edited by
        #3

        would it be the same as
        docsize = doc.toJson().size()?

        B JKSHJ 2 Replies Last reply
        0
        • B BrianL

          would it be the same as
          docsize = doc.toJson().size()?

          B Offline
          B Offline
          Bonnie
          wrote last edited by Bonnie
          #4

          @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.

          1 Reply Last reply
          2
          • B BrianL

            would it be the same as
            docsize = doc.toJson().size()?

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote last edited by
            #5

            @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::Indented gives you a much bigger size than QJsonDocument::Compact.

            So, I second @Bonnie's question: What is your purpose of finding the size?

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            B 1 Reply Last reply
            1
            • JKSHJ JKSH

              @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::Indented gives you a much bigger size than QJsonDocument::Compact.

              So, I second @Bonnie's question: What is your purpose of finding the size?

              B Offline
              B Offline
              BrianL
              wrote last edited by BrianL
              #6

              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;
              	}
              }
              
              Christian EhrlicherC 1 Reply Last reply
              0
              • B BrianL

                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;
                	}
                }
                
                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote last edited by
                #7

                @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.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                B 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @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.

                  B Offline
                  B Offline
                  BrianL
                  wrote last edited by
                  #8

                  @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

                  JonBJ 1 Reply Last reply
                  0
                  • B BrianL

                    @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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote last edited by JonB
                    #9

                    @BrianL
                    I think the original was simply incorrect. But it probably didn't matter (not sure receiver has to respect Content-Length value, 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.

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote last edited by
                      #10

                      FYI QRestAccessManager will do that with less code, you can directly pass a QJsonDocument and it will set the correct headers.

                      1 Reply Last reply
                      2
                      • B BrianL has marked this topic as solved

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved