QxSerializeQJson_QHash.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_QHASH_H_
  33. #define _QX_SERIALIZE_QJSON_QHASH_H_
  34. #ifdef _MSC_VER
  35. #pragma once
  36. #endif
  37. /*!
  38. * \file QxSerializeQJson_QHash.h
  39. * \author Lionel Marty
  40. * \ingroup QxSerialize
  41. * \brief Provide a Qt QJson serialization method (save/load) for type QHash<Key, Value>
  42. */
  43. #include <QtCore/qjsonvalue.h>
  44. #include <QtCore/qjsonobject.h>
  45. #include <QtCore/qjsonarray.h>
  46. #include <QtCore/qhash.h>
  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< QHash<Key, Value> >
  54. {
  55. static inline QJsonValue toJson(const QHash<Key, Value> & t, const QString & format)
  56. {
  57. QJsonArray arr; QJsonValue val;
  58. QHashIterator<Key, Value> itr(t);
  59. while (itr.hasNext())
  60. {
  61. itr.next(); QJsonObject obj;
  62. val = qx::cvt::to_json(itr.key(), format); obj.insert("key", val);
  63. val = qx::cvt::to_json(itr.value(), 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< QHash<Key, Value> >
  71. {
  72. static inline qx_bool fromJson(const QJsonValue & j, QHash<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(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(key, value);
  85. }
  86. return qx_bool(true);
  87. }
  88. };
  89. template <typename Value>
  90. struct QxConvert_ToJson< QHash<QString, Value> >
  91. {
  92. static inline QJsonValue toJson(const QHash<QString, Value> & t, const QString & format)
  93. {
  94. QJsonObject obj; QJsonValue val;
  95. QHashIterator<QString, Value> itr(t);
  96. while (itr.hasNext())
  97. {
  98. itr.next();
  99. val = qx::cvt::to_json(itr.value(), format);
  100. obj.insert(itr.key(), val);
  101. }
  102. return QJsonValue(obj);
  103. }
  104. };
  105. template <typename Value>
  106. struct QxConvert_FromJson< QHash<QString, Value> >
  107. {
  108. static inline qx_bool fromJson(const QJsonValue & j, QHash<QString, Value> & t, const QString & format)
  109. {
  110. t.clear();
  111. if (! j.isObject()) { return qx_bool(true); }
  112. QJsonObject obj = j.toObject(); QJsonValue val;
  113. t.reserve(obj.count());
  114. for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
  115. {
  116. QString key = itr.key(); Value value;
  117. qx::cvt::from_json(itr.value(), value, format);
  118. t.insert(key, value);
  119. }
  120. return qx_bool(true);
  121. }
  122. };
  123. template <typename Value>
  124. struct QxConvert_ToJson< QHash<std::string, Value> >
  125. {
  126. static inline QJsonValue toJson(const QHash<std::string, Value> & t, const QString & format)
  127. {
  128. QJsonObject obj; QJsonValue val;
  129. QHashIterator<std::string, Value> itr(t);
  130. while (itr.hasNext())
  131. {
  132. itr.next();
  133. #ifndef QT_NO_STL
  134. QString key = QString::fromStdString(itr.key());
  135. #else // QT_NO_STL
  136. std::string s = itr.key();
  137. QString key = QString::fromLatin1(s.data(), int(s.size()));
  138. #endif // QT_NO_STL
  139. val = qx::cvt::to_json(itr.value(), format);
  140. obj.insert(key, val);
  141. }
  142. return QJsonValue(obj);
  143. }
  144. };
  145. template <typename Value>
  146. struct QxConvert_FromJson< QHash<std::string, Value> >
  147. {
  148. static inline qx_bool fromJson(const QJsonValue & j, QHash<std::string, Value> & t, const QString & format)
  149. {
  150. t.clear();
  151. if (! j.isObject()) { return qx_bool(true); }
  152. QJsonObject obj = j.toObject(); QJsonValue val;
  153. t.reserve(obj.count());
  154. for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
  155. {
  156. QString key = itr.key(); Value value;
  157. qx::cvt::from_json(itr.value(), value, format);
  158. #ifndef QT_NO_STL
  159. std::string s = key.toStdString();
  160. #else // QT_NO_STL
  161. std::string s = key.toLatin1().constData();
  162. #endif // QT_NO_STL
  163. t.insert(s, value);
  164. }
  165. return qx_bool(true);
  166. }
  167. };
  168. #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  169. template <typename Value>
  170. struct QxConvert_ToJson< QHash<std::wstring, Value> >
  171. {
  172. static inline QJsonValue toJson(const QHash<std::wstring, Value> & t, const QString & format)
  173. {
  174. QJsonObject obj; QJsonValue val;
  175. QHashIterator<std::wstring, Value> itr(t);
  176. while (itr.hasNext())
  177. {
  178. itr.next();
  179. val = qx::cvt::to_json(itr.value(), format);
  180. obj.insert(QString::fromStdWString(itr.key()), val);
  181. }
  182. return QJsonValue(obj);
  183. }
  184. };
  185. template <typename Value>
  186. struct QxConvert_FromJson< QHash<std::wstring, Value> >
  187. {
  188. static inline qx_bool fromJson(const QJsonValue & j, QHash<std::wstring, Value> & t, const QString & format)
  189. {
  190. t.clear();
  191. if (! j.isObject()) { return qx_bool(true); }
  192. QJsonObject obj = j.toObject(); QJsonValue val;
  193. t.reserve(obj.count());
  194. for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
  195. {
  196. QString key = itr.key(); Value value;
  197. qx::cvt::from_json(itr.value(), value, format);
  198. t.insert(key.toStdWString(), value);
  199. }
  200. return qx_bool(true);
  201. }
  202. };
  203. #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  204. } // namespace detail
  205. } // namespace cvt
  206. } // namespace qx
  207. #endif // _QX_SERIALIZE_QJSON_QHASH_H_
  208. #endif // _QX_NO_JSON