QxSerializeQJson.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_H_
  33. #define _QX_SERIALIZE_QJSON_H_
  34. #ifdef _MSC_VER
  35. #pragma once
  36. #endif
  37. /*!
  38. * \file QxSerializeQJson.h
  39. * \author Lionel Marty
  40. * \ingroup QxSerialize
  41. * \brief Provide a serialization engine with Qt QJson classes (this feature requires Qt5)
  42. */
  43. #include <QtCore/qjsonvalue.h>
  44. #include <QtCore/qjsonobject.h>
  45. #include <QtCore/qjsonarray.h>
  46. #include <QtCore/qjsondocument.h>
  47. #include <QtCore/qfile.h>
  48. #include <QxCommon/QxBool.h>
  49. #include <QxConvert/QxConvert.h>
  50. namespace qx {
  51. namespace serialization {
  52. /*!
  53. * \ingroup QxSerialize
  54. * \brief serialize a class registered into QxOrm context using Qt QJson serialization engine (this feature requires Qt5)
  55. */
  56. namespace json {
  57. template <class T>
  58. inline QByteArray to_byte_array(const T & obj, void * owner = NULL, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
  59. {
  60. Q_UNUSED(flags); Q_UNUSED(owner);
  61. QJsonValue val = qx::cvt::to_json(obj, format);
  62. QJsonDocument doc = (val.isArray() ? QJsonDocument(val.toArray()) : QJsonDocument(val.toObject()));
  63. return doc.toJson();
  64. }
  65. template <class T>
  66. inline qx_bool from_byte_array(T & obj, const QByteArray & data, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
  67. {
  68. Q_UNUSED(flags);
  69. QJsonParseError err;
  70. QJsonDocument doc = QJsonDocument::fromJson(data, (& err));
  71. if (err.error != QJsonParseError::NoError)
  72. { return qx_bool(false, static_cast<long>(err.error), err.errorString()); }
  73. QJsonValue val = (doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object()));
  74. return qx::cvt::from_json(val, obj, format);
  75. }
  76. template <class T>
  77. inline QString to_string(const T & obj, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
  78. { return QString::fromUtf8(qx::serialization::json::to_byte_array(obj, NULL, flags, format)); }
  79. template <class T>
  80. inline qx_bool from_string(T & obj, const QString & sString, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
  81. { return qx::serialization::json::from_byte_array(obj, sString.toUtf8(), flags, format); }
  82. template <class T>
  83. inline qx_bool to_file(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
  84. {
  85. QByteArray data = qx::serialization::json::to_byte_array(obj, NULL, flags, format);
  86. QFile file(sFileName);
  87. if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  88. { return qx_bool(false, "cannot open file : " + sFileName); }
  89. file.write(data); file.close();
  90. return qx_bool(true);
  91. }
  92. template <class T>
  93. inline qx_bool from_file(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
  94. {
  95. QFile file(sFileName);
  96. if (! file.open(QIODevice::ReadOnly))
  97. { return qx_bool(false, "cannot open file : " + sFileName); }
  98. QByteArray data = file.readAll(); file.close();
  99. return qx::serialization::json::from_byte_array(obj, data, flags, format);
  100. }
  101. template <class T>
  102. inline qx_bool to_file_compressed(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, int iCompressionLevel = -1, const QString & format = QString())
  103. {
  104. QByteArray data = qx::serialization::json::to_byte_array(obj, NULL, flags, format);
  105. QByteArray compressed = qCompress(data, iCompressionLevel);
  106. QFile file(sFileName);
  107. if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  108. { return qx_bool(false, "cannot open file : " + sFileName); }
  109. file.write(compressed); file.close();
  110. return qx_bool(true);
  111. }
  112. template <class T>
  113. inline qx_bool from_file_compressed(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
  114. {
  115. QFile file(sFileName);
  116. if (! file.open(QIODevice::ReadOnly))
  117. { return qx_bool(false, "cannot open file : " + sFileName); }
  118. QByteArray data = file.readAll(); file.close();
  119. QByteArray uncompressed = qUncompress(data);
  120. return qx::serialization::json::from_byte_array(obj, uncompressed, flags, format);
  121. }
  122. } // namespace json
  123. } // namespace serialization
  124. } // namespace qx
  125. #endif // _QX_SERIALIZE_QJSON_H_
  126. #endif // _QX_NO_JSON