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:
Àlex Fiestas 2014-09-13 00:49:56 +02:00
parent d3aec4887f
commit f20f489999
7 changed files with 49 additions and 24 deletions

View file

@ -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)

View file

@ -36,7 +36,6 @@ PUBLIC
KF5::KDELibs4Support
PRIVATE
${QCA2_LIBRARIES}
${QJSON_LIBRARIES}
KF5::KIOWidgets
KF5::KCMUtils
Qt5::Network

View file

@ -23,14 +23,15 @@
#include <KSharedConfig>
#include <KConfigGroup>
#include <QMetaObject>
#include <QMetaProperty>
#include <QByteArray>
#include <QDataStream>
#include <QHostInfo>
#include <QSslKey>
#include <QDateTime>
#include <qjsondocument.h>
#include <QtCrypto>
#include <qjson/serializer.h>
#include <qjson/qobjecthelper.h>
#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;

View file

@ -30,7 +30,6 @@
#include <QIODevice>
#include <QtCrypto>
#include <QSharedPointer>
#include <qjson/parser.h>
#include <KUrl>
#include "kdeconnectcore_export.h"

View file

@ -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)

View file

@ -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

View file

@ -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 );