QxConvert_ToJson.inl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_ToJson< qx::trait::no_type > {
  36. static inline QJsonValue toJson(const qx::trait::no_type & t, const QString & format)
  37. { Q_UNUSED(t); Q_UNUSED(format); return QJsonValue(); } };
  38. template <> struct QxConvert_ToJson< bool > {
  39. static inline QJsonValue toJson(const bool & t, const QString & format)
  40. { Q_UNUSED(format); return QJsonValue(t); } };
  41. template <> struct QxConvert_ToJson< short > {
  42. static inline QJsonValue toJson(const short & t, const QString & format)
  43. { Q_UNUSED(format); return QJsonValue(static_cast<int>(t)); } };
  44. template <> struct QxConvert_ToJson< int > {
  45. static inline QJsonValue toJson(const int & t, const QString & format)
  46. { Q_UNUSED(format); return QJsonValue(t); } };
  47. template <> struct QxConvert_ToJson< long > {
  48. static inline QJsonValue toJson(const long & t, const QString & format)
  49. { Q_UNUSED(format); return QJsonValue(static_cast<double>(t)); } };
  50. template <> struct QxConvert_ToJson< long long > {
  51. static inline QJsonValue toJson(const long long & t, const QString & format)
  52. { Q_UNUSED(format); return QJsonValue(static_cast<double>(t)); } };
  53. template <> struct QxConvert_ToJson< float > {
  54. static inline QJsonValue toJson(const float & t, const QString & format)
  55. { Q_UNUSED(format); return QJsonValue(static_cast<double>(t)); } };
  56. template <> struct QxConvert_ToJson< double > {
  57. static inline QJsonValue toJson(const double & t, const QString & format)
  58. { Q_UNUSED(format); return QJsonValue(t); } };
  59. template <> struct QxConvert_ToJson< unsigned short > {
  60. static inline QJsonValue toJson(const unsigned short & t, const QString & format)
  61. { Q_UNUSED(format); return QJsonValue(static_cast<int>(t)); } };
  62. template <> struct QxConvert_ToJson< unsigned int > {
  63. static inline QJsonValue toJson(const unsigned int & t, const QString & format)
  64. { Q_UNUSED(format); return QJsonValue(static_cast<int>(t)); } };
  65. template <> struct QxConvert_ToJson< unsigned long > {
  66. static inline QJsonValue toJson(const unsigned long & t, const QString & format)
  67. { Q_UNUSED(format); return QJsonValue(static_cast<double>(t)); } };
  68. template <> struct QxConvert_ToJson< unsigned long long > {
  69. static inline QJsonValue toJson(const unsigned long long & t, const QString & format)
  70. { Q_UNUSED(format); return QJsonValue(static_cast<double>(t)); } };
  71. template <> struct QxConvert_ToJson< QDateTime > {
  72. static inline QJsonValue toJson(const QDateTime & t, const QString & format)
  73. {
  74. #ifdef _QX_ENABLE_MONGODB
  75. if (t.isValid() && format.startsWith("mongodb"))
  76. {
  77. QString dt = t.toString(QX_JSON_DATE_TIME_FORMAT); if (dt.count() <= QX_JSON_DATE_TIME_FORMAT_SIZE) { dt += "Z"; }
  78. QJsonObject obj; obj.insert("$date", QJsonValue(dt)); return QJsonValue(obj);
  79. }
  80. #endif // _QX_ENABLE_MONGODB
  81. Q_UNUSED(format); if (t.isValid()) { return QJsonValue(t.toString(QX_JSON_DATE_TIME_FORMAT)); }; return QJsonValue(); }
  82. };
  83. template <> struct QxConvert_ToJson< QDate > {
  84. static inline QJsonValue toJson(const QDate & t, const QString & format)
  85. {
  86. #ifdef _QX_ENABLE_MONGODB
  87. if (t.isValid() && format.startsWith("mongodb"))
  88. { QDateTime dt(t); return QxConvert_ToJson<QDateTime>::toJson(dt, format); }
  89. #endif // _QX_ENABLE_MONGODB
  90. Q_UNUSED(format); if (t.isValid()) { return QJsonValue(t.toString(Qt::ISODate)); }; return QJsonValue(); }
  91. };
  92. template <> struct QxConvert_ToJson< QTime > {
  93. static inline QJsonValue toJson(const QTime & t, const QString & format)
  94. { Q_UNUSED(format); if (t.isValid()) { return QJsonValue(t.toString(QX_JSON_DATE_TIME_FORMAT)); }; return QJsonValue(); } };
  95. template <> struct QxConvert_ToJson< QByteArray > {
  96. static inline QJsonValue toJson(const QByteArray & t, const QString & format)
  97. { Q_UNUSED(format); QString s = t.toBase64(); return QJsonValue(s); } };
  98. template <> struct QxConvert_ToJson< QString > {
  99. static inline QJsonValue toJson(const QString & t, const QString & format)
  100. {
  101. #ifdef _QX_ENABLE_MONGODB
  102. if (t.startsWith("qx_oid:") && format.startsWith("mongodb"))
  103. { QJsonObject obj; obj.insert("$oid", QJsonValue(t.right(t.size() - 7))); return QJsonValue(obj); }
  104. #endif // _QX_ENABLE_MONGODB
  105. Q_UNUSED(format); return QJsonValue(t);
  106. } };
  107. template <> struct QxConvert_ToJson< QVariant > {
  108. static inline QJsonValue toJson(const QVariant & t, const QString & format)
  109. { Q_UNUSED(format); return QJsonValue::fromVariant(t); } };
  110. template <> struct QxConvert_ToJson< QUuid > {
  111. static inline QJsonValue toJson(const QUuid & t, const QString & format)
  112. { Q_UNUSED(format); return QJsonValue(t.toString()); } };
  113. template <> struct QxConvert_ToJson< qx::QxDateNeutral > {
  114. static inline QJsonValue toJson(const qx::QxDateNeutral & t, const QString & format)
  115. { Q_UNUSED(format); return QJsonValue(t.toNeutral()); } };
  116. template <> struct QxConvert_ToJson< qx::QxTimeNeutral > {
  117. static inline QJsonValue toJson(const qx::QxTimeNeutral & t, const QString & format)
  118. { Q_UNUSED(format); return QJsonValue(t.toNeutral()); } };
  119. template <> struct QxConvert_ToJson< qx::QxDateTimeNeutral > {
  120. static inline QJsonValue toJson(const qx::QxDateTimeNeutral & t, const QString & format)
  121. { Q_UNUSED(format); return QJsonValue(t.toNeutral()); } };
  122. template <> struct QxConvert_ToJson< std::string > {
  123. static inline QJsonValue toJson(const std::string & t, const QString & format)
  124. #ifndef QT_NO_STL
  125. { Q_UNUSED(format); return QJsonValue(QString::fromStdString(t)); } };
  126. #else // QT_NO_STL
  127. { Q_UNUSED(format); return QJsonValue(QString::fromLatin1(t.data(), int(t.size()))); } };
  128. #endif // QT_NO_STL
  129. template <> struct QxConvert_ToJson< std::wstring > {
  130. static inline QJsonValue toJson(const std::wstring & t, const QString & format)
  131. #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  132. { Q_UNUSED(format); return QJsonValue(QString::fromStdWString(t)); } };
  133. #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  134. { Q_UNUSED(format); Q_UNUSED(t); qAssert(false); /* Need STL compatibility ! */ return QJsonValue(); } };
  135. #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  136. template <> struct QxConvert_ToJson< qx_bool > {
  137. static inline QJsonValue toJson(const qx_bool & t, const QString & format)
  138. { Q_UNUSED(format); QJsonObject obj; obj["value"] = t.getValue(); obj["code"] = static_cast<double>(t.getCode()); obj["desc"] = t.getDesc(); return QJsonValue(obj); } };
  139. #ifdef _QX_ENABLE_BOOST
  140. template <typename T> struct QxConvert_ToJson< boost::optional<T> > {
  141. static inline QJsonValue toJson(const boost::optional<T> & t, const QString & format)
  142. { if (t) { return qx::cvt::to_json((* t), format); }; return QJsonValue(); } };
  143. #endif // _QX_ENABLE_BOOST
  144. } // namespace detail
  145. } // namespace cvt
  146. } // namespace qx
  147. #endif // _QX_NO_JSON