Results 1 to 3 of 3

Thread: add file in json structure

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default add file in json structure

    i want to add a file to json structure and send it,i want to add that file to json using json array for example i reading file as binary an append to json array(use this code) :
    Qt Code:
    1. void send_file(QString file_path)
    2. {
    3. cout<<"SEND FILE: "<<file_path.toStdString()<<"\n";
    4.  
    5. QFile i_file(file_path.remove(file_path.count()-1,1));
    6. i_file.open(QIODevice::ReadOnly);
    7. if (i_file.isOpen())
    8. {
    9. QJsonObject packet;
    10. packet["message_type"]="ANSWER_PACKET";
    11. packet["sender"]=mac_address;
    12. packet["reciver"]="control_panel";
    13. packet["answer_type"]="PRIVATE";
    14. packet["what"]="send_file";
    15. packet["file_type"]="GENERAL";
    16. packet["file_path"]=file_path;
    17. QDataStream i_file_stream(&i_file);
    18. QJsonArray i_array;
    19. while (!i_file.atEnd())
    20. {
    21. int len=1048576;
    22. char char_buffer[len];
    23. int size_read=i_file_stream.readRawData(char_buffer,len);
    24. if (size_read>0)
    25. {
    26. QByteArray temp(char_buffer,size_read);
    27. i_array.append(temp.constData());
    28. }
    29. }
    30.  
    31. packet["file_content"]=i_array;
    32. QJsonDocument json_doc(packet);
    33. QByteArray buffer=json_doc.toJson();
    34.  
    35. cout<<QString(buffer).toStdString()<<"\n";
    36. i_file.close();
    37. send_packet(buffer);
    38.  
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 
    now,in another hand i using this code for deserializing and writing file:
    Qt Code:
    1. void settings_frame_widget::save_file(QJsonObject packet)
    2. {
    3. QJsonArray i_array =packet["file_content"].toArray();
    4. QByteArray file_buffer;
    5.  
    6. foreach (QJsonValue item, i_array)
    7. {
    8. QByteArray temp=item.toVariant().toByteArray();
    9. file_buffer.append(temp);
    10. }
    11. //Write File:
    12. if (download_address!=QString())
    13. {
    14. QFile i_file(download_address);
    15. i_file.open(QIODevice::WriteOnly);
    16. if (i_file.isOpen())
    17. {
    18. QDataStream file_stream(&i_file);
    19. file_stream<<file_buffer;
    20. }
    21. QMessageBox msgbox;
    22. msgbox.setText("Download Finish!");
    23. msgbox.exec();
    24. i_file.close();
    25. download_address=QString();
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    for example when i send sequential file like this:
    Qt Code:
    1. {
    2. "res1":{
    3. "isError":false,
    4. "key":"1234566",
    5. "data":{
    6. "messages":"O.K!"
    7. }
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    when recive it and write to file , see a slacks byte in first of file,like this:
    slack.jpg
    this sudden cause of random access file is damaged...
    what's the problem!?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: add file in json structure

    Not saying that this is the cause for your proble, but you are using asymmetric operations for reading and writing the file.

    When reading you are getting raw bytes from the file, when writing you are writing QByteArray objects into a QDataStream.

    If the file content is not data encoded by QDataStream but arbitrary binary content, then I would suggest to just read/write using the methods in QFile.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ms2222 (24th November 2014)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: add file in json structure

    You will probably have issues if you embed unencoded arbitrary binary data into a JSON structure which must remain compilable Javascript/parsable text.

  5. The following user says thank you to ChrisW67 for this useful post:

    ms2222 (5th December 2014)

Similar Threads

  1. reading JSON file in Qt5
    By LZAntal in forum Newbie
    Replies: 4
    Last Post: 21st August 2017, 20:44
  2. Replies: 2
    Last Post: 13th June 2014, 04:56
  3. Save QMap with structure to file.
    By Xandareva in forum Newbie
    Replies: 8
    Last Post: 5th March 2013, 21:41
  4. Using external Json file in QML
    By Kelteseth in forum Qt Quick
    Replies: 0
    Last Post: 30th November 2012, 17:08
  5. Read Text file using structure..
    By umulingu in forum Qt Programming
    Replies: 7
    Last Post: 14th September 2009, 11:22

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.