Results 1 to 2 of 2

Thread: 'Garbage at the end of the document' error on parsing QJsonDocument

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2012
    Qt products
    Qt4 Qt5
    Unix/X11 Windows Android

    Default 'Garbage at the end of the document' error on parsing QJsonDocument

    i want to sending json object over tcp socket and, i serializing the json object like this :
    Qt Code:
    1. void message_shooter::send_packet(QJsonObject packet)
    2. {
    3. QJsonDocument json_doc(packet);
    4. //QByteArray packet_bytes=json_doc.toBinaryData();
    5. QByteArray packet_bytes=json_doc.toJson();
    6. m_client_socket->write(packet_bytes);
    7. m_client_socket->waitForBytesWritten();
    8. m_client_socket->flush();
    9. qDebug()<<"Byte Written!";
    10. }
    To copy to clipboard, switch view to plain text mode 

    and i reading data like this :
    Qt Code:
    1. void connectivity_layer::on_socket_ready_read()
    2. {
    3. qDebug()<<"Ready read...";
    4. qint64 incomming_packet_size=m_socket->bytesAvailable();
    5. if (incomming_packet_size > 0)
    6. {
    7. QByteArray received_bytes=m_socket->read(incomming_packet_size);
    8. m_buffer.append(received_bytes);
    10. m_buffer.simplified();
    11. QJsonParseError parse_error;
    12. QJsonDocument json_doc=QJsonDocument::fromJson(m_buffer,&parse_error);
    14. if (parse_error.error == QJsonParseError::NoError)
    15. {
    16. QJsonObject i_packet=json_doc.object();
    17. m_buffer.clear();
    18. analys_packet(i_packet);
    20. }
    21. else
    22. {
    23. qDebug()<<"Error!";
    24. qDebug()<<QString::fromUtf8(m_buffer);
    25. qDebug()<<parse_error.errorString();
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    But this doesn't work and print 'Garbage at the end of the document' error! [when the data received completely]
    how can i solve this problem?
    Last edited by ms2222; 25th December 2015 at 09:47.

  2. #2
    Join Date
    Jan 2006
    Graz, Austria
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: 'Garbage at the end of the document' error on parsing QJsonDocument

    It is usually bad idea to not have the message length as part of the protocol.

    In your case you have to re-attempt parsing even if the message is not complete yet.
    What's even worse, if you parse a message you are clearing the buffer, throwing away any data for the next message that you have already received.

    So if you want to make this something robust, first transmit the length of the serialized JSON block, then the block itself.
    On the receiver side, first read the length, then as many bytes as necessary before attempting parsing and then keep all remaining available data for the next round.


    P.S.: QByteArray::simplified() is const. And a very bad idea anyway to call on an input buffer.


Similar Threads

  1. QJsonDocument toJson(QJsonDocument::Compact) not working
    By elmar.wieser in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2015, 06:23
  2. Replies: 2
    Last Post: 13th July 2014, 03:09
  3. Replies: 5
    Last Post: 3rd September 2013, 09:35
  4. Create fragments of XML while parsing XML document
    By bleze in forum Qt Programming
    Replies: 2
    Last Post: 3rd January 2013, 10:21
  5. QTextDocument -no document error
    By poporacer in forum Newbie
    Replies: 8
    Last Post: 11th April 2012, 01:08

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.