User Tools

Site Tools


wiki:qt_json_qstring_to_from_file_utility_shortcut

Table of Contents

intro

provides some convenience functions for loading/saving QStrings and Qt JSON object (to files)

header and source file, that's all there is. some problems are noted in the source file

#ifndef _Qtils_h_
#define _Qtils_h_
 
#include <QString>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>
 
namespace Qtils {
    QString stringFromFile(const QString & _path);
    void stringToFile(const QString & _path, const QString & _text);
 
    QJsonValue jsonFromFile(const QString & _path);
    bool jsonToFile(const QJsonValue & _src, const QString & _path);
 
    QJsonValue jsonFromString(const QString & _string);
    QString jsonToString(const QJsonValue & _src);
}
 
#endif
source
#include "Qtils.h"
 
#include <QFile>
 
namespace Qtils {
    QString stringFromFile(const QString & _path) {
        QFile file(_path);
        if (file.open(QIODevice::ReadOnly)) {
            return QString::fromUtf8(file.readAll());
        }
        return QString();
    }
 
    void stringToFile(const QString & _path, const QString & _text) {
        QFile file(_path);
        if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
            file.write(_text.toUtf8());
        }
    }
 
    QJsonValue jsonFromFile(const QString & _path) {
        QFile file(_path);
        if (file.open(QIODevice::ReadOnly)) {
            // interesting, can't cast up
            // that is QJsonDocument -> QJsonValue is impossible
            // however, i bet there is a single json value inside the doc
            QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
            // what's the top level? just want to return a value (spec is nothing here)
            // what's the problem with assiging a json doc to a json value?
 
            if (doc.isArray()) {
                return QJsonValue(doc.array());
            }
            else if (doc.isObject()) {
                return QJsonValue(doc.object());
            }
            else if (doc.isNull()) {
                return QJsonValue();
            }
            else if (doc.isEmpty()) {
                return QJsonValue();
            }
        }
        return QJsonValue();
    }
 
    bool jsonToFile(const QJsonValue & _src, const QString & _path) {
        QFile file(_path);
        if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
            // again with the conversion, maybe i should care, i don't
            QJsonDocument doc;
            if (_src.isArray()) {
                doc.setArray(_src.toArray());
                file.write(doc.toJson());
                return true;
            }
            else if (_src.isObject()) {
                doc.setObject(_src.toObject());
                file.write(doc.toJson());
                return true;
            }
            // you can't write a single value in. probably the best way to do it is just have a single entry array
        }
        return false;
    }
 
    QJsonValue jsonFromString(const QString & _string) {
        QJsonDocument doc = QJsonDocument::fromJson(_string.toUtf8());
        if (doc.isArray()) {
            return doc.array();
        } else if (doc.isObject()) {
            return doc.object();
        }
        return QJsonValue();
    }
 
    QString jsonToString(const QJsonValue & _src) {
        if (_src.isArray()) {
            return QString(QJsonDocument(_src.toArray()).toJson());
        } else if (_src.isObject()) {
            return QString(QJsonDocument(_src.toObject()).toJson());
        }
        return QString();
    }
}
wiki/qt_json_qstring_to_from_file_utility_shortcut.txt · Last modified: 2016/11/25 10:27 by xylene