QxOrm  1.5.0
C++ Object Relational Mapping library
QxSerializeQJson_std_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 #ifndef _QX_SERIALIZE_QJSON_STD_MAP_H_
00034 #define _QX_SERIALIZE_QJSON_STD_MAP_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 <map>
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< std::map<Key, Value> >
00062 {
00063    static inline QJsonValue toJson(const std::map<Key, Value> & t, const QString & format)
00064    {
00065       typedef typename std::map<Key, Value>::const_iterator type_itr;
00066       QJsonArray arr; QJsonValue val;
00067 
00068       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00069       {
00070          QJsonObject obj;
00071          val = qx::cvt::to_json(itr->first, format); obj.insert("key", val);
00072          val = qx::cvt::to_json(itr->second, format); obj.insert("value", val);
00073          arr.append(obj);
00074       }
00075 
00076       return QJsonValue(arr);
00077    }
00078 };
00079 
00080 template <typename Key, typename Value>
00081 struct QxConvert_FromJson< std::map<Key, Value> >
00082 {
00083    static inline qx_bool fromJson(const QJsonValue & j, std::map<Key, Value> & t, const QString & format)
00084    {
00085       t.clear();
00086       if (! j.isArray()) { return qx_bool(true); }
00087       QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
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 Value>
00103 struct QxConvert_ToJson< std::map<QString, Value> >
00104 {
00105    static inline QJsonValue toJson(const std::map<QString, Value> & t, const QString & format)
00106    {
00107       typedef typename std::map<QString, Value>::const_iterator type_itr;
00108       QJsonObject obj; QJsonValue val;
00109 
00110       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00111       {
00112          val = qx::cvt::to_json(itr->second, format);
00113          obj.insert(itr->first, val);
00114       }
00115 
00116       return QJsonValue(obj);
00117    }
00118 };
00119 
00120 template <typename Value>
00121 struct QxConvert_FromJson< std::map<QString, Value> >
00122 {
00123    static inline qx_bool fromJson(const QJsonValue & j, std::map<QString, Value> & t, const QString & format)
00124    {
00125       t.clear();
00126       if (! j.isObject()) { return qx_bool(true); }
00127       QJsonObject obj = j.toObject(); QJsonValue val;
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(std::make_pair(key, value));
00134       }
00135 
00136       return qx_bool(true);
00137    }
00138 };
00139 
00140 template <typename Value>
00141 struct QxConvert_ToJson< std::map<std::string, Value> >
00142 {
00143    static inline QJsonValue toJson(const std::map<std::string, Value> & t, const QString & format)
00144    {
00145       typedef typename std::map<std::string, Value>::const_iterator type_itr;
00146       QJsonObject obj; QJsonValue val;
00147 
00148       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00149       {
00150 #ifndef QT_NO_STL
00151          QString key = QString::fromStdString(itr->first);
00152 #else // QT_NO_STL
00153          std::string s = itr->first;
00154          QString key = QString::fromLatin1(s.data(), int(s.size()));
00155 #endif // QT_NO_STL
00156 
00157          val = qx::cvt::to_json(itr->second, format);
00158          obj.insert(key, val);
00159       }
00160 
00161       return QJsonValue(obj);
00162    }
00163 };
00164 
00165 template <typename Value>
00166 struct QxConvert_FromJson< std::map<std::string, Value> >
00167 {
00168    static inline qx_bool fromJson(const QJsonValue & j, std::map<std::string, Value> & t, const QString & format)
00169    {
00170       t.clear();
00171       if (! j.isObject()) { return qx_bool(true); }
00172       QJsonObject obj = j.toObject(); QJsonValue val;
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(std::make_pair(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< std::map<std::wstring, Value> >
00196 {
00197    static inline QJsonValue toJson(const std::map<std::wstring, Value> & t, const QString & format)
00198    {
00199       typedef typename std::map<std::wstring, Value>::const_iterator type_itr;
00200       QJsonObject obj; QJsonValue val;
00201 
00202       for (type_itr itr = t.begin(); itr != t.end(); ++itr)
00203       {
00204          val = qx::cvt::to_json(itr->second, format);
00205          obj.insert(QString::fromStdWString(itr->first), val);
00206       }
00207 
00208       return QJsonValue(obj);
00209    }
00210 };
00211 
00212 template <typename Value>
00213 struct QxConvert_FromJson< std::map<std::wstring, Value> >
00214 {
00215    static inline qx_bool fromJson(const QJsonValue & j, std::map<std::wstring, Value> & t, const QString & format)
00216    {
00217       t.clear();
00218       if (! j.isObject()) { return qx_bool(true); }
00219       QJsonObject obj = j.toObject(); QJsonValue val;
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(std::make_pair(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_STD_MAP_H_
00239 #endif // _QX_NO_JSON