00001 /* 00002 * Copyright (C) 2008, 2009 Nokia Corporation. 00003 * 00004 * Contact: Marius Vollmer <marius.vollmer@nokia.com> 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public License 00008 * version 2.1 as published by the Free Software Foundation. 00009 * 00010 * This program is distributed in the hope that it will be useful, but 00011 * WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 00018 * 02110-1301 USA 00019 * 00020 */ 00021 00022 #ifndef CONTEXTJSON_H 00023 #define CONTEXTJSON_H 00024 00025 #include <QString> 00026 #include <QVariant> 00027 #include <qjson/serializer.h> 00028 #include <qjson/parser.h> 00029 00030 // stolen from glib 00031 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) 00032 #define CKIT_GNUC_UNUSED __attribute__((__unused__)) 00033 #else 00034 #define CKIT_GNUC_UNUSED 00035 #endif 00036 00037 namespace ContextSubscriber { 00038 00039 // Please note that it is intentional that this header file contains 00040 // implementation. This way libcontextsubscriber doesn't have to link 00041 // against libqjson (this would be one more extra dependency), but 00042 // only relatively rare users of this feature have to. 00043 00050 CKIT_GNUC_UNUSED 00051 static QString qVariantToJSON(const QVariant &v) { 00052 QJson::Serializer serializer; 00053 return serializer.serialize(v); 00054 } 00055 00057 CKIT_GNUC_UNUSED 00058 static QVariant jsonToQVariant(const QString &s) { 00059 QJson::Parser parser; 00060 bool ok; 00061 QVariant v = parser.parse(s.toUtf8(), &ok); 00062 if (ok) 00063 return QVariant(v); 00064 else 00065 return QVariant(); 00066 } 00067 00068 } // namespace 00069 00070 #endif