From 59b10b725ca00d7732d19667e955285b19ec90e1 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Sun, 13 Apr 2014 21:20:06 +0100 Subject: [PATCH] Add JSON serialization wrapper --- src/libtomahawk/utils/TomahawkUtils.cpp | 14 ++++++++++++++ src/libtomahawk/utils/TomahawkUtils.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/libtomahawk/utils/TomahawkUtils.cpp b/src/libtomahawk/utils/TomahawkUtils.cpp index 24595e229..4c6b735c9 100644 --- a/src/libtomahawk/utils/TomahawkUtils.cpp +++ b/src/libtomahawk/utils/TomahawkUtils.cpp @@ -861,6 +861,20 @@ parseJson( const QByteArray& jsonData, bool* ok ) } +QByteArray +toJson( const QVariant &variant, bool* ok ) +{ +#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) + QJsonDocument doc = QJsonDocument::fromVariant( variant ); + *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 ) { diff --git a/src/libtomahawk/utils/TomahawkUtils.h b/src/libtomahawk/utils/TomahawkUtils.h index c85749a34..9962cdad7 100644 --- a/src/libtomahawk/utils/TomahawkUtils.h +++ b/src/libtomahawk/utils/TomahawkUtils.h @@ -209,6 +209,7 @@ namespace TomahawkUtils /* QJson */ 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 );