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