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. How to put QByteArray into QJsonObject
Qt 6.11 is out! See what's new in the release blog

How to put QByteArray into QJsonObject

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.5k 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.
  • E Offline
    E Offline
    enne9
    wrote on last edited by
    #1

    Let's say I have a custom class for which I already implemented:

    friend QDataStream &operator<<(QDataStream& out, const CustomClass& c);
    friend QDataStream &operator>>(QDataStream& in, CustomClass& c);
    

    How can I put the QByteArray extracted using QDataSteam into a QJsonObject?
    Here, my not successful attempt:

    CustomClass c{};
    QByteArray data;
    QDataStream in(&data, QIODevice::WriteOnly);
    in << c;
    
    QJsonObject message;
    message["type"] = QStringLiteral("login");
    message["binary"] = data.data();
    

    Thanks

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      Json doesn't support binary data.
      You can encode data to hex string or base64 string.

      1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        https://doc.qt.io/qt-5/qbytearray.html#toBase64

        1 Reply Last reply
        3
        • E Offline
          E Offline
          enne9
          wrote on last edited by
          #4

          Thanks, got it.

          message["binary"] = QString(data.toBase64());
          
          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