mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 23:05:36 +08:00
Ref T199, also cache files can be loaded
* unwrap cache JSON object * parameter to control if cache format is allowed
This commit is contained in:
@@ -215,10 +215,10 @@ namespace BlackCore
|
||||
return url;
|
||||
}
|
||||
|
||||
CGlobalSetup CGlobalSetup::fromJsonFile(const QString &fileNameAndPath)
|
||||
CGlobalSetup CGlobalSetup::fromJsonFile(const QString &fileNameAndPath, bool acceptCacheFormat)
|
||||
{
|
||||
CGlobalSetup setup;
|
||||
loadFromJsonFile(setup, fileNameAndPath);
|
||||
loadFromJsonFile(setup, fileNameAndPath, acceptCacheFormat);
|
||||
return setup;
|
||||
}
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@ namespace BlackCore
|
||||
static BlackMisc::Network::CUrl buildDbDataDirectoryUrl(const BlackMisc::Network::CUrl &candidate);
|
||||
|
||||
//! Object initialized by JSON file
|
||||
static CGlobalSetup fromJsonFile(const QString &fileNameAndPath);
|
||||
static CGlobalSetup fromJsonFile(const QString &fileNameAndPath, bool acceptCacheFormat);
|
||||
|
||||
private:
|
||||
bool m_wasLoaded = false; //!< Loaded from web
|
||||
|
||||
@@ -386,24 +386,24 @@ namespace BlackMisc
|
||||
{
|
||||
namespace Json
|
||||
{
|
||||
QJsonObject jsonObjectFromString(const QString &json)
|
||||
QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat)
|
||||
{
|
||||
if (json.isEmpty()) { return QJsonObject();}
|
||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
||||
return jsonDoc.object();
|
||||
const QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
||||
return acceptCacheFormat ? Json::swiftDataObjectValue(jsonDoc.object()) : jsonDoc.object();
|
||||
}
|
||||
|
||||
QJsonArray jsonArrayFromString(const QString &json)
|
||||
{
|
||||
if (json.isEmpty()) { return QJsonArray();}
|
||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
||||
const QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
||||
return jsonDoc.array();
|
||||
}
|
||||
|
||||
QJsonObject &appendJsonObject(QJsonObject &target, const QJsonObject &toBeAppended)
|
||||
{
|
||||
if (toBeAppended.isEmpty()) return target;
|
||||
QStringList keys = toBeAppended.keys();
|
||||
const QStringList keys = toBeAppended.keys();
|
||||
foreach (const QString &key, keys)
|
||||
{
|
||||
target.insert(key, toBeAppended.value(key));
|
||||
@@ -462,5 +462,32 @@ namespace BlackMisc
|
||||
// further checks would go here
|
||||
return looksLikeJson(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool looksLikeSwiftDataObject(const QJsonObject &object)
|
||||
{
|
||||
if (object.size() != 1) { return false; }
|
||||
const QString key = object.keys().front();
|
||||
const QJsonObject cacheObject = object.value(key).toObject();
|
||||
return (cacheObject.contains("type") && cacheObject.contains("value"));
|
||||
}
|
||||
|
||||
QJsonObject swiftDataObjectValue(const QJsonObject &object)
|
||||
{
|
||||
if (object.size() != 1) { return object; }
|
||||
const QString key = object.keys().front();
|
||||
const QJsonObject cacheObject = object.value(key).toObject();
|
||||
if (cacheObject.contains("type") && cacheObject.contains("value"))
|
||||
{
|
||||
return cacheObject.value("value").toObject();
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
QJsonObject swiftDataObjectValue(const QString &jsonString)
|
||||
{
|
||||
const QJsonObject obj = jsonObjectFromString(jsonString);
|
||||
if (obj.isEmpty()) { return obj; }
|
||||
return swiftDataObjectValue(obj);
|
||||
}
|
||||
} // ns
|
||||
} // ns
|
||||
|
||||
@@ -237,7 +237,7 @@ namespace BlackMisc
|
||||
|
||||
//! JSON Object from string
|
||||
//! \ingroup JSON
|
||||
BLACKMISC_EXPORT QJsonObject jsonObjectFromString(const QString &json);
|
||||
BLACKMISC_EXPORT QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat = false);
|
||||
|
||||
//! JSON Array from string
|
||||
//! \ingroup JSON
|
||||
@@ -257,15 +257,26 @@ namespace BlackMisc
|
||||
//! \remark Quick check if the string could be a valid swift JSON string
|
||||
BLACKMISC_EXPORT bool looksLikeSwiftJson(const QString &json);
|
||||
|
||||
//! Looks like a cache/setting object
|
||||
BLACKMISC_EXPORT bool looksLikeSwiftDataObject(const QJsonObject &object);
|
||||
|
||||
//! The value of a cache/setting object
|
||||
//! \remark if data object unstrip from that, otherwise leave unchanged
|
||||
BLACKMISC_EXPORT QJsonObject swiftDataObjectValue(const QJsonObject &object);
|
||||
|
||||
//! The value of a cache/setting object
|
||||
//! \remark if data object unstrip from that, otherwise leave unchanged
|
||||
BLACKMISC_EXPORT QJsonObject swiftDataObjectValue(const QString &jsonString);
|
||||
|
||||
/*!
|
||||
* Load JSON file and init by that
|
||||
*/
|
||||
template <class T>
|
||||
bool loadFromJsonFile(T &object, const QString &fileNameAndPath)
|
||||
bool loadFromJsonFile(T &object, const QString &fileNameAndPath, bool acceptCacheFormat = false)
|
||||
{
|
||||
const QString jsonString(CFileUtils::readFileToString(fileNameAndPath));
|
||||
if (jsonString.isEmpty()) { return false; }
|
||||
object.convertFromJson(jsonString);
|
||||
object.convertFromJson(jsonString, acceptCacheFormat);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -386,9 +397,10 @@ namespace BlackMisc
|
||||
}
|
||||
|
||||
//! Assign from JSON object string
|
||||
void convertFromJson(const QString &jsonString)
|
||||
void convertFromJson(const QString &jsonString, bool acceptCacheFormat = false)
|
||||
{
|
||||
convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString));
|
||||
const QJsonObject jsonObject = BlackMisc::Json::jsonObjectFromString(jsonString, acceptCacheFormat);
|
||||
convertFromJson(jsonObject);
|
||||
}
|
||||
|
||||
//! Get object from QJsonObject
|
||||
@@ -402,10 +414,11 @@ namespace BlackMisc
|
||||
|
||||
//! Get object from JSON string
|
||||
template<class DerivedObj = Derived>
|
||||
static DerivedObj fromJson(const QString &jsonString)
|
||||
static DerivedObj fromJson(const QString &jsonString, bool acceptCacheJson = false)
|
||||
{
|
||||
DerivedObj obj;
|
||||
obj.convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString));
|
||||
const QJsonObject jsonObj = acceptCacheJson ? Json::swiftDataObjectValue(jsonString) : Json::jsonObjectFromString(jsonString);
|
||||
obj.convertFromJson(jsonObj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user