mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 15:15:50 +08:00
refs #815 Added convertFromJsonNoThrow methods which catch CJsonException and return CStatusMessage.
This commit is contained in:
@@ -142,6 +142,12 @@ namespace BlackMisc
|
||||
this->convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString));
|
||||
}
|
||||
|
||||
//! 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
|
||||
|
||||
//! Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
|
||||
CStatusMessage convertFromJsonNoThrow(const QString &jsonString, const CLogCategoryList &categories, const QString &prefix); // implemented in statusmessage.h
|
||||
|
||||
//! \copydoc BlackMisc::Mixin::String::toQString
|
||||
QString convertToQString(bool i18n = false) const
|
||||
{
|
||||
|
||||
@@ -336,8 +336,37 @@ namespace BlackMisc
|
||||
BLACK_METAMEMBER(timestampMSecsSinceEpoch)
|
||||
);
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// CContainerBase methods implemented out-of-line to avoid circular include
|
||||
template <template <class> class C, class T, class CIt>
|
||||
CStatusMessage CContainerBase<C, T, CIt>::convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
try
|
||||
{
|
||||
convertFromJson(json);
|
||||
}
|
||||
catch (const CJsonException &ex)
|
||||
{
|
||||
return ex.toStatusMessage(categories, prefix);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
//! Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
|
||||
template <template <class> class C, class T, class CIt>
|
||||
CStatusMessage CContainerBase<C, T, CIt>::convertFromJsonNoThrow(const QString &jsonString, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
try
|
||||
{
|
||||
convertFromJson(jsonString);
|
||||
}
|
||||
catch (const CJsonException &ex)
|
||||
{
|
||||
return ex.toStatusMessage(categories, prefix);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Q_DECLARE_METATYPE(BlackMisc::CStatusMessage)
|
||||
Q_DECLARE_METATYPE(BlackMisc::CStatusMessage::StatusSeverity)
|
||||
|
||||
@@ -210,6 +210,19 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
CStatusMessage CVariant::convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
try
|
||||
{
|
||||
convertFromJson(json);
|
||||
}
|
||||
catch (const CJsonException &ex)
|
||||
{
|
||||
return ex.toStatusMessage(categories, prefix);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
QJsonObject CVariant::toMemoizedJson() const
|
||||
{
|
||||
auto *meta = getValueObjectMetaInfo();
|
||||
@@ -258,6 +271,19 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
CStatusMessage CVariant::convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
try
|
||||
{
|
||||
convertFromMemoizedJson(json);
|
||||
}
|
||||
catch (const CJsonException &ex)
|
||||
{
|
||||
return ex.toStatusMessage(categories, prefix);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
uint CVariant::getValueHash() const
|
||||
{
|
||||
switch (m_v.type())
|
||||
|
||||
@@ -277,12 +277,18 @@ namespace BlackMisc
|
||||
//! \copydoc BlackMisc::Mixin::JsonByMetaClass::convertFromJson
|
||||
void convertFromJson(const QJsonObject &json);
|
||||
|
||||
//! Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
|
||||
CStatusMessage convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix);
|
||||
|
||||
//! To compact JSON format.
|
||||
QJsonObject toMemoizedJson() const;
|
||||
|
||||
//! From compact JSON format.
|
||||
void convertFromMemoizedJson(const QJsonObject &json);
|
||||
|
||||
//! Call convertFromMemoizedJson, catch any CJsonException that is thrown and return it as CStatusMessage.
|
||||
CStatusMessage convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix);
|
||||
|
||||
//! \copydoc BlackMisc::Mixin::DBusByMetaClass::marshallToDbus
|
||||
void marshallToDbus(QDBusArgument &argument) const;
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
*/
|
||||
|
||||
#include "blackmisc/variantmap.h"
|
||||
#include "blackmisc/jsonexception.h"
|
||||
#include "blackmisc/statusmessagelist.h"
|
||||
|
||||
#include <QJsonValue>
|
||||
|
||||
@@ -100,4 +102,69 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
CStatusMessageList CVariantMap::convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
CStatusMessageList messages;
|
||||
clear();
|
||||
for (auto it = json.begin(); it != json.end(); ++it)
|
||||
{
|
||||
const QString key = it.key();
|
||||
CJsonScope scope(key);
|
||||
CVariant value;
|
||||
auto message = value.convertFromJsonNoThrow(it.value().toObject(), categories, prefix);
|
||||
if (message.isSuccess()) { implementationOf(*this).insert(cend(), key, value); }
|
||||
else { messages.push_back(message); }
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
CStatusMessageList CVariantMap::convertFromJsonNoThrow(const QJsonObject &json, const QStringList &keys, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
CStatusMessageList messages;
|
||||
clear();
|
||||
for (const auto &key : keys)
|
||||
{
|
||||
auto value = json.value(key);
|
||||
if (value.isUndefined()) { continue; }
|
||||
CJsonScope scope(key);
|
||||
CVariant var;
|
||||
auto message = var.convertFromJsonNoThrow(value.toObject(), categories, prefix);
|
||||
if (message.isSuccess()) { insert(key, var); }
|
||||
else { messages.push_back(message); }
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
CStatusMessageList CVariantMap::convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
CStatusMessageList messages;
|
||||
clear();
|
||||
for (auto it = json.begin(); it != json.end(); ++it)
|
||||
{
|
||||
const QString key = it.key();
|
||||
CJsonScope scope(key);
|
||||
CVariant value;
|
||||
auto message = value.convertFromMemoizedJsonNoThrow(it.value().toObject(), categories, prefix);
|
||||
if (message.isSuccess()) { implementationOf(*this).insert(cend(), key, value); }
|
||||
else { messages.push_back(message); }
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
CStatusMessageList CVariantMap::convertFromMemoizedJsonNoThrow(const QJsonObject &json, const QStringList &keys, const CLogCategoryList &categories, const QString &prefix)
|
||||
{
|
||||
CStatusMessageList messages;
|
||||
clear();
|
||||
for (const auto &key : keys)
|
||||
{
|
||||
auto value = json.value(key);
|
||||
if (value.isUndefined()) { continue; }
|
||||
CJsonScope scope(key);
|
||||
CVariant var;
|
||||
auto message = var.convertFromMemoizedJsonNoThrow(value.toObject(), categories, prefix);
|
||||
if (message.isSuccess()) { insert(key, var); }
|
||||
else { messages.push_back(message); }
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class CStatusMessageList;
|
||||
|
||||
/*!
|
||||
* Map of { QString, CVariant } pairs.
|
||||
*
|
||||
@@ -69,6 +71,12 @@ namespace BlackMisc
|
||||
//! Convert only keys present in list argument.
|
||||
void convertFromJson(const QJsonObject &json, const QStringList &keys);
|
||||
|
||||
//! Call convertFromJson, catch any CJsonException that are thrown and return them as CStatusMessage.
|
||||
CStatusMessageList convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix);
|
||||
|
||||
//! Call convertFromJson, catch any CJsonException that are thrown and return them as CStatusMessage.
|
||||
CStatusMessageList convertFromJsonNoThrow(const QJsonObject &json, const QStringList &keys, const CLogCategoryList &categories, const QString &prefix);
|
||||
|
||||
//! Insert values from this map into an existing compact JSON object.
|
||||
QJsonObject &mergeToMemoizedJson(QJsonObject &json) const;
|
||||
|
||||
@@ -81,6 +89,12 @@ namespace BlackMisc
|
||||
//! From compact JSON format.
|
||||
//! Convert only keys present in list argument.
|
||||
void convertFromMemoizedJson(const QJsonObject &json, const QStringList &keys);
|
||||
|
||||
//! Call convertFromMemoizedJson, catch any CJsonException that are thrown and return them as CStatusMessage.
|
||||
CStatusMessageList convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix);
|
||||
|
||||
//! Call convertFromMemoizedJson, catch any CJsonException that are thrown and return them as CStatusMessage.
|
||||
CStatusMessageList convertFromMemoizedJsonNoThrow(const QJsonObject &json, const QStringList &keys, const CLogCategoryList &categories, const QString &prefix);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user