mirror of
https://invent.kde.org/network/kdeconnect-kde.git
synced 2025-12-12 20:35:55 +01:00
Simplify NetworkPacket::unserialize
This commit is contained in:
@@ -62,40 +62,21 @@ QByteArray NetworkPacket::serialize() const
|
||||
return json;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void qjsonobject2qobject(const QJsonObject &obj, T *object)
|
||||
{
|
||||
for (auto iter = obj.begin(); iter != obj.end(); ++iter) {
|
||||
const int propertyIndex = T::staticMetaObject.indexOfProperty(iter.key().toLatin1().data());
|
||||
if (propertyIndex < 0) {
|
||||
qCWarning(KDECONNECT_CORE) << "missing property" << object << iter.key();
|
||||
continue;
|
||||
}
|
||||
|
||||
QMetaProperty property = T::staticMetaObject.property(propertyIndex);
|
||||
bool ret = property.writeOnGadget(object, iter.value().toVariant());
|
||||
if (!ret) {
|
||||
qCWarning(KDECONNECT_CORE) << "couldn't set" << object << "->" << property.name() << '=' << *iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool NetworkPacket::unserialize(const QByteArray &a, NetworkPacket *np)
|
||||
{
|
||||
// Json -> QVariant
|
||||
QJsonParseError parseError;
|
||||
auto parser = QJsonDocument::fromJson(a, &parseError);
|
||||
if (parser.isNull()) {
|
||||
QJsonDocument obj = QJsonDocument::fromJson(a, &parseError);
|
||||
if (obj.isNull()) {
|
||||
qCDebug(KDECONNECT_CORE) << "Unserialization error:" << parseError.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
const QJsonObject obj = parser.object();
|
||||
|
||||
qjsonobject2qobject(obj, np);
|
||||
|
||||
np->m_type = obj[QStringLiteral("type")].toString();
|
||||
np->m_body = obj[QStringLiteral("body")].toObject().toVariantMap();
|
||||
np->m_payloadSize = obj[QStringLiteral("payloadSize")].toInteger();
|
||||
// Will return an empty qvariantmap if was not present, which is ok
|
||||
np->m_payloadTransferInfo = obj.value(QLatin1String("payloadTransferInfo")).toVariant().toMap();
|
||||
np->m_payloadTransferInfo = obj[QLatin1String("payloadTransferInfo")].toObject().toVariantMap();
|
||||
|
||||
// Ids containing characters that are not allowed as dbus paths would make app crash
|
||||
if (np->m_body.contains(QStringLiteral("deviceId"))) {
|
||||
|
||||
Reference in New Issue
Block a user