mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 04:25:35 +08:00
Ref T304, "no throw" JSON functions
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -393,7 +393,6 @@ namespace BlackMisc
|
||||
if (json.isEmpty()) { return QJsonObject();}
|
||||
const QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
||||
return acceptCacheFormat ? Json::unwrapCache(jsonDoc.object()) : jsonDoc.object();
|
||||
// return acceptCacheFormat ? Json::swiftDataObjectValue(jsonDoc.object()) : jsonDoc.object();
|
||||
}
|
||||
|
||||
QString stringFromJsonObject(const QJsonObject &jsonObject, QJsonDocument::JsonFormat format)
|
||||
|
||||
@@ -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); }
|
||||
|
||||
@@ -26,12 +26,20 @@ namespace BlackMisc
|
||||
|
||||
CStatusMessage CJsonException::toStatusMessage(const CLogCategoryList &categories, const QString &prefix) const
|
||||
{
|
||||
return CStatusMessage(categories).validationError("%1: %2 in '%3'") << prefix << what() << getStackTrace();
|
||||
return CStatusMessage(categories).validationError(toString(prefix));
|
||||
}
|
||||
|
||||
QString CJsonException::toString(const QString &prefix) const
|
||||
{
|
||||
static const QString s("%1 in '%2'");
|
||||
static const QString sp("%1: %2 in '%3'");
|
||||
if (prefix.isEmpty()) { return s.arg(what()).arg(getStackTrace()); }
|
||||
return sp.arg(prefix).arg(what()).arg(getStackTrace());
|
||||
}
|
||||
|
||||
void CJsonException::toLogMessage(const CLogCategoryList &categories, const QString &prefix) const
|
||||
{
|
||||
CLogMessage(categories).validationError("%1: %2 in '%3'") << prefix << what() << getStackTrace();
|
||||
CLogMessage(categories).validationError(toString(prefix));
|
||||
}
|
||||
|
||||
QString CJsonException::stackString()
|
||||
|
||||
@@ -36,6 +36,9 @@ namespace BlackMisc
|
||||
//! Get a status message representation.
|
||||
CStatusMessage toStatusMessage(const CLogCategoryList &categories, const QString &prefix) const;
|
||||
|
||||
//! As string info
|
||||
QString toString(const QString &prefix) const;
|
||||
|
||||
//! Write a message to the log.
|
||||
void toLogMessage(const CLogCategoryList &categories, const QString &prefix) const;
|
||||
|
||||
|
||||
@@ -208,6 +208,13 @@ namespace BlackMisc
|
||||
return cat;
|
||||
}
|
||||
|
||||
//! JSON and JSON conversions
|
||||
static const CLogCategory &json()
|
||||
{
|
||||
static const CLogCategory cat { "swift.json" };
|
||||
return cat;
|
||||
}
|
||||
|
||||
//! Startup of application
|
||||
static const CLogCategory &startup()
|
||||
{
|
||||
@@ -271,7 +278,10 @@ namespace BlackMisc
|
||||
dataInconsistency(),
|
||||
download(),
|
||||
driver(),
|
||||
flightPlan(),
|
||||
guiComponent(),
|
||||
interpolator(),
|
||||
json(),
|
||||
mapping(),
|
||||
matching(),
|
||||
modelLoader(),
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace BlackMisc
|
||||
{ "driver", exactMatch(CLogCategory::driver()) },
|
||||
{ "flight plan", exactMatch(CLogCategory::flightPlan()) },
|
||||
{ "interpolator", exactMatch(CLogCategory::interpolator()) },
|
||||
{ "JSON (conversion)", exactMatch(CLogCategory::json()) },
|
||||
{ "model cache", exactMatch(CLogCategory::modelCache()) },
|
||||
{ "model GUI", exactMatch(CLogCategory::modelGui()) },
|
||||
{ "model loader", exactMatch(CLogCategory::modelLoader()) },
|
||||
|
||||
Reference in New Issue
Block a user