QxOrm  1.5.0
C++ Object Relational Mapping library
QxSerializeQJson_boost_unordered_map.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 #ifdef _QX_ENABLE_BOOST
00034 #ifndef _QX_SERIALIZE_QJSON_BOOST_UNORDERED_MAP_H_
00035 #define _QX_SERIALIZE_QJSON_BOOST_UNORDERED_MAP_H_
00036 
00037 #ifdef _MSC_VER
00038 #pragma once
00039 #endif
00040 
00048 #include <QtCore/qjsonvalue.h>
00049 #include <QtCore/qjsonobject.h>
00050 #include <QtCore/qjsonarray.h>
00051 
00052 #include <QxConvert/QxConvert.h>
00053 #include <QxConvert/QxConvert_Impl.h>
00054 
00055 namespace qx {
00056 namespace cvt {
00057 namespace detail {
00058 
00059 template <typename Key, typename Value>
00060 struct QxConvert_ToJson< boost::unordered_map<Key, Value> >
00061 {
00062    static inline QJsonValue toJson(const boost::unordered_map<Key, Value> & t, const QString & format)
00063    {
00064       typedef typename boost::unordered_map<Key, Value>::const_iterator type_itr;
00065       QJsonArray arr; QJsonValue val;
00066 
00067       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00068       {
00069          QJsonObject obj;
00070          val = qx::cvt::to_json(itr->first, format); obj.insert("key", val);
00071          val = qx::cvt::to_json(itr->second, 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< boost::unordered_map<Key, Value> >
00081 {
00082    static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_map<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<typename boost::unordered_map<Key, Value>::size_type>(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(std::make_pair(key, value));
00096       }
00097 
00098       return qx_bool(true);
00099    }
00100 };
00101 
00102 template <typename Key, typename Value>
00103 struct QxConvert_ToJson< boost::unordered_multimap<Key, Value> >
00104 {
00105    static inline QJsonValue toJson(const boost::unordered_multimap<Key, Value> & t, const QString & format)
00106    {
00107       typedef typename boost::unordered_multimap<Key, Value>::const_iterator type_itr;
00108       QJsonArray arr; QJsonValue val;
00109 
00110       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00111       {
00112          QJsonObject obj;
00113          val = qx::cvt::to_json(itr->first, format); obj.insert("key", val);
00114          val = qx::cvt::to_json(itr->second, format); obj.insert("value", val);
00115          arr.append(obj);
00116       }
00117 
00118       return QJsonValue(arr);
00119    }
00120 };
00121 
00122 template <typename Key, typename Value>
00123 struct QxConvert_FromJson< boost::unordered_multimap<Key, Value> >
00124 {
00125    static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_multimap<Key, Value> & t, const QString & format)
00126    {
00127       t.clear();
00128       if (! j.isArray()) { return qx_bool(true); }
00129       QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
00130       t.reserve(static_cast<typename boost::unordered_multimap<Key, Value>::size_type>(arr.count()));
00131 
00132       for (int i = 0; i < arr.count(); i++)
00133       {
00134          val = arr.at(i); if (! val.isObject()) { continue; }
00135          obj = val.toObject(); Key key; Value value;
00136          qx::cvt::from_json(obj.value("key"), key, format);
00137          qx::cvt::from_json(obj.value("value"), value, format);
00138          t.insert(std::make_pair(key, value));
00139       }
00140 
00141       return qx_bool(true);
00142    }
00143 };
00144 
00145 template <typename Value>
00146 struct QxConvert_ToJson< boost::unordered_map<QString, Value> >
00147 {
00148    static inline QJsonValue toJson(const boost::unordered_map<QString, Value> & t, const QString & format)
00149    {
00150       typedef typename boost::unordered_map<QString, Value>::const_iterator type_itr;
00151       QJsonObject obj; QJsonValue val;
00152 
00153       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00154       {
00155          val = qx::cvt::to_json(itr->second, format);
00156          obj.insert(itr->first, val);
00157       }
00158 
00159       return QJsonValue(obj);
00160    }
00161 };
00162 
00163 template <typename Value>
00164 struct QxConvert_FromJson< boost::unordered_map<QString, Value> >
00165 {
00166    static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_map<QString, Value> & t, const QString & format)
00167    {
00168       t.clear();
00169       if (! j.isObject()) { return qx_bool(true); }
00170       QJsonObject obj = j.toObject(); QJsonValue val;
00171       t.reserve(static_cast<typename boost::unordered_map<QString, Value>::size_type>(obj.count()));
00172 
00173       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00174       {
00175          QString key = itr.key(); Value value;
00176          qx::cvt::from_json(itr.value(), value, format);
00177          t.insert(std::make_pair(key, value));
00178       }
00179 
00180       return qx_bool(true);
00181    }
00182 };
00183 
00184 template <typename Value>
00185 struct QxConvert_ToJson< boost::unordered_map<std::string, Value> >
00186 {
00187    static inline QJsonValue toJson(const boost::unordered_map<std::string, Value> & t, const QString & format)
00188    {
00189       typedef typename boost::unordered_map<std::string, Value>::const_iterator type_itr;
00190       QJsonObject obj; QJsonValue val;
00191 
00192       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00193       {
00194 #ifndef QT_NO_STL
00195          QString key = QString::fromStdString(itr->first);
00196 #else // QT_NO_STL
00197          QString key = QString::fromLatin1(itr->first.data(), int(itr->first.size()));
00198 #endif // QT_NO_STL
00199 
00200          val = qx::cvt::to_json(itr->second, format);
00201          obj.insert(key, val);
00202       }
00203 
00204       return QJsonValue(obj);
00205    }
00206 };
00207 
00208 template <typename Value>
00209 struct QxConvert_FromJson< boost::unordered_map<std::string, Value> >
00210 {
00211    static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_map<std::string, Value> & t, const QString & format)
00212    {
00213       t.clear();
00214       if (! j.isObject()) { return qx_bool(true); }
00215       QJsonObject obj = j.toObject(); QJsonValue val;
00216       t.reserve(static_cast<typename boost::unordered_map<std::string, Value>::size_type>(obj.count()));
00217 
00218       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00219       {
00220          QString key = itr.key(); Value value;
00221          qx::cvt::from_json(itr.value(), value, format);
00222 
00223 #ifndef QT_NO_STL
00224          std::string s = key.toStdString();
00225 #else // QT_NO_STL
00226          std::string s = key.toLatin1().constData();
00227 #endif // QT_NO_STL
00228 
00229          t.insert(std::make_pair(s, value));
00230       }
00231 
00232       return qx_bool(true);
00233    }
00234 };
00235 
00236 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00237 
00238 template <typename Value>
00239 struct QxConvert_ToJson< boost::unordered_map<std::wstring, Value> >
00240 {
00241    static inline QJsonValue toJson(const boost::unordered_map<std::wstring, Value> & t, const QString & format)
00242    {
00243       typedef typename boost::unordered_map<std::wstring, Value>::const_iterator type_itr;
00244       QJsonObject obj; QJsonValue val;
00245 
00246       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00247       {
00248          val = qx::cvt::to_json(itr->second, format);
00249          obj.insert(QString::fromStdWString(itr->first), val);
00250       }
00251 
00252       return QJsonValue(obj);
00253    }
00254 };
00255 
00256 template <typename Value>
00257 struct QxConvert_FromJson< boost::unordered_map<std::wstring, Value> >
00258 {
00259    static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_map<std::wstring, Value> & t, const QString & format)
00260    {
00261       t.clear();
00262       if (! j.isObject()) { return qx_bool(true); }
00263       QJsonObject obj = j.toObject(); QJsonValue val;
00264       t.reserve(static_cast<typename boost::unordered_map<std::wstring, Value>::size_type>(obj.count()));
00265 
00266       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00267       {
00268          QString key = itr.key(); Value value;
00269          qx::cvt::from_json(itr.value(), value, format);
00270          t.insert(std::make_pair(key.toStdWString(), value));
00271       }
00272 
00273       return qx_bool(true);
00274    }
00275 };
00276 
00277 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00278 
00279 } // namespace detail
00280 } // namespace cvt
00281 } // namespace qx
00282 
00283 #endif // _QX_SERIALIZE_QJSON_BOOST_UNORDERED_MAP_H_
00284 #endif // _QX_ENABLE_BOOST
00285 #endif // _QX_NO_JSON