mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 00:16:51 +08:00
Ref T237, JSON functions
* container file JSON function automatically detecting JSON format (swift, cache, DB) * utility functions
This commit is contained in:
@@ -147,7 +147,7 @@ namespace BlackMisc
|
||||
//! Assign from JSON object string
|
||||
void convertFromJson(const QString &jsonString)
|
||||
{
|
||||
this->convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString));
|
||||
this->convertFromJson(Json::jsonObjectFromString(jsonString));
|
||||
}
|
||||
|
||||
//! Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
|
||||
|
||||
@@ -195,6 +195,42 @@ namespace BlackMisc
|
||||
return count;
|
||||
}
|
||||
|
||||
template<class OBJ, class CONTAINER, typename KEYTYPE>
|
||||
CONTAINER IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::fromMultipleJsonFormats(const QJsonObject &jsonObject)
|
||||
{
|
||||
// also accept cache format
|
||||
if (jsonObject.isEmpty())
|
||||
{
|
||||
const CONTAINER c;
|
||||
return c;
|
||||
}
|
||||
|
||||
if (Json::looksLikeSwiftDataObject(jsonObject))
|
||||
{
|
||||
const QJsonObject cacheObj = Json::swiftDataObjectValue(jsonObject);
|
||||
// swift own format
|
||||
CONTAINER container;
|
||||
container.convertFromJson(cacheObj);
|
||||
return container;
|
||||
}
|
||||
|
||||
if (Json::looksLikeSwiftContainerJson(jsonObject))
|
||||
{
|
||||
CONTAINER container;
|
||||
container.convertFromJson(jsonObject);
|
||||
return container;
|
||||
}
|
||||
|
||||
if (jsonObject.contains("data"))
|
||||
{
|
||||
// DB format
|
||||
return IDatastoreObjectList::fromDatabaseJson(jsonObject.value("data").toArray());
|
||||
}
|
||||
|
||||
// no idea what this is
|
||||
throw CJsonException("Unsupported JSON format");
|
||||
}
|
||||
|
||||
template<class OBJ, class CONTAINER, typename KEYTYPE>
|
||||
QDateTime IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::latestDbTimestamp() const
|
||||
{
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#define BLACKMISC_DB_DATABASEOBJECTLIST_H
|
||||
|
||||
#include "blackmisc/timestampobjectlist.h"
|
||||
#include "blackmisc/jsonexception.h"
|
||||
#include <QJsonArray>
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
@@ -65,6 +66,10 @@ namespace BlackMisc
|
||||
//! Number of entries with valid DB key
|
||||
int countWithValidDbKey() const;
|
||||
|
||||
//! From multiple JSON formats
|
||||
//! \remark supports native swift C++ format, DB format, and cache format
|
||||
static CONTAINER fromMultipleJsonFormats(const QJsonObject &jsonObject);
|
||||
|
||||
//! From DB JSON with default prefixes
|
||||
//! \remark Specialized classes might have their own fromDatabaseJson implementation
|
||||
static CONTAINER fromDatabaseJson(const QJsonArray &array);
|
||||
|
||||
@@ -473,7 +473,7 @@ namespace BlackMisc
|
||||
|
||||
QJsonObject swiftDataObjectValue(const QJsonObject &object)
|
||||
{
|
||||
if (object.size() != 1) { return 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"))
|
||||
@@ -489,5 +489,11 @@ namespace BlackMisc
|
||||
if (obj.isEmpty()) { return obj; }
|
||||
return swiftDataObjectValue(obj);
|
||||
}
|
||||
|
||||
bool looksLikeSwiftContainerJson(const QJsonObject &object)
|
||||
{
|
||||
// CContainerbase::convertFromJson
|
||||
return object.contains("containerbase");
|
||||
}
|
||||
} // ns
|
||||
} // ns
|
||||
|
||||
@@ -257,6 +257,9 @@ 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 valid swift container JSON object
|
||||
BLACKMISC_EXPORT bool looksLikeSwiftContainerJson(const QJsonObject &object);
|
||||
|
||||
//! Looks like a cache/setting object
|
||||
BLACKMISC_EXPORT bool looksLikeSwiftDataObject(const QJsonObject &object);
|
||||
|
||||
|
||||
@@ -71,16 +71,16 @@ namespace BlackMisc
|
||||
static QString flagToString(EntityFlag flag);
|
||||
|
||||
//! Convert to string
|
||||
static QString flagToString(BlackMisc::Network::CEntityFlags::Entity flag);
|
||||
static QString flagToString(CEntityFlags::Entity flag);
|
||||
|
||||
//! Representing single entity?
|
||||
static bool isSingleEntity(BlackMisc::Network::CEntityFlags::Entity flag);
|
||||
static bool isSingleEntity(CEntityFlags::Entity flag);
|
||||
|
||||
//! Any finished state
|
||||
static bool isFinishedReadState(ReadState state);
|
||||
|
||||
//! Represented number of entities
|
||||
static int numberOfEntities(BlackMisc::Network::CEntityFlags::Entity flag);
|
||||
static int numberOfEntities(CEntityFlags::Entity flag);
|
||||
|
||||
//! Convert to string
|
||||
static QString flagToString(ReadState flag);
|
||||
|
||||
Reference in New Issue
Block a user