Mantid
Loading...
Searching...
No Matches
QtJSONUtils.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <QFile>
10#include <QJsonDocument>
11#include <QJsonObject>
12#include <QTextStream>
13
14#include <stdexcept>
15
16namespace {
17
18void writeData(const QString &filename, const QByteArray &data) {
19 QFile jsonFile(filename);
20
21 if (!jsonFile.open(QFile::WriteOnly)) {
22 throw std::invalid_argument("Could not open file at " + filename.toStdString());
23 }
24
25 if (jsonFile.write(data) == -1) {
26 throw std::runtime_error("Failed to write data to " + filename.toStdString());
27 }
28}
29
30} // namespace
31namespace MantidQt::API {
32void saveJSONToFile(QString &filename, const QMap<QString, QVariant> &map) {
33 auto filenameString = filename.toStdString();
34 if (filenameString.find_last_of(".") == std::string::npos ||
35 filenameString.substr(filenameString.find_last_of(".") + 1) != std::string("json")) {
36 filename += ".json";
37 }
38
39 QJsonDocument jsonDocument(QJsonObject::fromVariantMap(map));
40 writeData(filename, jsonDocument.toJson());
41}
42
43QMap<QString, QVariant> loadJSONFromFile(const QString &filename) {
44 /* https://stackoverflow.com/questions/19822211/qt-parsing-json-using-qjsondocument-qjsonobject-qjsonarray
45 * is the source for a large portion of the source code for the Qt5
46 * implementation of this function, from user alanwsx and edited by BSMP.
47 */
48 QFile file(filename);
49 if (!file.open(QFile::ReadOnly)) {
50 throw std::invalid_argument("Cannot open file at: " + filename.toStdString());
51 }
52
53 // step 2
54 QTextStream text(&file);
55 QString jsonString;
56 jsonString = text.readAll();
57 file.close();
58
59 return loadJSONFromString(jsonString);
60}
61
62QMap<QString, QVariant> loadJSONFromString(const QString &jsonString) {
63 QByteArray jsonByteArray = jsonString.toLocal8Bit();
64
65 auto jsonDoc = QJsonDocument::fromJson(jsonByteArray);
66 if (jsonDoc.isNull()) {
67 throw std::runtime_error("Failed to create JSON doc.");
68 }
69 if (!jsonDoc.isObject()) {
70 throw std::runtime_error("JSON is not an object.");
71 }
72
73 QJsonObject jsonObj = jsonDoc.object();
74 if (jsonObj.isEmpty()) {
75 throw std::runtime_error("JSON object is empty.");
76 }
77
78 return jsonObj.toVariantMap();
79}
80
81} // namespace MantidQt::API
void EXPORT_OPT_MANTIDQT_COMMON saveJSONToFile(QString &filename, const QMap< QString, QVariant > &map)
Definition: QtJSONUtils.cpp:32
QMap< QString, QVariant > EXPORT_OPT_MANTIDQT_COMMON loadJSONFromString(const QString &jsonString)
Definition: QtJSONUtils.cpp:62
QMap< QString, QVariant > EXPORT_OPT_MANTIDQT_COMMON loadJSONFromFile(const QString &filename)
Definition: QtJSONUtils.cpp:43