mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-30 22:29:13 +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
|
//! Assign from JSON object string
|
||||||
void convertFromJson(const QString &jsonString)
|
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.
|
//! Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
|
||||||
|
|||||||
@@ -195,6 +195,42 @@ namespace BlackMisc
|
|||||||
return count;
|
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>
|
template<class OBJ, class CONTAINER, typename KEYTYPE>
|
||||||
QDateTime IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::latestDbTimestamp() const
|
QDateTime IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::latestDbTimestamp() const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#define BLACKMISC_DB_DATABASEOBJECTLIST_H
|
#define BLACKMISC_DB_DATABASEOBJECTLIST_H
|
||||||
|
|
||||||
#include "blackmisc/timestampobjectlist.h"
|
#include "blackmisc/timestampobjectlist.h"
|
||||||
|
#include "blackmisc/jsonexception.h"
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
@@ -65,6 +66,10 @@ namespace BlackMisc
|
|||||||
//! Number of entries with valid DB key
|
//! Number of entries with valid DB key
|
||||||
int countWithValidDbKey() const;
|
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
|
//! From DB JSON with default prefixes
|
||||||
//! \remark Specialized classes might have their own fromDatabaseJson implementation
|
//! \remark Specialized classes might have their own fromDatabaseJson implementation
|
||||||
static CONTAINER fromDatabaseJson(const QJsonArray &array);
|
static CONTAINER fromDatabaseJson(const QJsonArray &array);
|
||||||
|
|||||||
@@ -473,7 +473,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
QJsonObject swiftDataObjectValue(const QJsonObject &object)
|
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 QString key = object.keys().front();
|
||||||
const QJsonObject cacheObject = object.value(key).toObject();
|
const QJsonObject cacheObject = object.value(key).toObject();
|
||||||
if (cacheObject.contains("type") && cacheObject.contains("value"))
|
if (cacheObject.contains("type") && cacheObject.contains("value"))
|
||||||
@@ -489,5 +489,11 @@ namespace BlackMisc
|
|||||||
if (obj.isEmpty()) { return obj; }
|
if (obj.isEmpty()) { return obj; }
|
||||||
return swiftDataObjectValue(obj);
|
return swiftDataObjectValue(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool looksLikeSwiftContainerJson(const QJsonObject &object)
|
||||||
|
{
|
||||||
|
// CContainerbase::convertFromJson
|
||||||
|
return object.contains("containerbase");
|
||||||
|
}
|
||||||
} // ns
|
} // ns
|
||||||
} // ns
|
} // ns
|
||||||
|
|||||||
@@ -257,6 +257,9 @@ 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 valid swift container JSON object
|
||||||
|
BLACKMISC_EXPORT bool looksLikeSwiftContainerJson(const QJsonObject &object);
|
||||||
|
|
||||||
//! Looks like a cache/setting object
|
//! Looks like a cache/setting object
|
||||||
BLACKMISC_EXPORT bool looksLikeSwiftDataObject(const QJsonObject &object);
|
BLACKMISC_EXPORT bool looksLikeSwiftDataObject(const QJsonObject &object);
|
||||||
|
|
||||||
|
|||||||
@@ -71,16 +71,16 @@ namespace BlackMisc
|
|||||||
static QString flagToString(EntityFlag flag);
|
static QString flagToString(EntityFlag flag);
|
||||||
|
|
||||||
//! Convert to string
|
//! Convert to string
|
||||||
static QString flagToString(BlackMisc::Network::CEntityFlags::Entity flag);
|
static QString flagToString(CEntityFlags::Entity flag);
|
||||||
|
|
||||||
//! Representing single entity?
|
//! Representing single entity?
|
||||||
static bool isSingleEntity(BlackMisc::Network::CEntityFlags::Entity flag);
|
static bool isSingleEntity(CEntityFlags::Entity flag);
|
||||||
|
|
||||||
//! Any finished state
|
//! Any finished state
|
||||||
static bool isFinishedReadState(ReadState state);
|
static bool isFinishedReadState(ReadState state);
|
||||||
|
|
||||||
//! Represented number of entities
|
//! Represented number of entities
|
||||||
static int numberOfEntities(BlackMisc::Network::CEntityFlags::Entity flag);
|
static int numberOfEntities(CEntityFlags::Entity flag);
|
||||||
|
|
||||||
//! Convert to string
|
//! Convert to string
|
||||||
static QString flagToString(ReadState flag);
|
static QString flagToString(ReadState flag);
|
||||||
|
|||||||
Reference in New Issue
Block a user