Ref T304, "no throw" JSON functions

This commit is contained in:
Klaus Basan
2018-08-12 19:14:31 +02:00
parent cfbc0d4c35
commit 30ba404043
7 changed files with 65 additions and 3 deletions

View File

@@ -123,6 +123,7 @@ namespace BlackMisc
}
//! \copydoc BlackMisc::Mixin::JsonByMetaClass::convertFromJson
//! \throws CJsonException
void convertFromJson(const QJsonObject &json)
{
derived().clear();
@@ -167,6 +168,25 @@ namespace BlackMisc
return obj;
}
//! Static version of convertFromJson
static Derived fromJsonNoThrow(const QString &jsonString, bool acceptCacheJson, bool &success, QString &errMsg)
{
success = false;
Derived obj;
try
{
if (jsonString.isEmpty()) { return obj; }
const QJsonObject jsonObj = acceptCacheJson ? Json::swiftDataObjectValue(jsonString) : Json::jsonObjectFromString(jsonString);
obj.convertFromJson(jsonObj);
success = true;
}
catch (const CJsonException &ex)
{
errMsg = ex.toString("JSON conversion");
}
return obj;
}
//! Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
CStatusMessage convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix); // implemented in statusmessage.h