From 312d01d35b65b1fee4bf80162d78c366bef5cc8a Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sat, 13 Jan 2018 00:29:39 +0100 Subject: [PATCH] Ref T199, also cache files can be loaded * unwrap cache JSON object * parameter to control if cache format is allowed --- src/blackcore/data/globalsetup.cpp | 4 +-- src/blackcore/data/globalsetup.h | 2 +- src/blackmisc/json.cpp | 41 +++++++++++++++++++++++++----- src/blackmisc/json.h | 27 +++++++++++++++----- 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/blackcore/data/globalsetup.cpp b/src/blackcore/data/globalsetup.cpp index 3948db2fe..1b1b655d3 100644 --- a/src/blackcore/data/globalsetup.cpp +++ b/src/blackcore/data/globalsetup.cpp @@ -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; } diff --git a/src/blackcore/data/globalsetup.h b/src/blackcore/data/globalsetup.h index 37dae8d34..77e04d6ac 100644 --- a/src/blackcore/data/globalsetup.h +++ b/src/blackcore/data/globalsetup.h @@ -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 diff --git a/src/blackmisc/json.cpp b/src/blackmisc/json.cpp index 07ba3aa37..cae69b5a3 100644 --- a/src/blackmisc/json.cpp +++ b/src/blackmisc/json.cpp @@ -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 diff --git a/src/blackmisc/json.h b/src/blackmisc/json.h index b20e1291d..723b3e4d0 100644 --- a/src/blackmisc/json.h +++ b/src/blackmisc/json.h @@ -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 - 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 - 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; }