Results 1 to 2 of 2

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    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);
    9.  
    10. m_buffer.simplified();
    11. QJsonParseError parse_error;
    12. QJsonDocument json_doc=QJsonDocument::fromJson(m_buffer,&parse_error);
    13.  
    14. if (parse_error.error == QJsonParseError::NoError)
    15. {
    16. QJsonObject i_packet=json_doc.object();
    17. m_buffer.clear();
    18. analys_packet(i_packet);
    19.  
    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 10:47.

Similar Threads

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

Tags for this Thread

Bookmarks

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.