Ref T264, Ref T263 simple approach to remember last directory (load/save) in view

* utility function "unwrapCache" to unwrap cache data
* fixed loading from file, also supporting memoized formats as well
* remember last directory
This commit is contained in:
Klaus Basan
2018-05-15 02:58:56 +02:00
parent 50a625d2bc
commit ba8b9a52da
4 changed files with 98 additions and 13 deletions

View File

@@ -392,7 +392,8 @@ namespace BlackMisc
{
if (json.isEmpty()) { return QJsonObject();}
const QJsonDocument jsonDoc(QJsonDocument::fromJson(json.toUtf8()));
return acceptCacheFormat ? Json::swiftDataObjectValue(jsonDoc.object()) : jsonDoc.object();
return acceptCacheFormat ? Json::unwrapCache(jsonDoc.object()) : jsonDoc.object();
// return acceptCacheFormat ? Json::swiftDataObjectValue(jsonDoc.object()) : jsonDoc.object();
}
QString stringFromJsonObject(const QJsonObject &jsonObject, QJsonDocument::JsonFormat format)
@@ -487,11 +488,28 @@ namespace BlackMisc
const QJsonObject cacheObject = object.value(key).toObject();
if (cacheObject.contains("type") && cacheObject.contains("value"))
{
const QString type = cacheObject.value("type").toString(); // just to verify in debugger
Q_UNUSED(type);
return cacheObject.value("value").toObject();
}
return object;
}
QJsonObject unwrapCache(const QJsonObject &object)
{
if (object.size() != 1) { return object; } // no cache format
const QString key = object.keys().front();
const QJsonObject cacheObject = object.value(key).toObject();
if (cacheObject.contains("type") && cacheObject.contains("value"))
{
// return object in form type/value
const QString type = cacheObject.value("type").toString(); // just to verify in debugger
Q_UNUSED(type);
return cacheObject;
}
return object;
}
QJsonObject swiftDataObjectValue(const QString &jsonString)
{
const QJsonObject obj = jsonObjectFromString(jsonString);
@@ -499,6 +517,14 @@ namespace BlackMisc
return swiftDataObjectValue(obj);
}
QJsonObject unwrapCache(const QString &jsonString)
{
const QJsonObject obj = jsonObjectFromString(jsonString);
if (obj.isEmpty()) { return obj; }
return unwrapCache(obj);
}
bool looksLikeSwiftContainerJson(const QJsonObject &object)
{
// CContainerbase::convertFromJson

View File

@@ -275,6 +275,16 @@ namespace BlackMisc
//! \remark if data object unstrip from that, otherwise leave unchanged
BLACKMISC_EXPORT QJsonObject swiftDataObjectValue(const QString &jsonString);
//! The value of a cache/setting object
//! \remark if cache object unstrip from that, otherwise leave unchanged
//! \remark format is type/value
BLACKMISC_EXPORT QJsonObject unwrapCache(const QJsonObject &object);
//! The value of a cache/setting object
//! \remark if cache object unstrip from that, otherwise leave unchanged
//! \remark format is type/value
BLACKMISC_EXPORT QJsonObject unwrapCache(const QString &jsonString);
/*!
* Load JSON file and init by that
*/