Use home-grown json library for recent files

This commit is contained in:
Casper Jeukendrup 2023-06-21 15:44:28 +02:00
parent 2b9f3f9adc
commit f186e5a716
No known key found for this signature in database
GPG key ID: 6C571BEF59E722DD
3 changed files with 29 additions and 21 deletions

View file

@ -719,6 +719,11 @@ JsonDocument::JsonDocument(JsonObject o)
{
}
JsonDocument::JsonDocument(JsonArray a)
: m_data(a.m_data)
{
}
ByteArray JsonDocument::toJson(Format format) const
{
std::string json = m_data->val.serialize(format == Format::Indented);

View file

@ -139,9 +139,9 @@ public:
JsonValueRef operator [](size_t i);
private:
friend class JsonValue;
friend class JsonObject;
friend class JsonDocument;
void detach();
std::shared_ptr<JsonData> m_data;
@ -187,6 +187,7 @@ class JsonDocument
public:
JsonDocument(std::shared_ptr<JsonData> d = nullptr);
JsonDocument(JsonObject o);
JsonDocument(JsonArray a);
enum class Format {
Indented,

View file

@ -23,12 +23,11 @@
#include <QtConcurrent>
#include <QJsonArray>
#include <QJsonDocument>
#include "async/async.h"
#include "defer.h"
#include "serialization/json.h"
#include "multiinstances/resourcelockguard.h"
using namespace mu::project;
@ -145,24 +144,28 @@ void RecentFilesController::loadRecentFilesList()
return;
}
QJsonParseError err;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data.val.toQByteArrayNoCopy(), &err);
if (err.error != QJsonParseError::NoError) {
std::string err;
const JsonDocument json = JsonDocument::fromJson(data.val, &err);
if (!err.empty()) {
LOGE() << "Loading JSON failed: " << err;
return;
}
if (!jsonDoc.isArray()) {
if (!json.isArray()) {
return;
}
for (const QJsonValue val : jsonDoc.array()) {
const JsonArray array = json.rootArray();
for (size_t i = 0; i < array.size(); ++i) {
const JsonValue val = array.at(i);
if (val.isString()) {
newList.emplace_back(io::path_t(val.toString()));
newList.emplace_back(io::path_t(val.toStdString()));
} else if (val.isObject()) {
QJsonObject obj = val.toObject();
const JsonObject obj = val.toObject();
ProjectFile file;
file.path = obj["path"].toString();
file.displayNameOverride = obj["displayName"].toString();
file.path = obj["path"].toStdString();
file.displayNameOverride = QString::fromStdString(obj["displayName"].toStdString());
newList.push_back(file);
} else {
continue;
@ -223,23 +226,22 @@ void RecentFilesController::saveRecentFilesList()
m_isSaving = false;
};
QJsonArray jsonArray;
JsonArray jsonArray;
for (const ProjectFile& file : m_recentFilesList) {
if (!file.displayNameOverride.isEmpty()) {
QJsonObject obj;
obj["path"] = file.path.toQString();
obj["displayName"] = file.displayNameOverride;
JsonObject obj;
obj["path"] = file.path.toStdString();
obj["displayName"] = file.displayNameOverride.toStdString();
jsonArray << obj;
} else {
jsonArray << file.path.toQString();
jsonArray << file.path.toStdString();
}
}
QJsonDocument jsonDoc(jsonArray);
QByteArray json = jsonDoc.toJson(QJsonDocument::Compact);
JsonDocument json(jsonArray);
mi::WriteResourceLockGuard resource_guard(multiInstancesProvider(), RECENT_FILES_RESOURCE_NAME);
Ret ret = fileSystem()->writeFile(configuration()->recentFilesJsonPath(), ByteArray::fromQByteArrayNoCopy(json));
Ret ret = fileSystem()->writeFile(configuration()->recentFilesJsonPath(), json.toJson());
if (!ret) {
LOGE() << "Failed to save recent files list: " << ret.toString();
}