QtJsonSerializer  4.0.0
A library to perform generic seralization and deserialization of QObjects
jsonserializer.h
1 #ifndef QTJSONSERIALIZER_JSONSERIALIZER_H
2 #define QTJSONSERIALIZER_JSONSERIALIZER_H
3 
4 #include "QtJsonSerializer/qtjsonserializer_global.h"
5 #include "QtJsonSerializer/serializerbase.h"
6 
7 #include <QtCore/qjsonobject.h>
8 #include <QtCore/qjsonarray.h>
9 #include <QtCore/qjsondocument.h>
10 
11 namespace QtJsonSerializer {
12 
13 class JsonSerializerPrivate;
15 class Q_JSONSERIALIZER_EXPORT JsonSerializer : public SerializerBase
16 {
17  Q_OBJECT
18 
20  Q_PROPERTY(ByteArrayFormat byteArrayFormat READ byteArrayFormat WRITE setByteArrayFormat NOTIFY byteArrayFormatChanged)
22  Q_PROPERTY(bool validateBase64 READ validateBase64 WRITE setValidateBase64 NOTIFY validateBase64Changed)
23 
24 public:
26  enum class ByteArrayFormat {
27  Base64,
28  Base64url,
29  Base16
30  };
31  Q_ENUM(ByteArrayFormat)
32 
33 
34  explicit JsonSerializer(QObject *parent = nullptr);
35 
37  QJsonValue serialize(const QVariant &data) const;
39  void serializeTo(QIODevice *device, const QVariant &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
41  QByteArray serializeTo(const QVariant &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
42 
44  template <typename T>
45  typename QtJsonSerializer::__private::json_type<T>::type serialize(const T &data) const;
47  template <typename T>
48  void serializeTo(QIODevice *device, const T &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
50  template <typename T>
51  QByteArray serializeTo(const T &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
52 
54  QVariant deserialize(const QJsonValue &json, int metaTypeId, QObject *parent = nullptr) const;
56  QVariant deserializeFrom(QIODevice *device, int metaTypeId, QObject *parent = nullptr) const;
58  QVariant deserializeFrom(const QByteArray &data, int metaTypeId, QObject *parent = nullptr) const;
59 
61  template <typename T>
62  T deserialize(const typename QtJsonSerializer::__private::json_type<T>::type &json, QObject *parent = nullptr) const;
64  template <typename T>
65  T deserializeFrom(QIODevice *device, QObject *parent = nullptr) const;
67  template <typename T>
68  T deserializeFrom(const QByteArray &data, QObject *parent = nullptr) const;
69 
71  ByteArrayFormat byteArrayFormat() const;
73  bool validateBase64() const;
74 
75  std::variant<QCborValue, QJsonValue> serializeGeneric(const QVariant &value) const override;
76  QVariant deserializeGeneric(const std::variant<QCborValue, QJsonValue> &value, int metaTypeId, QObject *parent) const override;
77 
78 public Q_SLOTS:
80  void setByteArrayFormat(ByteArrayFormat byteArrayFormat);
82  void setValidateBase64(bool validateBase64);
83 
84 Q_SIGNALS:
86  void byteArrayFormatChanged(ByteArrayFormat byteArrayFormat, QPrivateSignal);
88  void validateBase64Changed(bool validateBase64, QPrivateSignal);
89 
90 protected:
91  // protected implementation -> internal use for the type converters
92  bool jsonMode() const override;
93  QCborTag typeTag(int metaTypeId) const override;
94  QList<int> typesForTag(QCborTag tag) const override;
95 
96 private:
97  Q_DECLARE_PRIVATE(JsonSerializer)
98 };
99 
100 // ------------- Generic Implementation -------------
101 
102 template<typename T>
103 typename QtJsonSerializer::__private::json_type<T>::type JsonSerializer::serialize(const T &data) const
104 {
105  static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
106  return __private::json_type<T>::convert(serialize(__private::variant_helper<T>::toVariant(data)));
107 }
108 
109 template<typename T>
110 void JsonSerializer::serializeTo(QIODevice *device, const T &data, QJsonDocument::JsonFormat format) const
111 {
112  static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
113  serializeTo(device, __private::variant_helper<T>::toVariant(data), format);
114 }
115 
116 template<typename T>
117 QByteArray JsonSerializer::serializeTo(const T &data, QJsonDocument::JsonFormat format) const
118 {
119  static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
120  return serializeTo(__private::variant_helper<T>::toVariant(data), format);
121 }
122 
123 template<typename T>
124 T JsonSerializer::deserialize(const typename __private::json_type<T>::type &json, QObject *parent) const
125 {
126  static_assert(__private::is_serializable<T>::value, "T cannot be deserialized");
127  return __private::variant_helper<T>::fromVariant(deserialize(json, qMetaTypeId<T>(), parent));
128 }
129 
130 template<typename T>
132 {
133  static_assert(__private::is_serializable<T>::value, "T cannot be deserialized");
134  return __private::variant_helper<T>::fromVariant(deserializeFrom(device, qMetaTypeId<T>(), parent));
135 }
136 
137 template<typename T>
139 {
140  static_assert(QtJsonSerializer::__private::is_serializable<T>::value, "T cannot be deserialized");
141  return QtJsonSerializer::__private::variant_helper<T>::fromVariant(deserializeFrom(data, qMetaTypeId<T>(), parent));
142 }
143 
144 }
145 
146 #endif // QTJSONSERIALIZER_JSONSERIALIZER_H
QtJsonSerializer
The QtJsonSerializer namespace holds all classes of the module.
Definition: cborserializer.h:7
QIODevice
QtJsonSerializer::JsonSerializer
A class to serialize and deserialize c++ classes to and from JSON.
Definition: jsonserializer.h:15
QtJsonSerializer::SerializerBase
A base class for the CBOR/JSON serializers.
Definition: serializerbase.h:34
QList
QtJsonSerializer::JsonSerializer::deserialize
QVariant deserialize(const QJsonValue &json, int metaTypeId, QObject *parent=nullptr) const
Deserializes a QJsonValue to a QVariant value, based on the given type id.
QJsonValue
QtJsonSerializer::JsonSerializer::serialize
QJsonValue serialize(const QVariant &data) const
Serializers a QVariant value to a QJsonValue.
QtJsonSerializer::JsonSerializer::serializeTo
void serializeTo(QIODevice *device, const QVariant &data, QJsonDocument::JsonFormat format=QJsonDocument::Compact) const
Serializers a QVariant value to a device.
QtJsonSerializer::JsonSerializer::ByteArrayFormat
ByteArrayFormat
Defines the different supported bytearray formats.
Definition: jsonserializer.h:26
QtJsonSerializer::JsonSerializer::deserializeFrom
QVariant deserializeFrom(QIODevice *device, int metaTypeId, QObject *parent=nullptr) const
Deserializes data from a device to a QVariant value, based on the given type id.
QVariant
QObject
QObject::parent
QObject * parent() const const
QByteArray