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. Is there a more efficient way to decoding JSON from QWebSocket?
Forum Updated to NodeBB v4.3 + New Features

Is there a more efficient way to decoding JSON from QWebSocket?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 98 Views 4 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.
  • martin_kyM Offline
    martin_kyM Offline
    martin_ky
    wrote last edited by martin_ky
    #1

    Many (if not most) WebSocket APIs out there use JSON format, which is sent as text frames, which use UTF-8 encoding on the wire. In QWebSocket, text frames are handled by following APIs, which expose the message as an already decoded QString:

    • QWebSocket::sendTextMessage(const QString &message)
    • QWebSocket::textMessageReceived(const QString &message)

    On the other hand, the native JSON support in Qt which is implemented by QJsonDocument provide APIs for serializing and deserializing: toJson() and fromJson(), which take an UTF-8 encoded QByteArray.

    That means, if I want to consume a JSON WebSocket API in Qt, I have to:

    1. receive a message as QString that is already decoded from UTF-8 internally by QWebSocket
    2. convert it to UTF-8 QByteArray so I can pass it to QJsonDocument::fromJson()

    Analogous but reverse process goes for sending JSON messages.

    This seems very inefficient, as there are apparently two needless conversions: UTF-8 -> QString -> UTF-8, before the JSON message can actually by read by QJsonDocument.

    Surely, there must be a more efficient way. Am I missing something? Is there a way to send/receive an UTF-8 encoded string directly using QWebSocket? Can these conversions be avoided, or is the Qt API lacking here? Thanks for any clues.

    JKSHJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      From a very quick look at the class, what about using sendBinaryMessage ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • martin_kyM martin_ky

        Many (if not most) WebSocket APIs out there use JSON format, which is sent as text frames, which use UTF-8 encoding on the wire. In QWebSocket, text frames are handled by following APIs, which expose the message as an already decoded QString:

        • QWebSocket::sendTextMessage(const QString &message)
        • QWebSocket::textMessageReceived(const QString &message)

        On the other hand, the native JSON support in Qt which is implemented by QJsonDocument provide APIs for serializing and deserializing: toJson() and fromJson(), which take an UTF-8 encoded QByteArray.

        That means, if I want to consume a JSON WebSocket API in Qt, I have to:

        1. receive a message as QString that is already decoded from UTF-8 internally by QWebSocket
        2. convert it to UTF-8 QByteArray so I can pass it to QJsonDocument::fromJson()

        Analogous but reverse process goes for sending JSON messages.

        This seems very inefficient, as there are apparently two needless conversions: UTF-8 -> QString -> UTF-8, before the JSON message can actually by read by QJsonDocument.

        Surely, there must be a more efficient way. Am I missing something? Is there a way to send/receive an UTF-8 encoded string directly using QWebSocket? Can these conversions be avoided, or is the Qt API lacking here? Thanks for any clues.

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

        @martin_ky said in Is there a more efficient way to decoding JSON from QWebSocket?:

        Surely, there must be a more efficient way.

        You have a good point: https://bugreports.qt.io/browse/QTBUG-133100

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

        1 Reply Last reply
        3
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote last edited by
          #4

          Looks like there are QByteArray versions of send and receive for QWebSocket, at least in 6.9

          1 Reply Last reply
          0
          • martin_kyM Offline
            martin_kyM Offline
            martin_ky
            wrote last edited by
            #5

            Folks suggesting to use sendBinaryMessage() and binaryMessageReceived() - I don't think that's a choice that I can make as a consumer of a WebSocket API. As far as my understanding of WebSockets protocol go, they work in one of two modes - either transport text frames (encoded as UTF-8 bytes) or binary mode (raw bytes). If the server chooses to send a JSON as a text message, I cannot receive it via the binaryMessageReceived() signal - QWebSocket does not emit this signal when a text message is received. To my knowledge, as of Qt 6.9.1, there is no way to access the UTF-8 bytes of a received text message via the QWebSocket API, only the already and often unnecessarily decoded QString.

            @JKSH: Thanks for the QTBUG - that is exactly my point.

            JonBJ 1 Reply Last reply
            0
            • martin_kyM martin_ky

              Folks suggesting to use sendBinaryMessage() and binaryMessageReceived() - I don't think that's a choice that I can make as a consumer of a WebSocket API. As far as my understanding of WebSockets protocol go, they work in one of two modes - either transport text frames (encoded as UTF-8 bytes) or binary mode (raw bytes). If the server chooses to send a JSON as a text message, I cannot receive it via the binaryMessageReceived() signal - QWebSocket does not emit this signal when a text message is received. To my knowledge, as of Qt 6.9.1, there is no way to access the UTF-8 bytes of a received text message via the QWebSocket API, only the already and often unnecessarily decoded QString.

              @JKSH: Thanks for the QTBUG - that is exactly my point.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by
              #6

              @martin_ky You might like to post something to this effect into the bug report.

              1 Reply Last reply
              0
              • Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote last edited by Kent-Dorfman
                #7

                The only way for the API to know if it is receiving data that is to be binary or text is through the mime-type header. I'd suggest verifying if websocket protocol uses mime-types. If what @martin_ky is true, then he should see appropriate mime-type headers in the transactions.

                The other option is to throw out QWebSocket and do TCP level session transport where everything is consider a stream of octets...or of course live with the QString/byte-array translations...I mean, is the amount of data being processed really a bottleneck in translating back and forth?

                1 Reply Last reply
                0
                • martin_kyM Offline
                  martin_kyM Offline
                  martin_ky
                  wrote last edited by
                  #8

                  @JonB The bugreport QTBUG-133100 is accurate. I upvoted it. You can too, if you want.

                  @Kent-Dorfman There is no such thing as mime-type in the WebSocket protocol. Basically, there is just 1 bit in the wire format that differentiates between a text message and a binary messages. When QWebSocket receives a text messages it decodes the UTF-8 payload and emits the QString signal. I see no way around it in Qt 6.9.

                  1 Reply Last reply
                  0

                  • Login

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