1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-16 11:04:01 +02:00

Move JSON Qt4/5 wrapper into its own header

This commit is contained in:
Uwe L. Korn
2014-04-18 14:11:24 +01:00
parent be88e540d9
commit ffb04781f8
41 changed files with 206 additions and 113 deletions

View File

@@ -24,6 +24,7 @@
#include "utils/Closure.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"
#include "utils/Json.h"
#include "utils/TomahawkUtils.h"
#include <QtPlugin>

View File

@@ -29,6 +29,7 @@
#include <network/Servent.h>
#include <sip/SipInfo.h>
#include <sip/PeerInfo.h>
#include <utils/Json.h>
#include <utils/Logger.h>
#include <SourceList.h>

View File

@@ -26,7 +26,7 @@
#include "Typedefs.h"
#include "audio/AudioEngine.h"
#include "TomahawkSettings.h"
#include "utils/TomahawkUtils.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/TomahawkCache.h"
#include "utils/NetworkAccessManager.h"

View File

@@ -19,7 +19,7 @@
#include "DiscogsPlugin.h"
#include "utils/TomahawkUtils.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/Closure.h"
#include "utils/NetworkAccessManager.h"

View File

@@ -30,6 +30,7 @@
#include "TomahawkSettings.h"
#include "utils/TomahawkUtils.h"
#include "infosystem/InfoSystemWorker.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"
#include "Source.h"

View File

@@ -26,6 +26,7 @@
#include "Typedefs.h"
#include "audio/AudioEngine.h"
#include "TomahawkSettings.h"
#include "utils/Json.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include "utils/TomahawkCache.h"

View File

@@ -19,6 +19,7 @@
#include "RoviPlugin.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"

View File

@@ -29,6 +29,7 @@
#include "Typedefs.h"
#include "audio/AudioEngine.h"
#include "TomahawkSettings.h"
#include "utils/Json.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"

View File

@@ -25,6 +25,7 @@
#include "database/DatabaseCommand_AddClientAuth.h"
#include "database/DatabaseCommand_ClientAuthValid.h"
#include "network/Servent.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/TomahawkUtils.h"

View File

@@ -24,6 +24,7 @@
#include "audio/AudioEngine.h"
#include "resolvers/Resolver.h"
#include "utils/Json.h"
#include "utils/Logger.h"
// Assumptions: QxtWebRequestEvent instance is called event and result is true on success

View File

@@ -345,7 +345,7 @@ list(APPEND libSources
playlist/dynamic/database/DatabaseGenerator.cpp
playlist/dynamic/database/DatabaseControl.cpp
playlist/dynamic/DynamicControl.cpp
resolvers/ExternalResolver.cpp
resolvers/Resolver.cpp
resolvers/ScriptCollection.cpp
@@ -360,6 +360,7 @@ list(APPEND libSources
sip/PeerInfo.cpp
sip/SipStatusMessage.cpp
utils/Json.cpp
utils/TomahawkUtils.cpp
utils/Logger.cpp
utils/Qnr_IoDeviceStream.cpp

View File

@@ -27,7 +27,7 @@
#include "infosystem/InfoSystemCache.h"
#include "resolvers/ExternalResolver.h"
#include "resolvers/ScriptCollection.h"
#include "utils/Json.h"
#include "utils/Logger.h"
using namespace Tomahawk;

View File

@@ -26,6 +26,7 @@
#include "infosystem/InfoSystemCache.h"
#include "playlist/PlaylistUpdaterInterface.h"
#include "utils/Logger.h"
#include "utils/Json.h"
#include "utils/TomahawkUtils.h"
#include "PlaylistEntry.h"

View File

@@ -28,7 +28,7 @@
#else
#include <qt5keychain/keychain.h>
#endif
#include "utils/TomahawkUtils.h"
#include "utils/Json.h"
#endif
#include <QStringList>

View File

@@ -27,6 +27,7 @@
#include "ConfigStorage.h"
#include "resolvers/ExternalResolver.h"
#include "resolvers/ExternalResolverGui.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "Album.h"

View File

@@ -20,6 +20,7 @@
#include "SpotifyAccount.h"
#include "utils/Closure.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"

View File

@@ -19,6 +19,7 @@
#include "Database.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "DatabaseCommand.h"

View File

@@ -22,6 +22,7 @@
#include "playlist/dynamic/DynamicControl.h"
#include "playlist/dynamic/GeneratorInterface.h"
#include "network/Servent.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "DatabaseImpl.h"

View File

@@ -19,6 +19,7 @@
#include "DatabaseCommand_CreatePlaylist.h"
#include "network/Servent.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "DatabaseImpl.h"

View File

@@ -19,6 +19,8 @@
#include "DatabaseCommand_LoadAllPlaylists_p.h"
#include "utils/Json.h"
#include "DatabaseImpl.h"
#include "Playlist.h"
#include "PlaylistEntry.h"

View File

@@ -21,6 +21,7 @@
#include "playlist/dynamic/DynamicControl.h"
#include "playlist/dynamic/GeneratorInterface.h"
#include "playlist/dynamic/GeneratorFactory.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "DatabaseImpl.h"

View File

@@ -18,6 +18,7 @@
#include "DatabaseCommand_LoadPlaylistEntries.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "DatabaseImpl.h"

View File

@@ -22,6 +22,7 @@
#include "playlist/dynamic/DynamicPlaylist.h"
#include "playlist/dynamic/DynamicControl.h"
#include "network/Servent.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "DatabaseImpl.h"

View File

@@ -20,6 +20,7 @@
#include "collection/Collection.h"
#include "network/Servent.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "DatabaseImpl.h"

View File

@@ -19,6 +19,7 @@
#include "DatabaseWorker.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "Database.h"

View File

@@ -25,6 +25,7 @@
#include "network/Servent.h"
#include "network/Msg.h"
#include "utils/Logger.h"
#include "utils/Json.h"
#include "utils/TomahawkUtils.h"
#include "QTcpSocketExtra.h"

View File

@@ -19,7 +19,7 @@
#include "Msg_p.h"
#include "utils/TomahawkUtils.h"
#include "utils/Json.h"
#include <QtEndian>

View File

@@ -20,6 +20,7 @@
#include "network/Msg_p.h"
#include "network/Servent.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/TomahawkUtils.h"

View File

@@ -32,6 +32,7 @@
#include "sip/PeerInfo.h"
#include "sip/SipPlugin.h"
#include "utils/Closure.h"
#include "utils/Json.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"

View File

@@ -26,6 +26,7 @@
#include "database/DatabaseCommand_SetDynamicPlaylistRevision.h"
#include "database/DatabaseCommand_LoadDynamicPlaylistEntries.h"
#include "database/DatabaseCommand_DeleteDynamicPlaylist.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "GeneratorFactory.h"

View File

@@ -22,6 +22,7 @@
#include "accounts/AccountConfigWidget.h"
#include "utils/TomahawkUtilsGui.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"
#include "utils/NetworkProxyFactory.h"

View File

@@ -20,7 +20,7 @@
#include "SipInfo.h"
#include "utils/Logger.h"
#include "utils/TomahawkUtils.h"
#include "utils/Json.h"
#include <QVariant>

View File

@@ -37,6 +37,7 @@
#include "jobview/JobStatusView.h"
#include "jobview/JobStatusModel.h"
#include "jobview/ErrorStatusMessage.h"
#include "utils/Json.h"
#include "utils/NetworkReply.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"

View File

@@ -20,19 +20,21 @@
#include "ItunesParser.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QRegExp>
#include "Query.h"
#include "SourceList.h"
#include "jobview/JobStatusView.h"
#include "jobview/JobStatusModel.h"
#include "jobview/ErrorStatusMessage.h"
#include "utils/Json.h"
#include "utils/NetworkReply.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"
#include "Query.h"
#include "SourceList.h"
#include <QNetworkAccessManager>
#include <QRegExp>
// Forward Declarations breaking QSharedPointer
#if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 )
#include "Playlist.h"

View File

@@ -0,0 +1,116 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2014, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Json.h"
// Qt version specific includes
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
#include <QUrlQuery>
#include <QJsonDocument>
#include <QMetaProperty>
#else
#include <qjson/parser.h>
#include <qjson/qobjecthelper.h>
#include <qjson/serializer.h>
#endif
namespace TomahawkUtils
{
QVariantMap
qobject2qvariant( const QObject* object )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
const QMetaObject* metaObject = object->metaObject();
QVariantMap map;
for ( int i = 0; i < metaObject->propertyCount(); ++i )
{
QMetaProperty metaproperty = metaObject->property( i );
if ( metaproperty.isReadable() )
{
map[ QLatin1String( metaproperty.name() ) ] = object->property( metaproperty.name() );
}
}
return map;
#else
return QJson::QObjectHelper::qobject2qvariant( object );
#endif
}
void
qvariant2qobject( const QVariantMap& variant, QObject* object )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
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 );
}
}
}
#else
QJson::QObjectHelper::qvariant2qobject( variant, object );
#endif
}
QVariant
parseJson( const QByteArray& jsonData, bool* ok )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson( jsonData, &error );
if ( ok != NULL )
{
*ok = ( error.error == QJsonParseError::NoError );
}
return doc.toVariant();
#else
QJson::Parser p;
return p.parse( jsonData, ok );
#endif
}
QByteArray
toJson( const QVariant &variant, bool* ok )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
QJsonDocument doc = QJsonDocument::fromVariant( variant );
if ( ok != NULL )
{
*ok = true;
}
return doc.toJson();
#else
QJson::Serializer serializer;
return serializer.serialize( variant, ok );
#endif
}
}

View File

@@ -0,0 +1,36 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2014, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TOMAHAWKUTILS_JSON_H
#define TOMAHAWKUTILS_JSON_H
#include "DllMacro.h"
#include <QVariant>
namespace TomahawkUtils
{
/* QJson */
DLLEXPORT QVariantMap qobject2qvariant( const QObject* object );
DLLEXPORT void qvariant2qobject( const QVariantMap& variant, QObject* object );
DLLEXPORT QVariant parseJson( const QByteArray& jsonData, bool* ok = 0 );
DLLEXPORT QByteArray toJson( const QVariant &variant, bool* ok = 0 );
}
#endif // TOMAHAWKUTILS_JSON_H

View File

@@ -19,6 +19,7 @@
#include "JspfLoader.h"
#include "utils/Json.h"
#include "utils/Logger.h"
#include "utils/NetworkReply.h"
#include "utils/TomahawkUtils.h"

View File

@@ -22,6 +22,7 @@
#include "ShortLinkHelper_p.h"
#include "utils/Closure.h"
#include "utils/Json.h"
#include "utils/NetworkAccessManager.h"
#include "utils/TomahawkUtils.h"
#include "Playlist.h"

View File

@@ -19,20 +19,22 @@
#include "SpotifyParser.h"
#include <QtNetwork/QNetworkAccessManager>
#include "jobview/JobStatusView.h"
#include "jobview/JobStatusModel.h"
#include "jobview/ErrorStatusMessage.h"
#include "utils/Json.h"
#include "utils/NetworkReply.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"
#include "Query.h"
#include "SourceList.h"
#include "DropJob.h"
#include "DropJobNotifier.h"
#include "ViewManager.h"
#include "jobview/JobStatusView.h"
#include "jobview/JobStatusModel.h"
#include "jobview/ErrorStatusMessage.h"
#include "utils/NetworkReply.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include "utils/NetworkAccessManager.h"
#include <QNetworkAccessManager>
using namespace Tomahawk;

View File

@@ -57,11 +57,6 @@
// Qt version specific includes
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
#include <QUrlQuery>
#include <QJsonDocument>
#else
#include <qjson/parser.h>
#include <qjson/qobjecthelper.h>
#include <qjson/serializer.h>
#endif
#ifdef Q_OS_WIN
@@ -848,88 +843,6 @@ compareVersionStrings( const QString& first, const QString& second )
}
QVariantMap
qobject2qvariant( const QObject* object )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
const QMetaObject* metaObject = object->metaObject();
QVariantMap map;
for ( int i = 0; i < metaObject->propertyCount(); ++i )
{
QMetaProperty metaproperty = metaObject->property( i );
if ( metaproperty.isReadable() )
{
map[ QLatin1String( metaproperty.name() ) ] = object->property( metaproperty.name() );
}
}
return map;
#else
return QJson::QObjectHelper::qobject2qvariant( object );
#endif
}
void
qvariant2qobject( const QVariantMap& variant, QObject* object )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
for ( QVariantMap::const_iterator iter = variant.begin(); iter != variant.end(); ++iter )
{
QVariant property = object->property( iter.key().toLatin1() );
QVariant value = iter.value();
Q_ASSERT( property.isValid() );
if ( property.isValid() )
{
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 );
}
}
}
#else
QJson::QObjectHelper::qvariant2qobject( variant, object );
#endif
}
QVariant
parseJson( const QByteArray& jsonData, bool* ok )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson( jsonData, &error );
if ( ok != NULL )
{
*ok = ( error.error == QJsonParseError::NoError );
}
return doc.toVariant();
#else
QJson::Parser p;
return p.parse( jsonData, ok );
#endif
}
QByteArray
toJson( const QVariant &variant, bool* ok )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
QJsonDocument doc = QJsonDocument::fromVariant( variant );
if ( ok != NULL )
{
*ok = true;
}
return doc.toJson();
#else
QJson::Serializer serializer;
return serializer.serialize( variant, ok );
#endif
}
void
urlAddQueryItem( QUrl& url, const QString& key, const QString& value )
{

View File

@@ -207,12 +207,6 @@ namespace TomahawkUtils
* Qt4 / Qt5 compatibility layer
*/
/* QJson */
DLLEXPORT QVariantMap qobject2qvariant( const QObject* object );
DLLEXPORT void qvariant2qobject( const QVariantMap& variant, QObject* object );
DLLEXPORT QVariant parseJson( const QByteArray& jsonData, bool* ok = 0 );
DLLEXPORT QByteArray toJson( const QVariant &variant, bool* ok = 0 );
/* QUrl */
DLLEXPORT void urlAddQueryItem( QUrl& url, const QString& key, const QString& value );
DLLEXPORT QString urlQueryItemValue( const QUrl& url, const QString& key );