QxSerializeQJson_std_unordered_map.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_NO_JSON
  32. #ifndef _QX_SERIALIZE_QJSON_STD_UNORDERED_MAP_H_
  33. #define _QX_SERIALIZE_QJSON_STD_UNORDERED_MAP_H_
  34. #ifdef _MSC_VER
  35. #pragma once
  36. #endif
  37. /*!
  38. * \file QxSerializeQJson_std_unordered_map.h
  39. * \author Lionel Marty
  40. * \ingroup QxSerialize
  41. * \brief Provide a Qt QJson serialization method (save/load) for type std::unordered_map<Key, Value> and std::unordered_multimap<Key, Value> (C++11 compilation option _QX_CPP_11_CONTAINER must be defined)
  42. */
  43. #include <QtCore/qjsonvalue.h>
  44. #include <QtCore/qjsonobject.h>
  45. #include <QtCore/qjsonarray.h>
  46. #include <unordered_map>
  47. #include <QxConvert/QxConvert.h>
  48. #include <QxConvert/QxConvert_Impl.h>
  49. namespace qx {
  50. namespace cvt {
  51. namespace detail {
  52. template <typename Key, typename Value>
  53. struct QxConvert_ToJson< std::unordered_map<Key, Value> >
  54. {
  55. static inline QJsonValue toJson(const std::unordered_map<Key, Value> & t, const QString & format)
  56. {
  57. typedef typename std::unordered_map<Key, Value>::const_iterator type_itr;
  58. QJsonArray arr; QJsonValue val;
  59. for (type_itr itr = t.begin(); itr != t.end(); ++itr)
  60. {
  61. QJsonObject obj;
  62. val = qx::cvt::to_json(itr->first, format); obj.insert("key", val);
  63. val = qx::cvt::to_json(itr->second, format); obj.insert("value", val);
  64. arr.append(obj);
  65. }
  66. return QJsonValue(arr);
  67. }
  68. };
  69. template <typename Key, typename Value>
  70. struct QxConvert_FromJson< std::unordered_map<Key, Value> >
  71. {
  72. static inline qx_bool fromJson(const QJsonValue & j, std::unordered_map<Key, Value> & t, const QString & format)
  73. {
  74. t.clear();
  75. if (! j.isArray()) { return qx_bool(true); }
  76. QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
  77. t.reserve(static_cast<typename std::unordered_map<Key, Value>::size_type>(arr.count()));
  78. for (int i = 0; i < arr.count(); i++)
  79. {
  80. val = arr.at(i); if (! val.isObject()) { continue; }
  81. obj = val.toObject(); Key key; Value value;
  82. qx::cvt::from_json(obj.value("key"), key, format);
  83. qx::cvt::from_json(obj.value("value"), value, format);
  84. t.insert(std::make_pair(key, value));
  85. }
  86. return qx_bool(true);
  87. }
  88. };
  89. template <typename Key, typename Value>
  90. struct QxConvert_ToJson< std::unordered_multimap<Key, Value> >
  91. {
  92. static inline QJsonValue toJson(const std::unordered_multimap<Key, Value> & t, const QString & format)
  93. {
  94. typedef typename std::unordered_multimap<Key, Value>::const_iterator type_itr;
  95. QJsonArray arr; QJsonValue val;
  96. for (type_itr itr = t.begin(); itr != t.end(); ++itr)
  97. {
  98. QJsonObject obj;
  99. val = qx::cvt::to_json(itr->first, format); obj.insert("key", val);
  100. val = qx::cvt::to_json(itr->second, format); obj.insert("value", val);
  101. arr.append(obj);
  102. }
  103. return QJsonValue(arr);
  104. }
  105. };
  106. template <typename Key, typename Value>
  107. struct QxConvert_FromJson< std::unordered_multimap<Key, Value> >
  108. {
  109. static inline qx_bool fromJson(const QJsonValue & j, std::unordered_multimap<Key, Value> & t, const QString & format)
  110. {
  111. t.clear();
  112. if (! j.isArray()) { return qx_bool(true); }
  113. QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
  114. t.reserve(static_cast<typename std::unordered_multimap<Key, Value>::size_type>(arr.count()));
  115. for (int i = 0; i < arr.count(); i++)
  116. {
  117. val = arr.at(i); if (! val.isObject()) { continue; }
  118. obj = val.toObject(); Key key; Value value;
  119. qx::cvt::from_json(obj.value("key"), key, format);
  120. qx::cvt::from_json(obj.value("value"), value, format);
  121. t.insert(std::make_pair(key, value));
  122. }
  123. return qx_bool(true);
  124. }
  125. };
  126. template <typename Value>
  127. struct QxConvert_ToJson< std::unordered_map<QString, Value> >
  128. {
  129. static inline QJsonValue toJson(const std::unordered_map<QString, Value> & t, const QString & format)
  130. {
  131. typedef typename std::unordered_map<QString, Value>::const_iterator type_itr;
  132. QJsonObject obj; QJsonValue val;
  133. for (type_itr itr = t.begin(); itr != t.end(); ++itr)
  134. {
  135. val = qx::cvt::to_json(itr->second, format);
  136. obj.insert(itr->first, val);
  137. }
  138. return QJsonValue(obj);
  139. }
  140. };
  141. template <typename Value>
  142. struct QxConvert_FromJson< std::unordered_map<QString, Value> >
  143. {
  144. static inline qx_bool fromJson(const QJsonValue & j, std::unordered_map<QString, Value> & t, const QString & format)
  145. {
  146. t.clear();
  147. if (! j.isObject()) { return qx_bool(true); }
  148. QJsonObject obj = j.toObject(); QJsonValue val;
  149. t.reserve(static_cast<typename std::unordered_map<QString, Value>::size_type>(obj.count()));
  150. for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
  151. {
  152. QString key = itr.key(); Value value;
  153. qx::cvt::from_json(itr.value(), value, format);
  154. t.insert(std::make_pair(key, value));
  155. }
  156. return qx_bool(true);
  157. }
  158. };
  159. template <typename Value>
  160. struct QxConvert_ToJson< std::unordered_map<std::string, Value> >
  161. {
  162. static inline QJsonValue toJson(const std::unordered_map<std::string, Value> & t, const QString & format)
  163. {
  164. typedef typename std::unordered_map<std::string, Value>::const_iterator type_itr;
  165. QJsonObject obj; QJsonValue val;
  166. for (type_itr itr = t.begin(); itr != t.end(); ++itr)
  167. {
  168. #ifndef QT_NO_STL
  169. QString key = QString::fromStdString(itr->first);
  170. #else // QT_NO_STL
  171. QString key = QString::fromLatin1(itr->first.data(), int(itr->first.size()));
  172. #endif // QT_NO_STL
  173. val = qx::cvt::to_json(itr->second, format);
  174. obj.insert(key, val);
  175. }
  176. return QJsonValue(obj);
  177. }
  178. };
  179. template <typename Value>
  180. struct QxConvert_FromJson< std::unordered_map<std::string, Value> >
  181. {
  182. static inline qx_bool fromJson(const QJsonValue & j, std::unordered_map<std::string, Value> & t, const QString & format)
  183. {
  184. t.clear();
  185. if (! j.isObject()) { return qx_bool(true); }
  186. QJsonObject obj = j.toObject(); QJsonValue val;
  187. t.reserve(static_cast<typename std::unordered_map<std::string, Value>::size_type>(obj.count()));
  188. for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
  189. {
  190. QString key = itr.key(); Value value;
  191. qx::cvt::from_json(itr.value(), value, format);
  192. #ifndef QT_NO_STL
  193. std::string s = key.toStdString();
  194. #else // QT_NO_STL
  195. std::string s = key.toLatin1().constData();
  196. #endif // QT_NO_STL
  197. t.insert(std::make_pair(s, value));
  198. }
  199. return qx_bool(true);
  200. }
  201. };
  202. #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  203. template <typename Value>
  204. struct QxConvert_ToJson< std::unordered_map<std::wstring, Value> >
  205. {
  206. static inline QJsonValue toJson(const std::unordered_map<std::wstring, Value> & t, const QString & format)
  207. {
  208. typedef typename std::unordered_map<std::wstring, Value>::const_iterator type_itr;
  209. QJsonObject obj; QJsonValue val;
  210. for (type_itr itr = t.begin(); itr != t.end(); ++itr)
  211. {
  212. val = qx::cvt::to_json(itr->second, format);
  213. obj.insert(QString::fromStdWString(itr->first), val);
  214. }
  215. return QJsonValue(obj);
  216. }
  217. };
  218. template <typename Value>
  219. struct QxConvert_FromJson< std::unordered_map<std::wstring, Value> >
  220. {
  221. static inline qx_bool fromJson(const QJsonValue & j, std::unordered_map<std::wstring, Value> & t, const QString & format)
  222. {
  223. t.clear();
  224. if (! j.isObject()) { return qx_bool(true); }
  225. QJsonObject obj = j.toObject(); QJsonValue val;
  226. t.reserve(static_cast<typename std::unordered_map<std::wstring, Value>::size_type>(obj.count()));
  227. for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
  228. {
  229. QString key = itr.key(); Value value;
  230. qx::cvt::from_json(itr.value(), value, format);
  231. t.insert(std::make_pair(key.toStdWString(), value));
  232. }
  233. return qx_bool(true);
  234. }
  235. };
  236. #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  237. } // namespace detail
  238. } // namespace cvt
  239. } // namespace qx
  240. #endif // _QX_SERIALIZE_QJSON_STD_UNORDERED_MAP_H_
  241. #endif // _QX_NO_JSON