From b7bf5b6241005b1226c1a84489bef61d12050355 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Sat, 12 Apr 2014 08:42:55 +0100 Subject: [PATCH] Add wrapper to handle JSON parsing w.r.t. Qt version --- src/libtomahawk/utils/TomahawkUtils.cpp | 19 +++++++++++++++++++ src/libtomahawk/utils/TomahawkUtils.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/src/libtomahawk/utils/TomahawkUtils.cpp b/src/libtomahawk/utils/TomahawkUtils.cpp index ba02b10f7..24595e229 100644 --- a/src/libtomahawk/utils/TomahawkUtils.cpp +++ b/src/libtomahawk/utils/TomahawkUtils.cpp @@ -54,8 +54,12 @@ #include #include +// Qt version specific includes #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) #include + #include +#else + #include #endif #ifdef Q_OS_WIN @@ -842,6 +846,21 @@ compareVersionStrings( const QString& first, const QString& second ) } +QVariant +parseJson( const QByteArray& jsonData, bool* ok ) +{ +#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) + QJsonParseError error; + QJsonDocument doc = QJsonDocument::fromJson( jsonData, &error ); + *ok = ( error.error == QJsonParseError::NoError ); + return doc.toVariant(); +#else + QJson::Parser p; + return p.parse( jsonData, ok ); +#endif +} + + void urlAddQueryItem( QUrl& url, const QString& key, const QString& value ) { diff --git a/src/libtomahawk/utils/TomahawkUtils.h b/src/libtomahawk/utils/TomahawkUtils.h index 03cdfed68..c85749a34 100644 --- a/src/libtomahawk/utils/TomahawkUtils.h +++ b/src/libtomahawk/utils/TomahawkUtils.h @@ -207,6 +207,9 @@ namespace TomahawkUtils * Qt4 / Qt5 compatibility layer */ + /* QJson */ + DLLEXPORT QVariant parseJson( const QByteArray& jsonData, bool* ok = 0 ); + /* QUrl */ DLLEXPORT void urlAddQueryItem( QUrl& url, const QString& key, const QString& value ); DLLEXPORT QString urlQueryItemValue( const QUrl& url, const QString& key );