| QxOrm 1.4.6 C++ Object Relational Mapping library

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

- File List
- File Members

QxSerializeQJson_QMap.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_QMAP_H_
    00034 #define _QX_SERIALIZE_QJSON_QMAP_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 #include <QtCore/qmap.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< QMap<Key, Value> >
    00061 {
    00062    static inline QJsonValue toJson(const QMap<Key, Value> & t, const QString & format)
    00063    {
    00064       QJsonArray arr; QJsonValue val;
    00065       QMapIterator<Key, Value> itr(t);
    00066
    00067       while (itr.hasNext())
    00068       {
    00069          itr.next(); QJsonObject obj;
    00070          val = qx::cvt::to_json(itr.key(), format); obj.insert("key", val);
    00071          val = qx::cvt::to_json(itr.value(), 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< QMap<Key, Value> >
    00081 {
    00082    static inline qx_bool fromJson(const QJsonValue & j, QMap<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
    00088       for (int i = 0; i < arr.count(); i++)
    00089       {
    00090          val = arr.at(i); if (! val.isObject()) { continue; }
    00091          obj = val.toObject(); Key key; Value value;
    00092          qx::cvt::from_json(obj.value("key"), key, format);
    00093          qx::cvt::from_json(obj.value("value"), value, format);
    00094          t.insert(key, value);
    00095       }
    00096
    00097       return qx_bool(true);
    00098    }
    00099 };
    00100
    00101 template <typename Value>
    00102 struct QxConvert_ToJson< QMap<QString, Value> >
    00103 {
    00104    static inline QJsonValue toJson(const QMap<QString, Value> & t, const QString & format)
    00105    {
    00106       QJsonObject obj; QJsonValue val;
    00107       QMapIterator<QString, Value> itr(t);
    00108
    00109       while (itr.hasNext())
    00110       {
    00111          itr.next();
    00112          val = qx::cvt::to_json(itr.value(), format);
    00113          obj.insert(itr.key(), val);
    00114       }
    00115
    00116       return QJsonValue(obj);
    00117    }
    00118 };
    00119
    00120 template <typename Value>
    00121 struct QxConvert_FromJson< QMap<QString, Value> >
    00122 {
    00123    static inline qx_bool fromJson(const QJsonValue & j, QMap<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(key, value);
    00134       }
    00135
    00136       return qx_bool(true);
    00137    }
    00138 };
    00139
    00140 template <typename Value>
    00141 struct QxConvert_ToJson< QMap<std::string, Value> >
    00142 {
    00143    static inline QJsonValue toJson(const QMap<std::string, Value> & t, const QString & format)
    00144    {
    00145       QJsonObject obj; QJsonValue val;
    00146       QMapIterator<std::string, Value> itr(t);
    00147
    00148       while (itr.hasNext())
    00149       {
    00150          itr.next();
    00151
    00152 #ifndef QT_NO_STL
    00153          QString key = QString::fromStdString(itr.key());
    00154 #else // QT_NO_STL
    00155          std::string s = itr.key();
    00156          QString key = QString::fromLatin1(s.data(), int(s.size()));
    00157 #endif // QT_NO_STL
    00158
    00159          val = qx::cvt::to_json(itr.value(), format);
    00160          obj.insert(key, val);
    00161       }
    00162
    00163       return QJsonValue(obj);
    00164    }
    00165 };
    00166
    00167 template <typename Value>
    00168 struct QxConvert_FromJson< QMap<std::string, Value> >
    00169 {
    00170    static inline qx_bool fromJson(const QJsonValue & j, QMap<std::string, Value> & t, const QString & format)
    00171    {
    00172       t.clear();
    00173       if (! j.isObject()) { return qx_bool(true); }
    00174       QJsonObject obj = j.toObject(); QJsonValue val;
    00175
    00176       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
    00177       {
    00178          QString key = itr.key(); Value value;
    00179          qx::cvt::from_json(itr.value(), value, format);
    00180
    00181 #ifndef QT_NO_STL
    00182          std::string s = key.toStdString();
    00183 #else // QT_NO_STL
    00184          std::string s = key.toLatin1().constData();
    00185 #endif // QT_NO_STL
    00186
    00187          t.insert(s, value);
    00188       }
    00189
    00190       return qx_bool(true);
    00191    }
    00192 };
    00193
    00194 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
    00195
    00196 template <typename Value>
    00197 struct QxConvert_ToJson< QMap<std::wstring, Value> >
    00198 {
    00199    static inline QJsonValue toJson(const QMap<std::wstring, Value> & t, const QString & format)
    00200    {
    00201       QJsonObject obj; QJsonValue val;
    00202       QMapIterator<std::wstring, Value> itr(t);
    00203
    00204       while (itr.hasNext())
    00205       {
    00206          itr.next();
    00207          val = qx::cvt::to_json(itr.value(), format);
    00208          obj.insert(QString::fromStdWString(itr.key()), val);
    00209       }
    00210
    00211       return QJsonValue(obj);
    00212    }
    00213 };
    00214
    00215 template <typename Value>
    00216 struct QxConvert_FromJson< QMap<std::wstring, Value> >
    00217 {
    00218    static inline qx_bool fromJson(const QJsonValue & j, QMap<std::wstring, Value> & t, const QString & format)
    00219    {
    00220       t.clear();
    00221       if (! j.isObject()) { return qx_bool(true); }
    00222       QJsonObject obj = j.toObject(); QJsonValue val;
    00223
    00224       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
    00225       {
    00226          QString key = itr.key(); Value value;
    00227          qx::cvt::from_json(itr.value(), value, format);
    00228          t.insert(key.toStdWString(), value);
    00229       }
    00230
    00231       return qx_bool(true);
    00232    }
    00233 };
    00234
    00235 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
    00236
    00237 } // namespace detail
    00238 } // namespace cvt
    00239 } // namespace qx
    00240
    00241 #endif // _QX_SERIALIZE_QJSON_QMAP_H_
    00242 #endif // _QX_NO_JSON