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:
Klaus Basan
2018-01-13 00:29:39 +01:00
parent 922f12f141
commit 312d01d35b
4 changed files with 57 additions and 17 deletions

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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

View File

@@ -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;
}