QtJsonSerializer  4.0.0
A library to perform generic seralization and deserialization of QObjects
typeconverter.h
1 #ifndef QTJSONSERIALIZER_TYPECONVERTER_H
2 #define QTJSONSERIALIZER_TYPECONVERTER_H
3 
4 #include "QtJsonSerializer/qtjsonserializer_global.h"
5 
6 #include <type_traits>
7 #include <limits>
8 
9 #include <QtCore/qmetatype.h>
10 #include <QtCore/qmetaobject.h>
11 #include <QtCore/qjsonvalue.h>
12 #include <QtCore/qcborvalue.h>
13 #include <QtCore/qvariant.h>
14 #include <QtCore/qsharedpointer.h>
15 
16 namespace QtJsonSerializer {
17 
19 class Q_JSONSERIALIZER_EXPORT TypeExtractor
20 {
21  Q_DISABLE_COPY(TypeExtractor)
22 
23 public:
24  TypeExtractor();
25  virtual ~TypeExtractor();
26 
28  virtual QByteArray baseType() const = 0;
30  virtual QList<int> subtypes() const = 0;
32  virtual QVariant extract(const QVariant &value, int index = -1) const = 0;
34  virtual void emplace(QVariant &target, const QVariant &value, int index = -1) const = 0;
35 };
36 
37 class TypeConverterPrivate;
39 class Q_JSONSERIALIZER_EXPORT TypeConverter
40 {
41  Q_DISABLE_COPY(TypeConverter)
42 public:
44  static constexpr auto NoTag = static_cast<QCborTag>(std::numeric_limits<std::underlying_type_t<QCborTag>>::max());
45 
47  enum Priority : int {
48  ExtremlyLow = -0x00FFFFFF,
49  VeryLow = -0x0000FFFF,
50  Low = -0x000000FF,
51  Standard = 0x00000000,
52  High = 0x000000FF,
53  VeryHigh = 0x0000FFFF,
54  ExtremlyHigh = 0x00FFFFFF
55  };
56 
59  Positive = 1,
60  Guessed = 2,
61  Negative = -1,
62  WrongTag = -2
63  };
64 
66  class Q_JSONSERIALIZER_EXPORT SerializationHelper
67  {
68  Q_DISABLE_COPY(SerializationHelper)
69  public:
71  virtual ~SerializationHelper();
72 
74  virtual bool jsonMode() const = 0;
76  virtual QVariant getProperty(const char *name) const = 0;
78  virtual QCborTag typeTag(int metaTypeId) const = 0;
80  virtual QSharedPointer<const TypeExtractor> extractor(int metaTypeId) const = 0;
81 
83  virtual QCborValue serializeSubtype(const QMetaProperty &property, const QVariant &value) const = 0;
85  virtual QCborValue serializeSubtype(int propertyType, const QVariant &value, const QByteArray &traceHint = {}) const = 0;
87  virtual QVariant deserializeSubtype(const QMetaProperty &property, const QCborValue &value, QObject *parent) const = 0;
89  virtual QVariant deserializeSubtype(int propertyType, const QCborValue &value, QObject *parent, const QByteArray &traceHint = {}) const = 0;
90  };
91 
93  TypeConverter();
95  virtual ~TypeConverter();
96 
98  virtual QByteArray name() const = 0;
99 
101  int priority() const;
103  void setPriority(int priority);
104 
106  const SerializationHelper *helper() const;
108  void setHelper(const SerializationHelper *helper);
109 
111  virtual bool canConvert(int metaTypeId) const = 0;
113  virtual QList<QCborTag> allowedCborTags(int metaTypeId) const;
115  virtual QList<QCborValue::Type> allowedCborTypes(int metaTypeId, QCborTag tag) const = 0;
117  virtual int guessType(QCborTag tag, QCborValue::Type dataType) const;
119  DeserializationCapabilityResult canDeserialize(int &metaTypeId,
120  QCborTag tag,
121  QCborValue::Type dataType) const;
122 
124  virtual QCborValue serialize(int propertyType, const QVariant &value) const = 0;
126  virtual QVariant deserializeCbor(int propertyType, const QCborValue &value, QObject *parent) const = 0;
128  virtual QVariant deserializeJson(int propertyType, const QCborValue &value, QObject *parent) const;
129 
130 private:
132 
133  void mapTypesToJson(QList<QCborValue::Type> &typeList) const;
134 };
135 
137 #define QT_JSONSERIALIZER_TYPECONVERTER_NAME(className) inline QByteArray name() const override { \
138  static_assert(std::is_same_v<className, std::decay_t<decltype(*this)>>); \
139  return QByteArrayLiteral(#className); \
140 }
141 
143 class Q_JSONSERIALIZER_EXPORT TypeConverterFactory
144 {
145  Q_DISABLE_COPY(TypeConverterFactory)
146 
147 public:
149  virtual ~TypeConverterFactory();
150 
152  virtual QSharedPointer<TypeConverter> createConverter() const = 0;
153 };
154 
156 template <typename TConverter, int OverwritePriority = TypeConverter::Priority::Standard>
158 {
159 public:
161 };
162 
163 // ------------- GENERIC IMPLEMENTATION -------------
164 
165 template<typename TConverter, int OverwritePriority>
167 {
168  auto converter = QSharedPointer<TConverter>::create();
169  if (OverwritePriority != TypeConverter::Priority::Standard)
170  converter->setPriority(OverwritePriority);
171  return converter;
172 }
173 
174 }
175 
176 #endif // QTJSONSERIALIZER_TYPECONVERTER_H
QtJsonSerializer::TypeExtractor
Interface to extract data from any generic container.
Definition: typeconverter.h:19
QtJsonSerializer::TypeConverter::SerializationHelper
Helper class passed to the type converter by the serializer. Do not implement yourself.
Definition: typeconverter.h:66
QtJsonSerializer
The QtJsonSerializer namespace holds all classes of the module.
Definition: cborserializer.h:7
QMetaProperty
QSharedPointer::create
QSharedPointer< T > create(Args &&... args)
QSharedPointer
QList
QtJsonSerializer::TypeConverterFactory
A factory interface to create instances of QJsonTypeConverters.
Definition: typeconverter.h:143
QtJsonSerializer::TypeConverter::Priority
Priority
Sample values for a priority value (default converters are mostly Standard and are guaranteed to be b...
Definition: typeconverter.h:47
QCborValue
QScopedPointer< TypeConverterPrivate >
QtJsonSerializer::TypeConverterStandardFactory
A template implementation of TypeConverterFactory to generically create simply converters.
Definition: typeconverter.h:157
QtJsonSerializer::TypeConverterStandardFactory::createConverter
QSharedPointer< TypeConverter > createConverter() const override
The primary factory method to create converters.
Definition: typeconverter.h:166
QtJsonSerializer::TypeConverter
An interface to create custom serializer type converters.
Definition: typeconverter.h:39
QVariant
QObject
QtJsonSerializer::TypeConverter::DeserializationCapabilityResult
DeserializationCapabilityResult
The possible results of canDeserialize(), used internally only.
Definition: typeconverter.h:58
QByteArray