QtJsonSerializer  4.0.0
A library to perform generic seralization and deserialization of QObjects
cborserializer.h
1 #ifndef QTJSONSERIALIZER_CBORSERIALIZER_H
2 #define QTJSONSERIALIZER_CBORSERIALIZER_H
3 
4 #include "QtJsonSerializer/qtjsonserializer_global.h"
5 #include "QtJsonSerializer/serializerbase.h"
6 
7 namespace QtJsonSerializer {
8 
9 class CborSerializerPrivate;
11 class Q_JSONSERIALIZER_EXPORT CborSerializer : public SerializerBase
12 {
13  Q_OBJECT
14 
16  Q_PROPERTY(bool handleSpecialNumbers READ handleSpecialNumbers WRITE setHandleSpecialNumbers NOTIFY handleSpecialNumbersChanged)
17 
18 public:
20  enum ExtendedTags : std::underlying_type_t<QCborTag> {
21  GenericObject = 27,
22  RationaleNumber = 30,
23  Identifier = 39,
24  Homogeneous = 41,
25  Set = 258,
26  ExplicitMap = 259,
27  NetworkAddress = 260,
28  NetworkAddressPrefix = 261,
29  };
30  Q_ENUM(ExtendedTags)
31 
32  // Additional unofficial CBOR-Tags, as defined by this library to tag Qt types for better typesafety
33  enum CustomTags : std::underlying_type_t<QCborTag> {
34  Color = 10000,
35  Font = 10001,
36  Enum = 10002,
37  Flags = 10003,
38  ConstructedObject = 10004,
39  Pair = 10005,
40  MultiMap = 10006,
41  VersionNumber = 10007,
42  Tuple = 10008,
43  BitArray = 10009,
44  Date = 10010,
45  Time = 10011,
46 
47  LocaleISO = 10100,
48  LocaleBCP47 = 10101,
49 
50  GeomSize = 10110,
51  GeomPoint = 10111,
52  GeomLine = 10112,
53  GeomRect = 10113,
54 
55  ChronoNanoSeconds = 10120,
56  ChronoMicroSeconds = 10121,
57  ChronoMilliSeconds = 10122,
58  ChronoSeconds = 10123,
59  ChronoMinutes = 10124,
60  ChronoHours = 10125,
61 
62  NoTag = std::numeric_limits<std::underlying_type_t<QCborTag>>::max()
63  };
64  Q_ENUM(CustomTags)
65 
66 
67  explicit CborSerializer(QObject *parent = nullptr);
68 
70  bool handleSpecialNumbers() const;
71 
73  template <typename T>
74  void setTypeTag(QCborTag tag = static_cast<QCborTag>(NoTag));
76  void setTypeTag(int metaTypeId, QCborTag tag = static_cast<QCborTag>(NoTag));
78  template <typename T>
79  QCborTag typeTag() const;
80  QCborTag typeTag(int metaTypeId) const override;
81 
83  QCborValue serialize(const QVariant &data) const;
85  void serializeTo(QIODevice *device, const QVariant &data, QCborValue::EncodingOptions options = QCborValue::NoTransformation) const;
87  QByteArray serializeTo(const QVariant &data, QCborValue::EncodingOptions options = QCborValue::NoTransformation) const;
88 
90  template <typename T>
91  QCborValue serialize(const T &data) const;
93  template <typename T>
94  void serializeTo(QIODevice *device, const T &data, QCborValue::EncodingOptions options = QCborValue::NoTransformation) const;
96  template <typename T>
97  QByteArray serializeTo(const T &data, QCborValue::EncodingOptions options = QCborValue::NoTransformation) const;
98 
100  QVariant deserialize(const QCborValue &cbor, int metaTypeId, QObject *parent = nullptr) const;
102  QVariant deserializeFrom(QIODevice *device, int metaTypeId, QObject *parent = nullptr) const;
104  QVariant deserializeFrom(const QByteArray &data, int metaTypeId, QObject *parent = nullptr) const;
105 
107  template <typename T>
108  T deserialize(const QCborValue &cbor, QObject *parent = nullptr) const;
110  template <typename T>
111  T deserializeFrom(QIODevice *device, QObject *parent = nullptr) const;
113  template <typename T>
114  T deserializeFrom(const QByteArray &data, QObject *parent = nullptr) const;
115 
116  std::variant<QCborValue, QJsonValue> serializeGeneric(const QVariant &value) const override;
117  QVariant deserializeGeneric(const std::variant<QCborValue, QJsonValue> &value, int metaTypeId, QObject *parent) const override;
118 
119 public Q_SLOTS:
121  void setHandleSpecialNumbers(bool handleSpecialNumbers);
122 
123 Q_SIGNALS:
125  void handleSpecialNumbersChanged(bool handleSpecialNumbers, QPrivateSignal);
126 
127 protected:
128  // protected implementation -> internal use for the type converters
129  bool jsonMode() const override;
130  QList<int> typesForTag(QCborTag tag) const override;
131 
132 private:
133  Q_DECLARE_PRIVATE(CborSerializer)
134 };
135 
136 // ------------- generic implementation -------------
137 
138 template<typename T>
139 void CborSerializer::setTypeTag(QCborTag tag)
140 {
141  setTypeTag(qMetaTypeId<T>(), tag);
142 }
143 
144 template<typename T>
145 QCborTag CborSerializer::typeTag() const
146 {
147  return typeTag(qMetaTypeId<T>());
148 }
149 
150 template<typename T>
152 {
153  static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
154  return serialize(__private::variant_helper<T>::toVariant(data));
155 }
156 
157 template<typename T>
158 void CborSerializer::serializeTo(QIODevice *device, const T &data, QCborValue::EncodingOptions options) const
159 {
160  static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
161  serializeTo(device, __private::variant_helper<T>::toVariant(data), options);
162 }
163 
164 template<typename T>
166 {
167  static_assert(__private::is_serializable<T>::value, "T cannot be serialized");
168  return serializeTo(__private::variant_helper<T>::toVariant(data), options);
169 }
170 
171 template<typename T>
172 T CborSerializer::deserialize(const QCborValue &cbor, QObject *parent) const
173 {
174  static_assert(__private::is_serializable<T>::value, "T cannot be deserialized");
175  return __private::variant_helper<T>::fromVariant(deserialize(cbor, qMetaTypeId<T>(), parent));
176 }
177 
178 template<typename T>
180 {
181  static_assert(__private::is_serializable<T>::value, "T cannot be deserialized");
182  return __private::variant_helper<T>::fromVariant(deserializeFrom(device, qMetaTypeId<T>(), parent));
183 }
184 
185 template<typename T>
187 {
188  static_assert(__private::is_serializable<T>::value, "T cannot be deserialized");
189  return __private::variant_helper<T>::fromVariant(deserializeFrom(data, qMetaTypeId<T>(), parent));
190 }
191 
192 }
193 
194 #endif // QTJSONSERIALIZER_CBORSERIALIZER_H
QtJsonSerializer::CborSerializer::serializeTo
void serializeTo(QIODevice *device, const QVariant &data, QCborValue::EncodingOptions options=QCborValue::NoTransformation) const
Serializers a QVariant value to a device.
QtJsonSerializer
The QtJsonSerializer namespace holds all classes of the module.
Definition: cborserializer.h:7
QIODevice
QCborValue::EncodingOptions
typedef EncodingOptions
QtJsonSerializer::SerializerBase
A base class for the CBOR/JSON serializers.
Definition: serializerbase.h:34
QList
QtJsonSerializer::CborSerializer::serialize
QCborValue serialize(const QVariant &data) const
Serializers a QVariant value to a QCborValue.
QtJsonSerializer::CborSerializer::ExtendedTags
ExtendedTags
Additional official CBOR-Tags, taken from https://www.iana.org/assignments/cbor-tags/cbor-tags....
Definition: cborserializer.h:20
QtJsonSerializer::CborSerializer::deserialize
QVariant deserialize(const QCborValue &cbor, int metaTypeId, QObject *parent=nullptr) const
Deserializes a QCborValue to a QVariant value, based on the given type id.
QtJsonSerializer::CborSerializer::CustomTags
CustomTags
Definition: cborserializer.h:33
QtJsonSerializer::CborSerializer::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.
QCborValue
QtJsonSerializer::CborSerializer
A class to serialize and deserialize c++ classes to and from CBOR.
Definition: cborserializer.h:11
QtJsonSerializer::CborSerializer::setTypeTag
void setTypeTag(QCborTag tag=static_cast< QCborTag >(NoTag))
Set a tag to always be used when serializing the given type.
Definition: cborserializer.h:139
QtJsonSerializer::CborSerializer::typeTag
QCborTag typeTag() const
Returns a tag registered for the given metaTypeId.
Definition: cborserializer.h:145
QVariant
QObject
QObject::parent
QObject * parent() const const
QByteArray