refs #911, utility functions for cut and paste JSON

This commit is contained in:
Klaus Basan
2017-03-23 01:32:49 +01:00
committed by Mathew Sutcliffe
parent 6c752b81f5
commit b785e32256
3 changed files with 25 additions and 4 deletions

View File

@@ -126,7 +126,7 @@ namespace BlackCore
CLogMessage(this).error("Authentication failed, no response from %1") << urlString; CLogMessage(this).error("Authentication failed, no response from %1") << urlString;
return; return;
} }
if (!json.startsWith('{') || !json.endsWith('}')) if (!Json::looksLikeJson(json))
{ {
CLogMessage(this).error("Illegal JSON object: %1") << CNetworkUtils::removeHtmlPartsFromPhpErrorMessage(json); CLogMessage(this).error("Illegal JSON object: %1") << CNetworkUtils::removeHtmlPartsFromPhpErrorMessage(json);
return; return;

View File

@@ -56,7 +56,7 @@ const QJsonValue &operator >>(const QJsonValue &json, QString &value)
const QJsonValue &operator >>(const QJsonValue &json, QStringList &value) const QJsonValue &operator >>(const QJsonValue &json, QStringList &value)
{ {
for (auto &&element : json.toArray()) { value << element.toString(); } for (auto && element : json.toArray()) { value << element.toString(); }
return json; return json;
} }
@@ -130,7 +130,7 @@ const QJsonValueRef &operator >>(const QJsonValueRef &json, QString &value)
const QJsonValueRef &operator >>(const QJsonValueRef &json, QStringList &value) const QJsonValueRef &operator >>(const QJsonValueRef &json, QStringList &value)
{ {
for (auto &&element : json.toArray()) { value << element.toString(); } for (auto && element : json.toArray()) { value << element.toString(); }
return json; return json;
} }
@@ -404,7 +404,7 @@ namespace BlackMisc
{ {
if (toBeAppended.isEmpty()) return target; if (toBeAppended.isEmpty()) return target;
QStringList keys = toBeAppended.keys(); QStringList keys = toBeAppended.keys();
foreach(const QString & key, keys) foreach (const QString &key, keys)
{ {
target.insert(key, toBeAppended.value(key)); target.insert(key, toBeAppended.value(key));
} }
@@ -449,5 +449,18 @@ namespace BlackMisc
} }
return currentObject; return currentObject;
} }
bool looksLikeJson(const QString &json)
{
if (json.isEmpty()) { return false; }
const QString t = json.trimmed();
return t.startsWith('{') && t.endsWith('}');
}
bool looksLikeSwiftJson(const QString &json)
{
// further checks would go here
return looksLikeJson(json);
}
} }
} }

View File

@@ -250,6 +250,14 @@ namespace BlackMisc
//! Merges an incremental json object into an existing one //! Merges an incremental json object into an existing one
BLACKMISC_EXPORT QJsonObject applyIncrementalObject(const QJsonObject &previousObject, const QJsonObject &incrementalObject); BLACKMISC_EXPORT QJsonObject applyIncrementalObject(const QJsonObject &previousObject, const QJsonObject &incrementalObject);
//! Looks like swift JSON?
//! \remark Quick check if the string could be a valid JSON string
BLACKMISC_EXPORT bool looksLikeJson(const QString &json);
//! Looks like swift JSON?
//! \remark Quick check if the string could be a valid swift JSON string
BLACKMISC_EXPORT bool looksLikeSwiftJson(const QString &json);
/*! /*!
* Load JSON file and init by that * Load JSON file and init by that
*/ */