mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-10 05:55:33 +08:00
Ref T304, "no throw" JSON functions
This commit is contained in:
@@ -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); }
|
||||
|
||||
Reference in New Issue
Block a user