mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-08 03:35:35 +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;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
CGlobalSetup CGlobalSetup::fromJsonFile(const QString &fileNameAndPath)
|
CGlobalSetup CGlobalSetup::fromJsonFile(const QString &fileNameAndPath, bool acceptCacheFormat)
|
||||||
{
|
{
|
||||||
CGlobalSetup setup;
|
CGlobalSetup setup;
|
||||||
loadFromJsonFile(setup, fileNameAndPath);
|
loadFromJsonFile(setup, fileNameAndPath, acceptCacheFormat);
|
||||||
return setup;
|
return setup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ namespace BlackCore
|
|||||||
static BlackMisc::Network::CUrl buildDbDataDirectoryUrl(const BlackMisc::Network::CUrl &candidate);
|
static BlackMisc::Network::CUrl buildDbDataDirectoryUrl(const BlackMisc::Network::CUrl &candidate);
|
||||||
|
|
||||||
//! Object initialized by JSON file
|
//! Object initialized by JSON file
|
||||||
static CGlobalSetup fromJsonFile(const QString &fileNameAndPath);
|
static CGlobalSetup fromJsonFile(const QString &fileNameAndPath, bool acceptCacheFormat);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_wasLoaded = false; //!< Loaded from web
|
bool m_wasLoaded = false; //!< Loaded from web
|
||||||
|
|||||||
@@ -386,24 +386,24 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
namespace Json
|
namespace Json
|
||||||
{
|
{
|
||||||
QJsonObject jsonObjectFromString(const QString &json)
|
QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat)
|
||||||
{
|
{
|
||||||
if (json.isEmpty()) { return QJsonObject();}
|
if (json.isEmpty()) { return QJsonObject();}
|
||||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
const QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
||||||
return jsonDoc.object();
|
return acceptCacheFormat ? Json::swiftDataObjectValue(jsonDoc.object()) : jsonDoc.object();
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonArray jsonArrayFromString(const QString &json)
|
QJsonArray jsonArrayFromString(const QString &json)
|
||||||
{
|
{
|
||||||
if (json.isEmpty()) { return QJsonArray();}
|
if (json.isEmpty()) { return QJsonArray();}
|
||||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
const QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
|
||||||
return jsonDoc.array();
|
return jsonDoc.array();
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject &appendJsonObject(QJsonObject &target, const QJsonObject &toBeAppended)
|
QJsonObject &appendJsonObject(QJsonObject &target, const QJsonObject &toBeAppended)
|
||||||
{
|
{
|
||||||
if (toBeAppended.isEmpty()) return target;
|
if (toBeAppended.isEmpty()) return target;
|
||||||
QStringList keys = toBeAppended.keys();
|
const QStringList keys = toBeAppended.keys();
|
||||||
foreach (const QString &key, keys)
|
foreach (const QString &key, keys)
|
||||||
{
|
{
|
||||||
target.insert(key, toBeAppended.value(key));
|
target.insert(key, toBeAppended.value(key));
|
||||||
@@ -462,5 +462,32 @@ namespace BlackMisc
|
|||||||
// further checks would go here
|
// further checks would go here
|
||||||
return looksLikeJson(json);
|
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
|
//! JSON Object from string
|
||||||
//! \ingroup JSON
|
//! \ingroup JSON
|
||||||
BLACKMISC_EXPORT QJsonObject jsonObjectFromString(const QString &json);
|
BLACKMISC_EXPORT QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat = false);
|
||||||
|
|
||||||
//! JSON Array from string
|
//! JSON Array from string
|
||||||
//! \ingroup JSON
|
//! \ingroup JSON
|
||||||
@@ -257,15 +257,26 @@ namespace BlackMisc
|
|||||||
//! \remark Quick check if the string could be a valid swift JSON string
|
//! \remark Quick check if the string could be a valid swift JSON string
|
||||||
BLACKMISC_EXPORT bool looksLikeSwiftJson(const QString &json);
|
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
|
* Load JSON file and init by that
|
||||||
*/
|
*/
|
||||||
template <class T>
|
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));
|
const QString jsonString(CFileUtils::readFileToString(fileNameAndPath));
|
||||||
if (jsonString.isEmpty()) { return false; }
|
if (jsonString.isEmpty()) { return false; }
|
||||||
object.convertFromJson(jsonString);
|
object.convertFromJson(jsonString, acceptCacheFormat);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,9 +397,10 @@ namespace BlackMisc
|
|||||||
}
|
}
|
||||||
|
|
||||||
//! Assign from JSON object string
|
//! 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
|
//! Get object from QJsonObject
|
||||||
@@ -402,10 +414,11 @@ namespace BlackMisc
|
|||||||
|
|
||||||
//! Get object from JSON string
|
//! Get object from JSON string
|
||||||
template<class DerivedObj = Derived>
|
template<class DerivedObj = Derived>
|
||||||
static DerivedObj fromJson(const QString &jsonString)
|
static DerivedObj fromJson(const QString &jsonString, bool acceptCacheJson = false)
|
||||||
{
|
{
|
||||||
DerivedObj obj;
|
DerivedObj obj;
|
||||||
obj.convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString));
|
const QJsonObject jsonObj = acceptCacheJson ? Json::swiftDataObjectValue(jsonString) : Json::jsonObjectFromString(jsonString);
|
||||||
|
obj.convertFromJson(jsonObj);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user