mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 21:15:33 +08:00
Ref T286, JSON utility functions
This commit is contained in:
@@ -548,5 +548,89 @@ namespace BlackMisc
|
||||
{
|
||||
return (object.contains("data"));
|
||||
}
|
||||
|
||||
QString firstJsonValueAsString(const QString &json)
|
||||
{
|
||||
static const QString empty;
|
||||
if (json.isEmpty()) { return empty; }
|
||||
const QJsonObject obj = jsonObjectFromString(json);
|
||||
return firstJsonValueAsString(obj);
|
||||
}
|
||||
|
||||
QString firstJsonValueAsString(const QJsonObject &json)
|
||||
{
|
||||
static const QString empty;
|
||||
if (json.isEmpty()) { return empty; }
|
||||
const QStringList keys1 = json.keys();
|
||||
if (keys1.isEmpty()) { return empty; }
|
||||
if (keys1.contains("value", Qt::CaseInsensitive))
|
||||
{
|
||||
return json.value("value").toString();
|
||||
}
|
||||
|
||||
const QJsonObject jsonLevel1 = json.value(keys1.front()).toObject();
|
||||
return jsonLevel1.value("value").toString();
|
||||
}
|
||||
|
||||
QStringList firstJsonValueAsStringList(const QString &json)
|
||||
{
|
||||
static const QStringList empty;
|
||||
if (json.isEmpty()) { return empty; }
|
||||
const QJsonObject obj = jsonObjectFromString(json);
|
||||
return firstJsonValueAsStringList(obj);
|
||||
}
|
||||
|
||||
int firstJsonValueAsInt(const QString &json, int defaultValue, bool *ok)
|
||||
{
|
||||
if (ok) { *ok = false; }
|
||||
if (json.isEmpty()) { return defaultValue; }
|
||||
const QJsonObject obj = jsonObjectFromString(json);
|
||||
return firstJsonValueAsInt(obj, defaultValue, ok);
|
||||
}
|
||||
|
||||
int firstJsonValueAsInt(const QJsonObject &json, int defaultValue, bool *ok)
|
||||
{
|
||||
if (ok) { *ok = false; }
|
||||
if (json.isEmpty()) { return defaultValue; }
|
||||
const QStringList keys1 = json.keys();
|
||||
if (keys1.isEmpty()) { return defaultValue; }
|
||||
if (keys1.contains("value", Qt::CaseInsensitive))
|
||||
{
|
||||
if (ok) { *ok = true; }
|
||||
return json.value("value").toInt(defaultValue);
|
||||
}
|
||||
|
||||
const QJsonObject jsonLevel1 = json.value(keys1.front()).toObject();
|
||||
if (!jsonLevel1.contains("value")) { return defaultValue; }
|
||||
if (ok) { *ok = true; }
|
||||
return jsonLevel1.value("value").toInt(defaultValue);
|
||||
}
|
||||
|
||||
QStringList firstJsonValueAsStringList(const QJsonObject &json)
|
||||
{
|
||||
static const QStringList empty;
|
||||
if (json.isEmpty()) { return empty; }
|
||||
const QStringList keys1 = json.keys();
|
||||
if (keys1.isEmpty()) { return empty; }
|
||||
if (keys1.contains("value", Qt::CaseInsensitive))
|
||||
{
|
||||
return arrayToQStringList(json.value("value").toArray());
|
||||
}
|
||||
|
||||
const QJsonObject jsonLevel1 = json.value(keys1.front()).toObject();
|
||||
return arrayToQStringList(jsonLevel1.value("value").toArray());
|
||||
}
|
||||
|
||||
QStringList arrayToQStringList(const QJsonArray &array)
|
||||
{
|
||||
QStringList sl;
|
||||
if (array.isEmpty()) { return sl; }
|
||||
for (int i = 0; i < array.size(); i++)
|
||||
{
|
||||
if (!array.at(i).isString()) { continue; }
|
||||
sl.push_back(array.at(i).toString());
|
||||
}
|
||||
return sl;
|
||||
}
|
||||
} // ns
|
||||
} // ns
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QJsonValueRef>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QtGlobal>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
@@ -247,6 +247,27 @@ namespace BlackMisc
|
||||
//! \ingroup JSON
|
||||
BLACKMISC_EXPORT QJsonArray jsonArrayFromString(const QString &json);
|
||||
|
||||
//! First JSON string object marked as "value"
|
||||
BLACKMISC_EXPORT QString firstJsonValueAsString(const QString &json);
|
||||
|
||||
//! First JSON string object marked as "value"
|
||||
BLACKMISC_EXPORT QString firstJsonValueAsString(const QJsonObject &json);
|
||||
|
||||
//! First JSON string object marked as "value"
|
||||
BLACKMISC_EXPORT int firstJsonValueAsInt(const QString &json, int defaultValue = -1, bool *ok = nullptr);
|
||||
|
||||
//! First JSON string object marked as "value"
|
||||
BLACKMISC_EXPORT int firstJsonValueAsInt(const QJsonObject &json, int defaultValue = -1, bool *ok = nullptr);
|
||||
|
||||
//! First JSON string list object marked as "value"
|
||||
BLACKMISC_EXPORT QStringList firstJsonValueAsStringList(const QString &json);
|
||||
|
||||
//! First JSON string list object marked as "value"
|
||||
BLACKMISC_EXPORT QStringList firstJsonValueAsStringList(const QJsonObject &json);
|
||||
|
||||
//! JSON array to QStringList
|
||||
BLACKMISC_EXPORT QStringList arrayToQStringList(const QJsonArray &array);
|
||||
|
||||
//! Creates an incremental json object from two existing objects
|
||||
BLACKMISC_EXPORT QJsonObject getIncrementalObject(const QJsonObject &previousObject, const QJsonObject ¤tObject);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user