mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 18:55:38 +08:00
refs #815 Catch and handle CJsonException when calling convertFromJson.
This commit is contained in:
@@ -763,7 +763,7 @@ namespace BlackMisc
|
||||
auto json = QJsonDocument::fromJson(file.readAll()).object();
|
||||
QUuid uuid(json.value("uuid").toString());
|
||||
CSequence<CProcessInfo> apps;
|
||||
apps.convertFromJson(json.value("apps").toObject());
|
||||
auto status = apps.convertFromJsonNoThrow(json.value("apps").toObject(), this, QStringLiteral("Error in %1 apps object").arg(m_filename));
|
||||
apps.removeIf([](const CProcessInfo &pi) { return ! pi.exists(); });
|
||||
|
||||
if (apps.isEmpty()) { uuid = CIdentifier().toUuid(); }
|
||||
|
||||
@@ -329,7 +329,15 @@ namespace BlackMisc
|
||||
{
|
||||
CVariantMap map;
|
||||
map.convertFromMemoizedJson(json);
|
||||
insertValues({ map, QDateTime::currentMSecsSinceEpoch() });
|
||||
if (! map.isEmpty()) { insertValues({ map, QDateTime::currentMSecsSinceEpoch() }); }
|
||||
}
|
||||
|
||||
CStatusMessageList CValueCache::loadFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
CVariantMap map;
|
||||
auto messages = map.convertFromMemoizedJsonNoThrow(json, categories, prefix);
|
||||
if (! map.isEmpty()) { insertValues({ map, QDateTime::currentMSecsSinceEpoch() }); }
|
||||
return messages;
|
||||
}
|
||||
|
||||
CStatusMessage CValueCache::saveToFiles(const QString &dir, const QString &keyPrefix)
|
||||
@@ -418,6 +426,7 @@ namespace BlackMisc
|
||||
keysInFiles.insert(filename.completeBaseName(), {});
|
||||
}
|
||||
}
|
||||
bool ok = true;
|
||||
for (auto it = keysInFiles.cbegin(); it != keysInFiles.cend(); ++it)
|
||||
{
|
||||
QFile file(dir + "/" + it.key() + ".json");
|
||||
@@ -435,13 +444,19 @@ namespace BlackMisc
|
||||
return CStatusMessage(this).error("Invalid JSON format in %1") << file.fileName();
|
||||
}
|
||||
CVariantMap temp;
|
||||
temp.convertFromMemoizedJson(json.object(), it.value());
|
||||
if (it.value().isEmpty()) { temp.convertFromMemoizedJson(json.object()); }
|
||||
const QString messagePrefix = QStringLiteral("Parsing %1.json").arg(it.key());
|
||||
auto messages = temp.convertFromMemoizedJsonNoThrow(json.object(), it.value(), this, messagePrefix);
|
||||
if (it.value().isEmpty()) { messages.push_back(temp.convertFromMemoizedJsonNoThrow(json.object(), this, messagePrefix)); }
|
||||
if (! messages.isEmpty())
|
||||
{
|
||||
ok = false;
|
||||
CLogMessage::preformatted(messages);
|
||||
}
|
||||
temp.removeDuplicates(currentValues);
|
||||
o_values.insert(temp, QFileInfo(file).lastModified().toMSecsSinceEpoch());
|
||||
}
|
||||
return CStatusMessage(this).info("Loaded cache values %1 from %2") <<
|
||||
(keysMessage.isEmpty() ? o_values.keys().to<QStringList>().join(",") : keysMessage) << dir;
|
||||
return CStatusMessage(this).info("Loaded cache values %1 from %2 %3") <<
|
||||
(keysMessage.isEmpty() ? o_values.keys().to<QStringList>().join(",") : keysMessage) << dir << (ok ? "successfully" : "with errors");
|
||||
}
|
||||
|
||||
void CValueCache::markAllAsSaved(const QString &keyPrefix)
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "blackmisc/propertyindex.h"
|
||||
#include "blackmisc/range.h"
|
||||
#include "blackmisc/slot.h"
|
||||
#include "blackmisc/statusmessage.h"
|
||||
#include "blackmisc/statusmessagelist.h"
|
||||
#include "blackmisc/valuecacheprivate.h"
|
||||
#include "blackmisc/variant.h"
|
||||
#include "blackmisc/variantmap.h"
|
||||
@@ -193,9 +193,14 @@ namespace BlackMisc
|
||||
|
||||
//! Load all values in Json format.
|
||||
//! Values already in the cache will remain in the cache unless they are overwritten.
|
||||
//! \throws BlackMisc::CJsonException if JSON schema validation fails.
|
||||
//! \threadsafe
|
||||
void loadFromJson(const QJsonObject &json);
|
||||
|
||||
//! Call loadFromJson, catch any CJsonException that are thrown and return them as CStatusMessage.
|
||||
//! \threadsafe
|
||||
CStatusMessageList loadFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix);
|
||||
|
||||
//! Save values to Json files in a given directory.
|
||||
//! If prefix is provided then only those values whose keys start with that prefix.
|
||||
//! \threadsafe
|
||||
|
||||
Reference in New Issue
Block a user