QxConvert_FromJson.inl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. namespace qx {
  33. namespace cvt {
  34. namespace detail {
  35. template <> struct QxConvert_FromJson< qx::trait::no_type > {
  36. static inline qx_bool fromJson(const QJsonValue & j, qx::trait::no_type & t, const QString & format)
  37. { Q_UNUSED(j); Q_UNUSED(t); Q_UNUSED(format); return qx_bool(true); } };
  38. template <> struct QxConvert_FromJson< bool > {
  39. static inline qx_bool fromJson(const QJsonValue & j, bool & t, const QString & format)
  40. { Q_UNUSED(format); t = j.toBool(); return qx_bool(true); } };
  41. template <> struct QxConvert_FromJson< short > {
  42. static inline qx_bool fromJson(const QJsonValue & j, short & t, const QString & format)
  43. { Q_UNUSED(format); t = static_cast<short>(qRound(j.toDouble())); return qx_bool(true); } };
  44. template <> struct QxConvert_FromJson< int > {
  45. static inline qx_bool fromJson(const QJsonValue & j, int & t, const QString & format)
  46. { Q_UNUSED(format); t = qRound(j.toDouble()); return qx_bool(true); } };
  47. template <> struct QxConvert_FromJson< long > {
  48. static inline qx_bool fromJson(const QJsonValue & j, long & t, const QString & format)
  49. { Q_UNUSED(format); t = static_cast<long>(qRound64(j.toDouble())); return qx_bool(true); } };
  50. template <> struct QxConvert_FromJson< long long > {
  51. static inline qx_bool fromJson(const QJsonValue & j, long long & t, const QString & format)
  52. { Q_UNUSED(format); t = static_cast<long long>(qRound64(j.toDouble())); return qx_bool(true); } };
  53. template <> struct QxConvert_FromJson< float > {
  54. static inline qx_bool fromJson(const QJsonValue & j, float & t, const QString & format)
  55. { Q_UNUSED(format); t = static_cast<float>(j.toDouble()); return qx_bool(true); } };
  56. template <> struct QxConvert_FromJson< double > {
  57. static inline qx_bool fromJson(const QJsonValue & j, double & t, const QString & format)
  58. { Q_UNUSED(format); t = j.toDouble(); return qx_bool(true); } };
  59. template <> struct QxConvert_FromJson< unsigned short > {
  60. static inline qx_bool fromJson(const QJsonValue & j, unsigned short & t, const QString & format)
  61. { Q_UNUSED(format); t = static_cast<unsigned short>(qRound(j.toDouble())); return qx_bool(true); } };
  62. template <> struct QxConvert_FromJson< unsigned int > {
  63. static inline qx_bool fromJson(const QJsonValue & j, unsigned int & t, const QString & format)
  64. { Q_UNUSED(format); t = static_cast<unsigned int>(qRound(j.toDouble())); return qx_bool(true); } };
  65. template <> struct QxConvert_FromJson< unsigned long > {
  66. static inline qx_bool fromJson(const QJsonValue & j, unsigned long & t, const QString & format)
  67. { Q_UNUSED(format); t = static_cast<unsigned long>(qRound64(j.toDouble())); return qx_bool(true); } };
  68. template <> struct QxConvert_FromJson< unsigned long long > {
  69. static inline qx_bool fromJson(const QJsonValue & j, unsigned long long & t, const QString & format)
  70. { Q_UNUSED(format); t = static_cast<unsigned long long>(qRound64(j.toDouble())); return qx_bool(true); } };
  71. template <> struct QxConvert_FromJson< QDateTime > {
  72. static inline qx_bool fromJson(const QJsonValue & j, QDateTime & t, const QString & format)
  73. {
  74. #ifdef _QX_ENABLE_MONGODB
  75. if (j.isObject() && format.startsWith("mongodb"))
  76. {
  77. QJsonObject obj = j.toObject(); QString dt;
  78. if (obj.contains("$date")) { dt = obj.value("$date").toString(); }
  79. if (! dt.isEmpty()) { t = QDateTime::fromString(dt, QX_JSON_DATE_TIME_FORMAT); return qx_bool(true); }
  80. }
  81. #endif // _QX_ENABLE_MONGODB
  82. Q_UNUSED(format); t = (j.isNull() ? QDateTime() : QDateTime::fromString(j.toString(), QX_JSON_DATE_TIME_FORMAT)); return qx_bool(true); }
  83. };
  84. template <> struct QxConvert_FromJson< QDate > {
  85. static inline qx_bool fromJson(const QJsonValue & j, QDate & t, const QString & format)
  86. {
  87. #ifdef _QX_ENABLE_MONGODB
  88. if (j.isObject() && format.startsWith("mongodb"))
  89. { QDateTime dt; QxConvert_FromJson<QDateTime>::fromJson(j, dt, format); t = dt.date(); return qx_bool(true); }
  90. #endif // _QX_ENABLE_MONGODB
  91. Q_UNUSED(format); t = (j.isNull() ? QDate() : QDate::fromString(j.toString(), Qt::ISODate)); return qx_bool(true); }
  92. };
  93. template <> struct QxConvert_FromJson< QTime > {
  94. static inline qx_bool fromJson(const QJsonValue & j, QTime & t, const QString & format)
  95. { Q_UNUSED(format); t = (j.isNull() ? QTime() : QTime::fromString(j.toString(), QX_JSON_DATE_TIME_FORMAT)); return qx_bool(true); } };
  96. template <> struct QxConvert_FromJson< QByteArray > {
  97. static inline qx_bool fromJson(const QJsonValue & j, QByteArray & t, const QString & format)
  98. { Q_UNUSED(format); t = QByteArray::fromBase64(j.toString().toLatin1()); return qx_bool(true); } };
  99. template <> struct QxConvert_FromJson< QString > {
  100. static inline qx_bool fromJson(const QJsonValue & j, QString & t, const QString & format)
  101. {
  102. Q_UNUSED(format); t = j.toString();
  103. #ifdef _QX_ENABLE_MONGODB
  104. if (t.isEmpty() && j.isObject() && format.startsWith("mongodb"))
  105. {
  106. QJsonObject obj = j.toObject();
  107. if (obj.contains("$oid")) { t = obj.value("$oid").toString(); }
  108. if (! t.isEmpty()) { t = "qx_oid:" + t; }
  109. }
  110. #endif // _QX_ENABLE_MONGODB
  111. return qx_bool(true);
  112. } };
  113. template <> struct QxConvert_FromJson< QVariant > {
  114. static inline qx_bool fromJson(const QJsonValue & j, QVariant & t, const QString & format)
  115. { Q_UNUSED(format); t = j.toVariant(); return qx_bool(true); } };
  116. template <> struct QxConvert_FromJson< QUuid > {
  117. static inline qx_bool fromJson(const QJsonValue & j, QUuid & t, const QString & format)
  118. { Q_UNUSED(format); t = QUuid(j.toString()); return qx_bool(true); } };
  119. template <> struct QxConvert_FromJson< qx::QxDateNeutral > {
  120. static inline qx_bool fromJson(const QJsonValue & j, qx::QxDateNeutral & t, const QString & format)
  121. { Q_UNUSED(format); t = qx::QxDateNeutral::fromNeutral(j.toString()); return qx_bool(true); } };
  122. template <> struct QxConvert_FromJson< qx::QxTimeNeutral > {
  123. static inline qx_bool fromJson(const QJsonValue & j, qx::QxTimeNeutral & t, const QString & format)
  124. { Q_UNUSED(format); t = qx::QxTimeNeutral::fromNeutral(j.toString()); return qx_bool(true); } };
  125. template <> struct QxConvert_FromJson< qx::QxDateTimeNeutral > {
  126. static inline qx_bool fromJson(const QJsonValue & j, qx::QxDateTimeNeutral & t, const QString & format)
  127. { Q_UNUSED(format); t = qx::QxDateTimeNeutral::fromNeutral(j.toString()); return qx_bool(true); } };
  128. template <> struct QxConvert_FromJson< std::string > {
  129. static inline qx_bool fromJson(const QJsonValue & j, std::string & t, const QString & format)
  130. #ifndef QT_NO_STL
  131. { Q_UNUSED(format); t = j.toString().toStdString(); return qx_bool(true); } };
  132. #else // QT_NO_STL
  133. { Q_UNUSED(format); t = j.toString().toLatin1().constData(); return qx_bool(true); } };
  134. #endif // QT_NO_STL
  135. template <> struct QxConvert_FromJson< std::wstring > {
  136. static inline qx_bool fromJson(const QJsonValue & j, std::wstring & t, const QString & format)
  137. #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  138. { Q_UNUSED(format); t = j.toString().toStdWString(); return qx_bool(true); } };
  139. #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  140. { Q_UNUSED(format); Q_UNUSED(t); Q_UNUSED(j); qAssert(false); /* Need STL compatibility ! */ return qx_bool(true); } };
  141. #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  142. template <> struct QxConvert_FromJson< qx_bool > {
  143. static inline qx_bool fromJson(const QJsonValue & j, qx_bool & t, const QString & format)
  144. {
  145. Q_UNUSED(format);
  146. t = qx_bool();
  147. if (j.isObject())
  148. {
  149. QJsonObject obj = j.toObject();
  150. t.setValue(obj["value"].toBool());
  151. t.setCode(static_cast<long>(qRound64(obj["code"].toDouble())));
  152. t.setDesc(obj["desc"].toString());
  153. }
  154. return qx_bool(true);
  155. } };
  156. #ifdef _QX_ENABLE_BOOST
  157. template <typename T> struct QxConvert_FromJson< boost::optional<T> > {
  158. static inline qx_bool fromJson(const QJsonValue & j, boost::optional<T> & t, const QString & format)
  159. {
  160. if (j.isNull()) { t = boost::none; return qx_bool(true); }
  161. else if (! t) { t = T(); }
  162. return qx::cvt::from_json(j, (* t), format);
  163. } };
  164. #endif // _QX_ENABLE_BOOST
  165. } // namespace detail
  166. } // namespace cvt
  167. } // namespace qx
  168. #endif // _QX_NO_JSON