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. Binarydata over QJsonObject becomes to big.
Forum Updated to NodeBB v4.3 + New Features

Binarydata over QJsonObject becomes to big.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 801 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.
  • T Offline
    T Offline
    themts
    wrote on last edited by
    #1

    Hey guys,

    I have a websocket communication where I'm using QJsonDocument for serialization.
    I also have to transmit binary-data so I encapsulate it as a base64 String.
    I would expect a 33% data-increase over a direct binary transmission but my data becomes nearly 3 times as big:

    Bin-Data around 236 kByte while Json-Data is around 631 kByte.
    Am I doing something wrong?

    QByteArray ba = qryAtt.value("attachment").toByteArray(); //get data from sql-query
    qDebug() << "SIZE:" << ba.size(); //236432
    
    QJsonObject obj {
      {"fileName", qryAtt.value("fileName").toString()},
      ...
      {"attachment", QString(ba.toBase64())}
    };          
    

    THX
    mts

    JonBJ 1 Reply Last reply
    0
    • T themts

      Hey guys,

      I have a websocket communication where I'm using QJsonDocument for serialization.
      I also have to transmit binary-data so I encapsulate it as a base64 String.
      I would expect a 33% data-increase over a direct binary transmission but my data becomes nearly 3 times as big:

      Bin-Data around 236 kByte while Json-Data is around 631 kByte.
      Am I doing something wrong?

      QByteArray ba = qryAtt.value("attachment").toByteArray(); //get data from sql-query
      qDebug() << "SIZE:" << ba.size(); //236432
      
      QJsonObject obj {
        {"fileName", qryAtt.value("fileName").toString()},
        ...
        {"attachment", QString(ba.toBase64())}
      };          
      

      THX
      mts

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @themts
      I don't know what you expect, but this seems to have nothing to do with JSON or websockets. Just look at the size of what ba.toBase64() returns.

      1 Reply Last reply
      1
      • T Offline
        T Offline
        themts
        wrote on last edited by
        #3

        no, that is not the point.
        The base64 itself has the size that I expected.
        Binary: 236432
        Base64: 315244
        33% bigger

        but why does my QJson object have a size of around 631000 byte ?

        jsulmJ Christian EhrlicherC 2 Replies Last reply
        0
        • T themts

          no, that is not the point.
          The base64 itself has the size that I expected.
          Binary: 236432
          Base64: 315244
          33% bigger

          but why does my QJson object have a size of around 631000 byte ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @themts Store your JSON in a file and take a look at it to see what exactly is in there.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • T Offline
            T Offline
            themts
            wrote on last edited by
            #5

            that is a little bit strange.
            The file itself has an expected size and also the content looks normal.
            I created a file before I send the data and when I received the data. Both are identical.

            BUT:

            void ...onBinaryMessageReceived(const QByteArray &message)
            {
                qDebug() << "onBinaryMessageReceived" << message.size(); // HERE it tells me 631476
            
                //Json
                QJsonDocument doc = QJsonDocument::fromBinaryData(message);
            
            1 Reply Last reply
            0
            • T themts

              no, that is not the point.
              The base64 itself has the size that I expected.
              Binary: 236432
              Base64: 315244
              33% bigger

              but why does my QJson object have a size of around 631000 byte ?

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Why do you use QJsonDocument::from/toBinaryData at all? QStrings are utf-16 encoded in binary representation so 2 * 315244 = 630488 bytes + rest.

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

              T 1 Reply Last reply
              3
              • Christian EhrlicherC Christian Ehrlicher

                Why do you use QJsonDocument::from/toBinaryData at all? QStrings are utf-16 encoded in binary representation so 2 * 315244 = 630488 bytes + rest.

                T Offline
                T Offline
                themts
                wrote on last edited by
                #7

                @Christian-Ehrlicher Yes! You are absolutely right... toJson converts to UTF-8 and so it is working as expected.
                Thank you!

                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