[SOLVED] Pass a binary file in Json



  • 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


  • Qt Champions 2016

    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();
    


  • 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


  • Qt Champions 2016

    This post is deleted!

  • Qt Champions 2016

    This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.