QxOrm  1.5.0
C++ Object Relational Mapping library
QxSerializeQJson_QxCollection.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifndef _QX_NO_JSON
00033 #ifndef _QX_SERIALIZE_QJSON_QX_COLLECTION_H_
00034 #define _QX_SERIALIZE_QJSON_QX_COLLECTION_H_
00035 
00036 #ifdef _MSC_VER
00037 #pragma once
00038 #endif
00039 
00047 #include <QtCore/qjsonvalue.h>
00048 #include <QtCore/qjsonobject.h>
00049 #include <QtCore/qjsonarray.h>
00050 
00051 #include <QxCollection/QxCollection.h>
00052 
00053 #include <QxConvert/QxConvert.h>
00054 #include <QxConvert/QxConvert_Impl.h>
00055 
00056 namespace qx {
00057 namespace cvt {
00058 namespace detail {
00059 
00060 template <typename Key, typename Value>
00061 struct QxConvert_ToJson< qx::QxCollection<Key, Value> >
00062 {
00063    static inline QJsonValue toJson(const qx::QxCollection<Key, Value> & t, const QString & format)
00064    {
00065       QJsonArray arr; QJsonValue val;
00066 
00067       for (long l = 0; l < t.count(); l++)
00068       {
00069          QJsonObject obj;
00070          val = qx::cvt::to_json(t.getKeyByIndex(l), format); obj.insert("key", val);
00071          val = qx::cvt::to_json(t.getByIndex(l), format); obj.insert("value", val);
00072          arr.append(obj);
00073       }
00074 
00075       return QJsonValue(arr);
00076    }
00077 };
00078 
00079 template <typename Key, typename Value>
00080 struct QxConvert_FromJson< qx::QxCollection<Key, Value> >
00081 {
00082    static inline qx_bool fromJson(const QJsonValue & j, qx::QxCollection<Key, Value> & t, const QString & format)
00083    {
00084       t.clear();
00085       if (! j.isArray()) { return qx_bool(true); }
00086       QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
00087       t.reserve(static_cast<long>(arr.count()));
00088 
00089       for (int i = 0; i < arr.count(); i++)
00090       {
00091          val = arr.at(i); if (! val.isObject()) { continue; }
00092          obj = val.toObject(); Key key; Value value;
00093          qx::cvt::from_json(obj.value("key"), key, format);
00094          qx::cvt::from_json(obj.value("value"), value, format);
00095          t.insert(key, value);
00096       }
00097 
00098       return qx_bool(true);
00099    }
00100 };
00101 
00102 template <typename Value>
00103 struct QxConvert_ToJson< qx::QxCollection<QString, Value> >
00104 {
00105    static inline QJsonValue toJson(const qx::QxCollection<QString, Value> & t, const QString & format)
00106    {
00107       QJsonObject obj; QJsonValue val;
00108 
00109       for (long l = 0; l < t.count(); l++)
00110       {
00111          val = qx::cvt::to_json(t.getByIndex(l), format);
00112          obj.insert(t.getKeyByIndex(l), val);
00113       }
00114 
00115       return QJsonValue(obj);
00116    }
00117 };
00118 
00119 template <typename Value>
00120 struct QxConvert_FromJson< qx::QxCollection<QString, Value> >
00121 {
00122    static inline qx_bool fromJson(const QJsonValue & j, qx::QxCollection<QString, Value> & t, const QString & format)
00123    {
00124       t.clear();
00125       if (! j.isObject()) { return qx_bool(true); }
00126       QJsonObject obj = j.toObject(); QJsonValue val;
00127       t.reserve(static_cast<long>(obj.count()));
00128 
00129       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00130       {
00131          QString key = itr.key(); Value value;
00132          qx::cvt::from_json(itr.value(), value, format);
00133          t.insert(key, value);
00134       }
00135 
00136       return qx_bool(true);
00137    }
00138 };
00139 
00140 template <typename Value>
00141 struct QxConvert_ToJson< qx::QxCollection<std::string, Value> >
00142 {
00143    static inline QJsonValue toJson(const qx::QxCollection<std::string, Value> & t, const QString & format)
00144    {
00145       QJsonObject obj; QJsonValue val;
00146 
00147       for (long l = 0; l < t.count(); l++)
00148       {
00149 #ifndef QT_NO_STL
00150          QString key = QString::fromStdString(t.getKeyByIndex(l));
00151 #else // QT_NO_STL
00152          std::string s = t.getKeyByIndex(l);
00153          QString key = QString::fromLatin1(s.data(), int(s.size()));
00154 #endif // QT_NO_STL
00155 
00156          val = qx::cvt::to_json(t.getByIndex(l), format);
00157          obj.insert(key, val);
00158       }
00159 
00160       return QJsonValue(obj);
00161    }
00162 };
00163 
00164 template <typename Value>
00165 struct QxConvert_FromJson< qx::QxCollection<std::string, Value> >
00166 {
00167    static inline qx_bool fromJson(const QJsonValue & j, qx::QxCollection<std::string, Value> & t, const QString & format)
00168    {
00169       t.clear();
00170       if (! j.isObject()) { return qx_bool(true); }
00171       QJsonObject obj = j.toObject(); QJsonValue val;
00172       t.reserve(static_cast<long>(obj.count()));
00173 
00174       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00175       {
00176          QString key = itr.key(); Value value;
00177          qx::cvt::from_json(itr.value(), value, format);
00178 
00179 #ifndef QT_NO_STL
00180          std::string s = key.toStdString();
00181 #else // QT_NO_STL
00182          std::string s = key.toLatin1().constData();
00183 #endif // QT_NO_STL
00184 
00185          t.insert(s, value);
00186       }
00187 
00188       return qx_bool(true);
00189    }
00190 };
00191 
00192 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00193 
00194 template <typename Value>
00195 struct QxConvert_ToJson< qx::QxCollection<std::wstring, Value> >
00196 {
00197    static inline QJsonValue toJson(const qx::QxCollection<std::wstring, Value> & t, const QString & format)
00198    {
00199       QJsonObject obj; QJsonValue val;
00200 
00201       for (long l = 0; l < t.count(); l++)
00202       {
00203          val = qx::cvt::to_json(t.getByIndex(l), format);
00204          obj.insert(QString::fromStdWString(t.getKeyByIndex(l)), val);
00205       }
00206 
00207       return QJsonValue(obj);
00208    }
00209 };
00210 
00211 template <typename Value>
00212 struct QxConvert_FromJson< qx::QxCollection<std::wstring, Value> >
00213 {
00214    static inline qx_bool fromJson(const QJsonValue & j, qx::QxCollection<std::wstring, Value> & t, const QString & format)
00215    {
00216       t.clear();
00217       if (! j.isObject()) { return qx_bool(true); }
00218       QJsonObject obj = j.toObject(); QJsonValue val;
00219       t.reserve(static_cast<long>(obj.count()));
00220 
00221       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00222       {
00223          QString key = itr.key(); Value value;
00224          qx::cvt::from_json(itr.value(), value, format);
00225          t.insert(key.toStdWString(), value);
00226       }
00227 
00228       return qx_bool(true);
00229    }
00230 };
00231 
00232 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00233 
00234 } // namespace detail
00235 } // namespace cvt
00236 } // namespace qx
00237 
00238 #endif // _QX_SERIALIZE_QJSON_QX_COLLECTION_H_
00239 #endif // _QX_NO_JSON