QxOrm  1.5.0
C++ Object Relational Mapping library
QxSerializeQJson_QHash.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_QHASH_H_
00034 #define _QX_SERIALIZE_QJSON_QHASH_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/qhash.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< QHash<Key, Value> >
00061 {
00062    static inline QJsonValue toJson(const QHash<Key, Value> & t, const QString & format)
00063    {
00064       QJsonArray arr; QJsonValue val;
00065       QHashIterator<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< QHash<Key, Value> >
00081 {
00082    static inline qx_bool fromJson(const QJsonValue & j, QHash<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(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< QHash<QString, Value> >
00104 {
00105    static inline QJsonValue toJson(const QHash<QString, Value> & t, const QString & format)
00106    {
00107       QJsonObject obj; QJsonValue val;
00108       QHashIterator<QString, Value> itr(t);
00109 
00110       while (itr.hasNext())
00111       {
00112          itr.next();
00113          val = qx::cvt::to_json(itr.value(), format);
00114          obj.insert(itr.key(), val);
00115       }
00116 
00117       return QJsonValue(obj);
00118    }
00119 };
00120 
00121 template <typename Value>
00122 struct QxConvert_FromJson< QHash<QString, Value> >
00123 {
00124    static inline qx_bool fromJson(const QJsonValue & j, QHash<QString, Value> & t, const QString & format)
00125    {
00126       t.clear();
00127       if (! j.isObject()) { return qx_bool(true); }
00128       QJsonObject obj = j.toObject(); QJsonValue val;
00129       t.reserve(obj.count());
00130 
00131       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00132       {
00133          QString key = itr.key(); Value value;
00134          qx::cvt::from_json(itr.value(), value, format);
00135          t.insert(key, value);
00136       }
00137 
00138       return qx_bool(true);
00139    }
00140 };
00141 
00142 template <typename Value>
00143 struct QxConvert_ToJson< QHash<std::string, Value> >
00144 {
00145    static inline QJsonValue toJson(const QHash<std::string, Value> & t, const QString & format)
00146    {
00147       QJsonObject obj; QJsonValue val;
00148       QHashIterator<std::string, Value> itr(t);
00149 
00150       while (itr.hasNext())
00151       {
00152          itr.next();
00153 
00154 #ifndef QT_NO_STL
00155          QString key = QString::fromStdString(itr.key());
00156 #else // QT_NO_STL
00157          std::string s = itr.key();
00158          QString key = QString::fromLatin1(s.data(), int(s.size()));
00159 #endif // QT_NO_STL
00160 
00161          val = qx::cvt::to_json(itr.value(), format);
00162          obj.insert(key, val);
00163       }
00164 
00165       return QJsonValue(obj);
00166    }
00167 };
00168 
00169 template <typename Value>
00170 struct QxConvert_FromJson< QHash<std::string, Value> >
00171 {
00172    static inline qx_bool fromJson(const QJsonValue & j, QHash<std::string, Value> & t, const QString & format)
00173    {
00174       t.clear();
00175       if (! j.isObject()) { return qx_bool(true); }
00176       QJsonObject obj = j.toObject(); QJsonValue val;
00177       t.reserve(obj.count());
00178 
00179       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00180       {
00181          QString key = itr.key(); Value value;
00182          qx::cvt::from_json(itr.value(), value, format);
00183 
00184 #ifndef QT_NO_STL
00185          std::string s = key.toStdString();
00186 #else // QT_NO_STL
00187          std::string s = key.toLatin1().constData();
00188 #endif // QT_NO_STL
00189 
00190          t.insert(s, value);
00191       }
00192 
00193       return qx_bool(true);
00194    }
00195 };
00196 
00197 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00198 
00199 template <typename Value>
00200 struct QxConvert_ToJson< QHash<std::wstring, Value> >
00201 {
00202    static inline QJsonValue toJson(const QHash<std::wstring, Value> & t, const QString & format)
00203    {
00204       QJsonObject obj; QJsonValue val;
00205       QHashIterator<std::wstring, Value> itr(t);
00206 
00207       while (itr.hasNext())
00208       {
00209          itr.next();
00210          val = qx::cvt::to_json(itr.value(), format);
00211          obj.insert(QString::fromStdWString(itr.key()), val);
00212       }
00213 
00214       return QJsonValue(obj);
00215    }
00216 };
00217 
00218 template <typename Value>
00219 struct QxConvert_FromJson< QHash<std::wstring, Value> >
00220 {
00221    static inline qx_bool fromJson(const QJsonValue & j, QHash<std::wstring, Value> & t, const QString & format)
00222    {
00223       t.clear();
00224       if (! j.isObject()) { return qx_bool(true); }
00225       QJsonObject obj = j.toObject(); QJsonValue val;
00226       t.reserve(obj.count());
00227 
00228       for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
00229       {
00230          QString key = itr.key(); Value value;
00231          qx::cvt::from_json(itr.value(), value, format);
00232          t.insert(key.toStdWString(), value);
00233       }
00234 
00235       return qx_bool(true);
00236    }
00237 };
00238 
00239 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00240 
00241 } // namespace detail
00242 } // namespace cvt
00243 } // namespace qx
00244 
00245 #endif // _QX_SERIALIZE_QJSON_QHASH_H_
00246 #endif // _QX_NO_JSON