Port to aelperay from QJson to native json support
Straight forward port from QJson to the native Json support. As a note I have added 2 helper functions that make the code bit more readable (object2qvariant and qvariatn2qobject).
This commit is contained in:
parent
d3aec4887f
commit
f20f489999
7 changed files with 49 additions and 24 deletions
|
@ -8,7 +8,6 @@ set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_SOURCE_DI
|
||||||
find_package(Qt5 5.2 REQUIRED COMPONENTS Quick Test)
|
find_package(Qt5 5.2 REQUIRED COMPONENTS Quick Test)
|
||||||
find_package(KF5 REQUIRED COMPONENTS I18n KIO Notifications ConfigWidgets DBusAddons KCMUtils KDELibs4Support)
|
find_package(KF5 REQUIRED COMPONENTS I18n KIO Notifications ConfigWidgets DBusAddons KCMUtils KDELibs4Support)
|
||||||
find_package(QCA2 REQUIRED)
|
find_package(QCA2 REQUIRED)
|
||||||
find_package(QJSON REQUIRED)
|
|
||||||
|
|
||||||
include(KDEInstallDirs)
|
include(KDEInstallDirs)
|
||||||
include(KDECompilerSettings)
|
include(KDECompilerSettings)
|
||||||
|
@ -19,7 +18,7 @@ include(ECMInstallIcons)
|
||||||
include(FeatureSummary)
|
include(FeatureSummary)
|
||||||
|
|
||||||
include(GenerateExportHeader)
|
include(GenerateExportHeader)
|
||||||
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${QJSON_INCLUDE_DIR})
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|
||||||
add_subdirectory(core)
|
add_subdirectory(core)
|
||||||
add_subdirectory(kcm)
|
add_subdirectory(kcm)
|
||||||
|
|
|
@ -36,7 +36,6 @@ PUBLIC
|
||||||
KF5::KDELibs4Support
|
KF5::KDELibs4Support
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${QCA2_LIBRARIES}
|
${QCA2_LIBRARIES}
|
||||||
${QJSON_LIBRARIES}
|
|
||||||
KF5::KIOWidgets
|
KF5::KIOWidgets
|
||||||
KF5::KCMUtils
|
KF5::KCMUtils
|
||||||
Qt5::Network
|
Qt5::Network
|
||||||
|
|
|
@ -23,14 +23,15 @@
|
||||||
#include <KSharedConfig>
|
#include <KSharedConfig>
|
||||||
#include <KConfigGroup>
|
#include <KConfigGroup>
|
||||||
|
|
||||||
|
#include <QMetaObject>
|
||||||
|
#include <QMetaProperty>
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
#include <QHostInfo>
|
#include <QHostInfo>
|
||||||
#include <QSslKey>
|
#include <QSslKey>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <qjsondocument.h>
|
||||||
#include <QtCrypto>
|
#include <QtCrypto>
|
||||||
#include <qjson/serializer.h>
|
|
||||||
#include <qjson/qobjecthelper.h>
|
|
||||||
|
|
||||||
#include "filetransferjob.h"
|
#include "filetransferjob.h"
|
||||||
|
|
||||||
|
@ -62,6 +63,18 @@ void NetworkPackage::createIdentityPackage(NetworkPackage* np)
|
||||||
//kDebug(kdeconnect_kded()) << "createIdentityPackage" << np->serialize();
|
//kDebug(kdeconnect_kded()) << "createIdentityPackage" << np->serialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVariantMap qobject2qvairant(const QObject* object)
|
||||||
|
{
|
||||||
|
QVariantMap map;
|
||||||
|
auto metaObject = object->metaObject();
|
||||||
|
for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i) {
|
||||||
|
const char *name = metaObject->property(i).name();
|
||||||
|
map.insert(QString::fromLatin1(name), object->property(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
QByteArray NetworkPackage::serialize() const
|
QByteArray NetworkPackage::serialize() const
|
||||||
{
|
{
|
||||||
//Object -> QVariant
|
//Object -> QVariant
|
||||||
|
@ -69,7 +82,7 @@ QByteArray NetworkPackage::serialize() const
|
||||||
//variant["id"] = mId;
|
//variant["id"] = mId;
|
||||||
//variant["type"] = mType;
|
//variant["type"] = mType;
|
||||||
//variant["body"] = mBody;
|
//variant["body"] = mBody;
|
||||||
QVariantMap variant = QJson::QObjectHelper::qobject2qvariant(this);
|
QVariantMap variant = qobject2qvairant(this);
|
||||||
|
|
||||||
if (hasPayload()) {
|
if (hasPayload()) {
|
||||||
//kDebug(kdeconnect_kded()) << "Serializing payloadTransferInfo";
|
//kDebug(kdeconnect_kded()) << "Serializing payloadTransferInfo";
|
||||||
|
@ -79,10 +92,10 @@ QByteArray NetworkPackage::serialize() const
|
||||||
|
|
||||||
//QVariant -> json
|
//QVariant -> json
|
||||||
bool ok;
|
bool ok;
|
||||||
QJson::Serializer serializer;
|
auto jsonDocument = QJsonDocument::fromVariant(variant);
|
||||||
QByteArray json = serializer.serialize(variant,&ok);
|
QByteArray json = jsonDocument.toJson(QJsonDocument::Compact);
|
||||||
if (!ok) {
|
if (json.isEmpty()) {
|
||||||
kDebug(debugArea()) << "Serialization error:" << serializer.errorMessage();
|
kDebug(debugArea()) << "Serialization error:";
|
||||||
} else {
|
} else {
|
||||||
if (!isEncrypted()) {
|
if (!isEncrypted()) {
|
||||||
//kDebug(kDebugArea) << "Serialized package:" << json;
|
//kDebug(kDebugArea) << "Serialized package:" << json;
|
||||||
|
@ -93,19 +106,38 @@ QByteArray NetworkPackage::serialize() const
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void qvariant2qobject(const QVariantMap& variant, QObject* object)
|
||||||
|
{
|
||||||
|
for ( QVariantMap::const_iterator iter = variant.begin(); iter != variant.end(); ++iter )
|
||||||
|
{
|
||||||
|
QVariant property = object->property( iter.key().toLatin1() );
|
||||||
|
Q_ASSERT( property.isValid() );
|
||||||
|
if ( property.isValid() )
|
||||||
|
{
|
||||||
|
QVariant value = iter.value();
|
||||||
|
if ( value.canConvert( property.type() ) )
|
||||||
|
{
|
||||||
|
value.convert( property.type() );
|
||||||
|
object->setProperty( iter.key().toLatin1(), value );
|
||||||
|
} else if ( QString( QLatin1String("QVariant") ).compare( QLatin1String( property.typeName() ) ) == 0) {
|
||||||
|
object->setProperty( iter.key().toLatin1(), value );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np)
|
bool NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np)
|
||||||
{
|
{
|
||||||
//Json -> QVariant
|
//Json -> QVariant
|
||||||
QJson::Parser parser;
|
QJsonParseError parseError;
|
||||||
bool ok;
|
auto parser = QJsonDocument::fromJson(a, &parseError);
|
||||||
QVariantMap variant = parser.parse(a, &ok).toMap();
|
if (parser.isNull()) {
|
||||||
if (!ok) {
|
kDebug(debugArea()) << "Unserialization error:" << parseError.errorString();
|
||||||
kDebug(debugArea()) << "Unserialization error:" << a;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//QVariant -> Object
|
auto variant = parser.toVariant().toMap();
|
||||||
QJson::QObjectHelper::qvariant2qobject(variant, np);
|
qvariant2qobject(variant, np);
|
||||||
|
|
||||||
if (!np->isEncrypted()) {
|
if (!np->isEncrypted()) {
|
||||||
//kDebug(kDebugArea) << "Unserialized: " << a;
|
//kDebug(kDebugArea) << "Unserialized: " << a;
|
||||||
|
|
|
@ -30,7 +30,6 @@
|
||||||
#include <QIODevice>
|
#include <QIODevice>
|
||||||
#include <QtCrypto>
|
#include <QtCrypto>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
#include <qjson/parser.h>
|
|
||||||
#include <KUrl>
|
#include <KUrl>
|
||||||
|
|
||||||
#include "kdeconnectcore_export.h"
|
#include "kdeconnectcore_export.h"
|
||||||
|
|
|
@ -72,8 +72,6 @@ target_link_libraries(kdeconnectinterfaces
|
||||||
Qt5::Core
|
Qt5::Core
|
||||||
Qt5::DBus
|
Qt5::DBus
|
||||||
KF5::KDELibs4Support
|
KF5::KDELibs4Support
|
||||||
|
|
||||||
${QJSON_LIBRARIES}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
configure_file(KDEConnectConfig.cmake.in ${CMAKE_BINARY_DIR}/interfaces/KDEConnectConfig.cmake @ONLY)
|
configure_file(KDEConnectConfig.cmake.in ${CMAKE_BINARY_DIR}/interfaces/KDEConnectConfig.cmake @ONLY)
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
include_directories(
|
include_directories(
|
||||||
${QJSON_INCLUDE_DIR}
|
|
||||||
${QCA2_INCLUDE_DIR}
|
${QCA2_INCLUDE_DIR}
|
||||||
${KDEConnectCore_BINARY_DIR}
|
${KDEConnectCore_BINARY_DIR}
|
||||||
${CMAKE_SOURCE_DIR}
|
${CMAKE_SOURCE_DIR}
|
||||||
|
@ -8,7 +7,6 @@ include_directories(
|
||||||
set(kdeconnect_libraries
|
set(kdeconnect_libraries
|
||||||
KF5::KIOWidgets
|
KF5::KIOWidgets
|
||||||
Qt5::Network
|
Qt5::Network
|
||||||
${QJSON_LIBRARIES}
|
|
||||||
${QCA2_LIBRARIES}
|
${QCA2_LIBRARIES}
|
||||||
Qt5::Test
|
Qt5::Test
|
||||||
kdeconnectcore
|
kdeconnectcore
|
||||||
|
|
|
@ -66,7 +66,7 @@ void NetworkPackageTests::networkPackageTest()
|
||||||
QCOMPARE( np.type(), np2.type() );
|
QCOMPARE( np.type(), np2.type() );
|
||||||
QCOMPARE( np.body(), np2.body() );
|
QCOMPARE( np.body(), np2.body() );
|
||||||
|
|
||||||
QByteArray json("{ \"id\": \"123\", \"type\": \"test\", \"body\": { \"testing\": true } }");
|
QByteArray json("{\"id\":\"123\",\"type\":\"test\",\"body\":{\"testing\":true}}");
|
||||||
//qDebug() << json;
|
//qDebug() << json;
|
||||||
NetworkPackage::unserialize(json,&np2);
|
NetworkPackage::unserialize(json,&np2);
|
||||||
QCOMPARE( np2.id(), QString("123") );
|
QCOMPARE( np2.id(), QString("123") );
|
||||||
|
@ -129,7 +129,7 @@ void NetworkPackageTests::networkPackageEncryptionTest()
|
||||||
|
|
||||||
//Test for long package encryption that need multi-chunk encryption
|
//Test for long package encryption that need multi-chunk encryption
|
||||||
|
|
||||||
QByteArray json = "{ \"body\" : { \"nowPlaying\" : \"A really long song name - A really long artist name\", \"player\" : \"A really long player name\", \"the_meaning_of_life_the_universe_and_everything\" : \"42\" }, \"id\" : \"A really long package id\", \"type\" : \"kdeconnect.a_really_really_long_package_type\" }\n";
|
QByteArray json = "{\"body\":{\"nowPlaying\":\"A really long song name - A really long artist name\",\"player\":\"A really long player name\",\"the_meaning_of_life_the_universe_and_everything\":\"42\"},\"id\":\"A really long package id\",\"type\":\"kdeconnect.a_really_really_long_package_type\"}\n";
|
||||||
qDebug() << "EME_PKCS1_OAEP maximumEncryptSize" << publicKey.maximumEncryptSize(QCA::EME_PKCS1_OAEP);
|
qDebug() << "EME_PKCS1_OAEP maximumEncryptSize" << publicKey.maximumEncryptSize(QCA::EME_PKCS1_OAEP);
|
||||||
qDebug() << "EME_PKCS1v15 maximumEncryptSize" << publicKey.maximumEncryptSize(QCA::EME_PKCS1v15);
|
qDebug() << "EME_PKCS1v15 maximumEncryptSize" << publicKey.maximumEncryptSize(QCA::EME_PKCS1v15);
|
||||||
QCOMPARE( json.size() > publicKey.maximumEncryptSize(NetworkPackage::EncryptionAlgorithm), true );
|
QCOMPARE( json.size() > publicKey.maximumEncryptSize(NetworkPackage::EncryptionAlgorithm), true );
|
||||||
|
|
Loading…
Reference in a new issue