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. [SOLVED] Pass a binary file in Json
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Pass a binary file in Json

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.3k Views 2 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.
  • N Offline
    N Offline
    Nando
    wrote on 13 Jul 2015, 11:22 last edited by Nando
    #1

    Hi,

    is there a way to transport binary data (QByteArray / File content) via Json?
    I did not find any methods to pass a QByteArray to a Json object....

    Any ideas?

    Greetings
    Nando

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 13 Jul 2015, 11:27 last edited by mrjj
      #2

      You should decode/encode it:

      #include <QCoreApplication>
      #include <QString>
      #include <QDebug>
      #include <QByteArray>
       
      QString base64_encode(QString string);
      QString base64_decode(QString string);
       
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
       
          QString srcString = "Hello";
          QString encodedString = base64_encode(srcString);
          QString decodedString = base64_decode(encodedString);
       
          qDebug() << "Encoded string is" << encodedString;
          qDebug() << "Decoded string is" << decodedString;
          return a.exec();
      }
       
      QString base64_encode(QString string){
          QByteArray ba;
          ba.append(string);
          return ba.toBase64();
      }
       
      QString base64_decode(QString string){
          QByteArray ba;
          ba.append(string);
          return QByteArray::fromBase64(ba);
      }
      

      from
      http://karanbalkar.com/2014/02/base64-encoding-and-decoding-using-qt-5-framework/
      site is really slow loading.

      edit:
      to use it with binary , just load it to QByteArray directly

      QFile file(fileName);
      if (!file.open(QIODevice::ReadOnly)) return;
      QByteArray blob = file.readAll();
      
      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nando
        wrote on 13 Jul 2015, 11:56 last edited by Nando
        #3

        Hi,

        thank you for your example.

        i tried it like this and now it is working fine, thank you very much!

        CLIENT:
        m_jsonObject["data"] = QString(data.toBase64());

        SERVER:
        QByteArray ba;
        ba.append(json["data"].toString());
        QByteArray data = QByteArray::fromBase64(ba);

        It works fine :)

        Greetings

        M 2 Replies Last reply 13 Jul 2015, 12:01
        0
        • N Nando
          13 Jul 2015, 11:56

          Hi,

          thank you for your example.

          i tried it like this and now it is working fine, thank you very much!

          CLIENT:
          m_jsonObject["data"] = QString(data.toBase64());

          SERVER:
          QByteArray ba;
          ba.append(json["data"].toString());
          QByteArray data = QByteArray::fromBase64(ba);

          It works fine :)

          Greetings

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 13 Jul 2015, 12:01 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • N Nando
            13 Jul 2015, 11:56

            Hi,

            thank you for your example.

            i tried it like this and now it is working fine, thank you very much!

            CLIENT:
            m_jsonObject["data"] = QString(data.toBase64());

            SERVER:
            QByteArray ba;
            ba.append(json["data"].toString());
            QByteArray data = QByteArray::fromBase64(ba);

            It works fine :)

            Greetings

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 13 Jul 2015, 12:03 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0

            1/5

            13 Jul 2015, 11:22

            • Login

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