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

@@ -463,11 +463,32 @@ namespace BlackMisc
static DerivedObj fromJson(const QString &jsonString, bool acceptCacheJson = false)
{
DerivedObj obj;
if (jsonString.isEmpty()) { return obj; }
const QJsonObject jsonObj = acceptCacheJson ? Json::swiftDataObjectValue(jsonString) : Json::jsonObjectFromString(jsonString);
obj.convertFromJson(jsonObj);
return obj;
}
//! Get object from JSON string
template<class DerivedObj = Derived>
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;
}
private:
const Derived *derived() const { return static_cast<const Derived *>(this); }
Derived *derived() { return static_cast<Derived *>(this); }