Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and load a QJsonDocument to a file?

Tags:

c++

json

qt

qt5

I am trying to learn how to use JSON and the Qt JSON classes. For example I wnat to create a simple QJsonDocument, save it to a file, load it into a different QJsonDocument and compare results.

I managed to create a QJsonDocument. However there is no simple command in the QJsonDocument interface to save it to a file. The same goes for loading the document from a file.

#include <QJsonObject>
#include <QJsonDocument>
#include <QVariant>

int main()
{
    QVariantMap map;
    map.insert("integer", 1);
    map.insert("double", 2.34);
    map.insert("bool", QVariant(true));
    map.insert("string", "word");
    QJsonObject object = QJsonObject::fromVariantMap(map);

    QJsonDocument document;
    document.setObject(object);

    // ?? save document to file
    // ?? load file to document

    return 0;
}

This answer shows how to load the document by

  1. reading to a QFile
  2. converting QFile to a QString
  3. converting the QString to a QByteArray
  4. constructing the QJsonDocument from the QByteArray

Is there a more straightforward way to do this?

like image 309
Martin Drozdik Avatar asked Jan 21 '14 13:01

Martin Drozdik


People also ask

How do I update a JSON file in Qt?

toObject(); m_addvalue. insert("Street","India");//set the value you want to modify ref=m_addvalue; //assign the modified object to reference JsonDocument. setObject(RootObject); // set to json document file. open(QFile::WriteOnly | QFile::Text | QFile::Truncate); file.

Does Qt support JSON?

Qt provides support for dealing with JSON data. JSON is a format to encode object data derived from Javascript, but now widely used as a data exchange format on the internet. The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data.


2 Answers

Personally, I think that code [that you linked to] looks a bit messy. Warning: head compiled code follows.

QJsonDocument loadJson(QString fileName) {
    QFile jsonFile(fileName);
    jsonFile.open(QFile::ReadOnly);
    return QJsonDocument().fromJson(jsonFile.readAll());
}

void saveJson(QJsonDocument document, QString fileName) {
    QFile jsonFile(fileName);
    jsonFile.open(QFile::WriteOnly);
    jsonFile.write(document.toJson());
}

This may not be perfect: it assumes QFile instead of QIODevice, but if you're dealing with only local files maybe it won't matter. You can then use these functions instead of repeating the Json load/save code everytime you need to load/save Json.

like image 51
John Chadwick Avatar answered Sep 29 '22 11:09

John Chadwick


No need for converting to string and back. With QSettings and QVariant classes you can easily do that. Create QVariant object from QJsonDocument and save it with QSettings. Look at functions QJsonDocument::fromVariant and QJsonDocument::toVariant. Combine them with QSettings class and specifically void QSettings::setValue ( const QString & key, const QVariant & value ) method, that works well with QVariant and that's it.

Also QSettings class has this constructor QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 ) that would allow you to set path to the file - fileName variable

like image 43
Shf Avatar answered Sep 29 '22 10:09

Shf