| QxOrm 1.4.5 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

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