QByteArray is truncated

Issue #13 resolved
Jochen Becher created an issue

QByteArray is truncated at the first '\0' character on sending it. QByteArray is not a string (though it supports a kind of string without encoding) but a generic byte array. All count() characters should be transferred.

I am using Json implementation from Qt 5.3.

Comments (3)

  1. Matt Broadstone

    Do you have a sample code to express this problem? It would make it much easier to fix if we could add an auto test for it.

  2. Jochen Becher reporter

    Sorry, I do not have a small example. But it should be easy to write one:

    Service slot:

    void test(const QByteArray &a) { qDebug() << a.count(); }

    Client call:

    invokeRemoteMethod("agent.test", QByteArray("\x1\x0\x2", 3));

    The service should print "3" but prints "1" because the QByteArray is transmitted only upto the '\x0' at index 1. The "\x0\x2" is omitted. I debugged this down to the json message which only contains the '\x1' as a string character.

    Edit: After reading a bit more about Json I understand that toBase64() and fromBase64() will do the magic thing. I added this to my code and it works fine. Shouldn't QByteArrays always be encoded using Base64?

  3. Matt Broadstone

    yeah, raw byte data is going to be hard since it's not supported by json itself. it might make sense to base64 encode your QByteArray, but then again it's up to the user to decide what to do with their data.

    I'm going to close this now as it is not a qjsonrpc related issue, rather something that should be brought up directly with qt with the qjson classes

  4. Log in to comment