From f20f48999903cc515a935c00a52a5bca1b9b254f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=80lex=20Fiestas?= Date: Sat, 13 Sep 2014 00:49:56 +0200 Subject: [PATCH] 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). --- CMakeLists.txt | 3 +- core/CMakeLists.txt | 1 - core/networkpackage.cpp | 60 +++++++++++++++++++++++++++-------- core/networkpackage.h | 1 - interfaces/CMakeLists.txt | 2 -- tests/CMakeLists.txt | 2 -- tests/networkpackagetests.cpp | 4 +-- 7 files changed, 49 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 426394f86..0ed103784 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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(KF5 REQUIRED COMPONENTS I18n KIO Notifications ConfigWidgets DBusAddons KCMUtils KDELibs4Support) find_package(QCA2 REQUIRED) -find_package(QJSON REQUIRED) include(KDEInstallDirs) include(KDECompilerSettings) @@ -19,7 +18,7 @@ include(ECMInstallIcons) include(FeatureSummary) include(GenerateExportHeader) -include_directories(${CMAKE_CURRENT_BINARY_DIR} ${QJSON_INCLUDE_DIR}) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) add_subdirectory(core) add_subdirectory(kcm) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index ab2bc6298..090d4fbc0 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -36,7 +36,6 @@ PUBLIC KF5::KDELibs4Support PRIVATE ${QCA2_LIBRARIES} - ${QJSON_LIBRARIES} KF5::KIOWidgets KF5::KCMUtils Qt5::Network diff --git a/core/networkpackage.cpp b/core/networkpackage.cpp index 330850ce9..25fc971da 100644 --- a/core/networkpackage.cpp +++ b/core/networkpackage.cpp @@ -23,14 +23,15 @@ #include #include +#include +#include #include #include #include #include #include +#include #include -#include -#include #include "filetransferjob.h" @@ -62,6 +63,18 @@ void NetworkPackage::createIdentityPackage(NetworkPackage* np) //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 { //Object -> QVariant @@ -69,7 +82,7 @@ QByteArray NetworkPackage::serialize() const //variant["id"] = mId; //variant["type"] = mType; //variant["body"] = mBody; - QVariantMap variant = QJson::QObjectHelper::qobject2qvariant(this); + QVariantMap variant = qobject2qvairant(this); if (hasPayload()) { //kDebug(kdeconnect_kded()) << "Serializing payloadTransferInfo"; @@ -79,10 +92,10 @@ QByteArray NetworkPackage::serialize() const //QVariant -> json bool ok; - QJson::Serializer serializer; - QByteArray json = serializer.serialize(variant,&ok); - if (!ok) { - kDebug(debugArea()) << "Serialization error:" << serializer.errorMessage(); + auto jsonDocument = QJsonDocument::fromVariant(variant); + QByteArray json = jsonDocument.toJson(QJsonDocument::Compact); + if (json.isEmpty()) { + kDebug(debugArea()) << "Serialization error:"; } else { if (!isEncrypted()) { //kDebug(kDebugArea) << "Serialized package:" << json; @@ -93,19 +106,38 @@ QByteArray NetworkPackage::serialize() const 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) { //Json -> QVariant - QJson::Parser parser; - bool ok; - QVariantMap variant = parser.parse(a, &ok).toMap(); - if (!ok) { - kDebug(debugArea()) << "Unserialization error:" << a; + QJsonParseError parseError; + auto parser = QJsonDocument::fromJson(a, &parseError); + if (parser.isNull()) { + kDebug(debugArea()) << "Unserialization error:" << parseError.errorString(); return false; } - //QVariant -> Object - QJson::QObjectHelper::qvariant2qobject(variant, np); + auto variant = parser.toVariant().toMap(); + qvariant2qobject(variant, np); if (!np->isEncrypted()) { //kDebug(kDebugArea) << "Unserialized: " << a; diff --git a/core/networkpackage.h b/core/networkpackage.h index ed26676de..33737d915 100644 --- a/core/networkpackage.h +++ b/core/networkpackage.h @@ -30,7 +30,6 @@ #include #include #include -#include #include #include "kdeconnectcore_export.h" diff --git a/interfaces/CMakeLists.txt b/interfaces/CMakeLists.txt index db20a4b0b..ec59957e0 100644 --- a/interfaces/CMakeLists.txt +++ b/interfaces/CMakeLists.txt @@ -72,8 +72,6 @@ target_link_libraries(kdeconnectinterfaces Qt5::Core Qt5::DBus KF5::KDELibs4Support - - ${QJSON_LIBRARIES} ) configure_file(KDEConnectConfig.cmake.in ${CMAKE_BINARY_DIR}/interfaces/KDEConnectConfig.cmake @ONLY) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 70c1ce083..8a631535b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,4 @@ include_directories( - ${QJSON_INCLUDE_DIR} ${QCA2_INCLUDE_DIR} ${KDEConnectCore_BINARY_DIR} ${CMAKE_SOURCE_DIR} @@ -8,7 +7,6 @@ include_directories( set(kdeconnect_libraries KF5::KIOWidgets Qt5::Network - ${QJSON_LIBRARIES} ${QCA2_LIBRARIES} Qt5::Test kdeconnectcore diff --git a/tests/networkpackagetests.cpp b/tests/networkpackagetests.cpp index 35e14bfcf..d21397529 100644 --- a/tests/networkpackagetests.cpp +++ b/tests/networkpackagetests.cpp @@ -66,7 +66,7 @@ void NetworkPackageTests::networkPackageTest() QCOMPARE( np.type(), np2.type() ); 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; NetworkPackage::unserialize(json,&np2); QCOMPARE( np2.id(), QString("123") ); @@ -129,7 +129,7 @@ void NetworkPackageTests::networkPackageEncryptionTest() //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_PKCS1v15 maximumEncryptSize" << publicKey.maximumEncryptSize(QCA::EME_PKCS1v15); QCOMPARE( json.size() > publicKey.maximumEncryptSize(NetworkPackage::EncryptionAlgorithm), true );