jsonserializer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef QTJSONSERIALIZER_JSONSERIALIZER_H
  2. #define QTJSONSERIALIZER_JSONSERIALIZER_H
  3. #include "qtjsonserializer_global.h"
  4. #include "serializerbase.h"
  5. #include <QtCore/qjsonobject.h>
  6. #include <QtCore/qjsonarray.h>
  7. #include <QtCore/qjsondocument.h>
  8. namespace QtJsonSerializer {
  9. class JsonSerializerPrivate;
  10. //! A class to serialize and deserialize c++ classes to and from JSON
  11. class Q_JSONSERIALIZER_EXPORT JsonSerializer : public SerializerBase
  12. {
  13. Q_OBJECT
  14. //! Specifies the format, in which bytearray should be converter to a JSON string
  15. Q_PROPERTY(ByteArrayFormat byteArrayFormat READ byteArrayFormat WRITE setByteArrayFormat NOTIFY byteArrayFormatChanged)
  16. //! Specify whether deserializing a QByteArray should verify the data as base64 instead of silent discarding
  17. Q_PROPERTY(bool validateBase64 READ validateBase64 WRITE setValidateBase64 NOTIFY validateBase64Changed)
  18. public:
  19. //! Defines the different supported bytearray formats
  20. enum class ByteArrayFormat {
  21. Base64, //!< Data is encoded as base64 string with padding
  22. Base64url, //!< Data is encoded as base64url string, without padding
  23. Base16 //!< Data is encoded as hexadecimal string (any case)
  24. };
  25. Q_ENUM(ByteArrayFormat)
  26. //! Default constructor
  27. explicit JsonSerializer(QObject *parent = nullptr);
  28. //! Serializers a QVariant value to a QJsonValue
  29. QJsonValue serialize(const QVariant &data) const;
  30. //! Serializers a QVariant value to a device
  31. void serializeTo(QIODevice *device, const QVariant &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
  32. //! Serializers a QVariant value to a byte array
  33. QByteArray serializeTo(const QVariant &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
  34. //! Serializers a generic c++ type to json
  35. template <typename T>
  36. typename QtJsonSerializer::__private::json_type<T>::type serialize(const T &data) const;
  37. //! Serializers a generic c++ type to a device
  38. template <typename T>
  39. void serializeTo(QIODevice *device, const T &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
  40. //! Serializers a generic c++ type to a byte array
  41. template <typename T>
  42. QByteArray serializeTo(const T &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
  43. //! Deserializes a QJsonValue to a QVariant value, based on the given type id
  44. QVariant deserialize(const QJsonValue &json, int metaTypeId, QObject *parent = nullptr) const;
  45. //! Deserializes data from a device to a QVariant value, based on the given type id
  46. QVariant deserializeFrom(QIODevice *device, int metaTypeId, QObject *parent = nullptr) const;
  47. //! Deserializes data from a device to a QVariant value, based on the given type id
  48. QVariant deserializeFrom(const QByteArray &data, int metaTypeId, QObject *parent = nullptr) const;
  49. //! Deserializes a json to the given c++ type
  50. template <typename T>
  51. T deserialize(const typename QtJsonSerializer::__private::json_type<T>::type &json, QObject *parent = nullptr) const;
  52. //! Deserializes data from a device to the given c++ type
  53. template <typename T>
  54. T deserializeFrom(QIODevice *device, QObject *parent = nullptr) const;
  55. //! Deserializes data from a byte array to the given c++ type
  56. template <typename T>
  57. T deserializeFrom(const QByteArray &data, QObject *parent = nullptr) const;
  58. //! @readAcFn{QJsonSerializer::byteArrayFormat}
  59. ByteArrayFormat byteArrayFormat() const;
  60. //! @readAcFn{QJsonSerializer::validateBase64}
  61. bool validateBase64() const;
  62. std::variant<QCborValue, QJsonValue> serializeGeneric(const QVariant &value) const override;
  63. QVariant deserializeGeneric(const std::variant<QCborValue, QJsonValue> &value, int metaTypeId, QObject *parent) const override;
  64. public Q_SLOTS:
  65. //! @writeAcFn{QJsonSerializer::byteArrayFormat}
  66. void setByteArrayFormat(ByteArrayFormat byteArrayFormat);
  67. //! @writeAcFn{QJsonSerializer::validateBase64}
  68. void setValidateBase64(bool validateBase64);
  69. Q_SIGNALS:
  70. //! @notifyAcFn{QJsonSerializer::byteArrayFormat}
  71. void byteArrayFormatChanged(ByteArrayFormat byteArrayFormat, QPrivateSignal);
  72. //! @notifyAcFn{QJsonSerializer::validateBase64}
  73. void validateBase64Changed(bool validateBase64, QPrivateSignal);
  74. protected:
  75. // protected implementation -> internal use for the type converters
  76. bool jsonMode() const override;
  77. QCborTag typeTag(int metaTypeId) const override;
  78. QList<int> typesForTag(QCborTag tag) const override;
  79. private:
  80. Q_DECLARE_PRIVATE(JsonSerializer)
  81. };
  82. // ------------- Generic Implementation -------------
  83. template<typename T>
  84. typename QtJsonSerializer::__private::json_type<T>::type JsonSerializer::serialize(const T &data) const
  85. {
  86. static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
  87. return __private::json_type<T>::convert(serialize(__private::variant_helper<T>::toVariant(data)));
  88. }
  89. template<typename T>
  90. void JsonSerializer::serializeTo(QIODevice *device, const T &data, QJsonDocument::JsonFormat format) const
  91. {
  92. static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
  93. serializeTo(device, __private::variant_helper<T>::toVariant(data), format);
  94. }
  95. template<typename T>
  96. QByteArray JsonSerializer::serializeTo(const T &data, QJsonDocument::JsonFormat format) const
  97. {
  98. static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
  99. return serializeTo(__private::variant_helper<T>::toVariant(data), format);
  100. }
  101. template<typename T>
  102. T JsonSerializer::deserialize(const typename __private::json_type<T>::type &json, QObject *parent) const
  103. {
  104. static_assert(__private::is_serializable<T>::value, "T cannot be deserialized");
  105. return __private::variant_helper<T>::fromVariant(deserialize(json, qMetaTypeId<T>(), parent));
  106. }
  107. template<typename T>
  108. T JsonSerializer::deserializeFrom(QIODevice *device, QObject *parent) const
  109. {
  110. static_assert(__private::is_serializable<T>::value, "T cannot be deserialized");
  111. return __private::variant_helper<T>::fromVariant(deserializeFrom(device, qMetaTypeId<T>(), parent));
  112. }
  113. template<typename T>
  114. T JsonSerializer::deserializeFrom(const QByteArray &data, QObject *parent) const
  115. {
  116. static_assert(QtJsonSerializer::__private::is_serializable<T>::value, "T cannot be deserialized");
  117. return QtJsonSerializer::__private::variant_helper<T>::fromVariant(deserializeFrom(data, qMetaTypeId<T>(), parent));
  118. }
  119. }
  120. #endif // QTJSONSERIALIZER_JSONSERIALIZER_H