1 #ifndef QTJSONSERIALIZER_SERIALIZERBASE_H
2 #define QTJSONSERIALIZER_SERIALIZERBASE_H
4 #include "QtJsonSerializer/qtjsonserializer_global.h"
5 #include "QtJsonSerializer/exception.h"
6 #include "QtJsonSerializer/typeconverter.h"
7 #include "QtJsonSerializer/qtjsonserializer_helpertypes.h"
8 #include "QtJsonSerializer/metawriters.h"
9 #include "QtJsonSerializer/typeextractors.h"
15 #include <QtCore/qobject.h>
16 #include <QtCore/qmetaobject.h>
17 #include <QtCore/qcborvalue.h>
18 #include <QtCore/qvariant.h>
19 #include <QtCore/qsharedpointer.h>
20 #include <QtCore/qpointer.h>
21 #include <QtCore/qlist.h>
22 #include <QtCore/qlinkedlist.h>
23 #include <QtCore/qvector.h>
24 #include <QtCore/qset.h>
25 #include <QtCore/qqueue.h>
26 #include <QtCore/qstack.h>
27 #include <QtCore/qhash.h>
28 #include <QtCore/qmap.h>
32 class SerializerBasePrivate;
39 Q_PROPERTY(
bool allowDefaultNull READ allowDefaultNull WRITE setAllowDefaultNull NOTIFY allowDefaultNullChanged)
41 Q_PROPERTY(
bool keepObjectName READ keepObjectName WRITE setKeepObjectName NOTIFY keepObjectNameChanged)
43 Q_PROPERTY(
bool enumAsString READ enumAsString WRITE setEnumAsString NOTIFY enumAsStringChanged)
45 Q_PROPERTY(
bool versionAsString READ versionAsString WRITE setVersionAsString NOTIFY versionAsStringChanged)
47 Q_PROPERTY(
bool dateAsTimeStamp READ dateAsTimeStamp WRITE setDateAsTimeStamp NOTIFY dateAsTimeStampChanged)
49 Q_PROPERTY(
bool useBcp47Locale READ useBcp47Locale WRITE setUseBcp47Locale NOTIFY useBcp47LocaleChanged)
51 Q_PROPERTY(ValidationFlags validationFlags READ validationFlags WRITE setValidationFlags NOTIFY validationFlagsChanged)
53 Q_PROPERTY(
Polymorphing polymorphing READ polymorphing WRITE setPolymorphing NOTIFY polymorphingChanged)
55 Q_PROPERTY(
MultiMapMode multiMapMode READ multiMapMode WRITE setMultiMapMode NOTIFY multiMapModeChanged)
57 Q_PROPERTY(
bool ignoreStoredAttribute READ ignoresStoredAttribute WRITE setIgnoreStoredAttribute NOTIFY ignoreStoredAttributeChanged)
62 StandardValidation = 0x00,
63 NoExtraProperties = 0x01,
65 StrictBasicTypes = 0x04,
67 FullPropertyValidation = (NoExtraProperties | AllProperties),
68 FullValidation = (FullPropertyValidation | StrictBasicTypes),
70 Q_DECLARE_FLAGS(ValidationFlags, ValidationFlag)
71 Q_FLAG(ValidationFlags)
90 template<
typename TType,
typename TExtractor>
91 static void registerExtractor();
97 static inline void registerListConverters();
100 static inline void registerSetConverters();
102 template<
typename TKey,
typename TValue,
bool mapTypes = true,
bool hashTypes = true>
103 static inline void registerMapConverters();
106 static inline void registerPointerConverters();
109 static inline void registerBasicConverters();
111 template<
typename T,
typename U>
112 static inline void registerPairConverters();
114 template<
typename... TArgs>
115 static inline void registerTupleConverters();
118 static inline void registerOptionalConverters();
120 template<
typename... TArgs>
121 static inline void registerVariantConverters();
124 virtual std::variant<QCborValue, QJsonValue> serializeGeneric(
const QVariant &value)
const = 0;
126 virtual QVariant deserializeGeneric(
const std::variant<QCborValue, QJsonValue> &value,
int metaTypeId,
QObject *parent =
nullptr)
const = 0;
129 bool allowDefaultNull()
const;
131 bool keepObjectName()
const;
133 bool enumAsString()
const;
135 bool versionAsString()
const;
137 bool dateAsTimeStamp()
const;
139 bool useBcp47Locale()
const;
141 ValidationFlags validationFlags()
const;
143 Polymorphing polymorphing()
const;
145 MultiMapMode multiMapMode()
const;
147 bool ignoresStoredAttribute()
const;
150 template <
typename TConverter,
int Priority = TypeConverter::Priority::Standard>
151 static void addJsonTypeConverterFactory();
156 template <
typename TConverter>
157 void addJsonTypeConverter();
163 void setAllowDefaultNull(
bool allowDefaultNull);
165 void setKeepObjectName(
bool keepObjectName);
167 void setEnumAsString(
bool enumAsString);
169 void setVersionAsString(
bool versionAsString);
171 void setDateAsTimeStamp(
bool dateAsTimeStamp);
173 void setUseBcp47Locale(
bool useBcp47Locale);
175 void setValidationFlags(ValidationFlags validationFlags);
177 void setPolymorphing(Polymorphing polymorphing);
179 void setMultiMapMode(MultiMapMode multiMapMode);
181 void setIgnoreStoredAttribute(
bool ignoreStoredAttribute);
185 void allowDefaultNullChanged(
bool allowDefaultNull, QPrivateSignal);
187 void keepObjectNameChanged(
bool keepObjectName, QPrivateSignal);
189 void enumAsStringChanged(
bool enumAsString, QPrivateSignal);
191 void versionAsStringChanged(
bool versionAsString, QPrivateSignal);
193 void dateAsTimeStampChanged(
bool dateAsTimeStamp, QPrivateSignal);
195 void useBcp47LocaleChanged(
bool useBcp47Locale, QPrivateSignal);
197 void validationFlagsChanged(ValidationFlags validationFlags, QPrivateSignal);
199 void polymorphingChanged(Polymorphing polymorphing, QPrivateSignal);
201 void multiMapModeChanged(MultiMapMode multiMapMode, QPrivateSignal);
203 void ignoreStoredAttributeChanged(
bool ignoreStoredAttribute, QPrivateSignal);
211 virtual QList<int> typesForTag(QCborTag tag)
const = 0;
214 QVariant getProperty(
const char *name)
const override;
224 QVariant deserializeVariant(
int propertyType,
const QCborValue &value,
QObject *parent,
bool skipConversion =
false)
const;
229 static void registerInverseTypedefImpl(
int typeId,
const char *normalizedTypeName);
233 #define Q_JSON_POLYMORPHIC(x) \
234 static_assert(std::is_same<decltype(x), bool>::value, "x must be bool"); \
235 Q_CLASSINFO("polymorphic", #x)
239 template<
typename TType,
typename TExtractor>
248 MetaWriters::SequentialWriter::registerWriter<QList, T>();
249 MetaWriters::SequentialWriter::registerWriter<QLinkedList, T>();
250 MetaWriters::SequentialWriter::registerWriter<QVector, T>();
251 MetaWriters::SequentialWriter::registerWriter<QStack, T>();
252 MetaWriters::SequentialWriter::registerWriter<QQueue, T>();
258 MetaWriters::SequentialWriter::registerWriter<QSet, T>();
261 template<
typename TKey,
typename TValue,
bool mapTypes,
bool hashTypes>
264 if constexpr (mapTypes) {
265 MetaWriters::AssociativeWriter::registerWriter<QMap, TKey, TValue>();
266 MetaWriters::AssociativeWriter::registerWriter<QMultiMap, TKey, TValue>();
268 if constexpr (hashTypes) {
269 MetaWriters::AssociativeWriter::registerWriter<QHash, TKey, TValue>();
270 MetaWriters::AssociativeWriter::registerWriter<QMultiHash, TKey, TValue>();
277 registerExtractor<QSharedPointer<T>, TypeExtractors::SmartPointerExtractor<QSharedPointer, T>>();
278 if constexpr (std::is_base_of_v<QObject, T>)
279 registerExtractor<QPointer<T>, TypeExtractors::SmartPointerExtractor<QPointer, T>>();
285 if constexpr (std::is_base_of_v<QObject, T>) {
286 registerBasicConverters<T*>();
287 registerPointerConverters<T>();
288 registerBasicConverters<QSharedPointer<T>>();
289 registerBasicConverters<QPointer<T>>();
291 registerListConverters<T>();
292 registerSetConverters<T>();
293 registerMapConverters<QString, T>();
297 template<
typename T1,
typename T2>
300 registerExtractor<QPair<T1, T2>, TypeExtractors::PairExtractor<QPair, T1, T2>>();
301 registerExtractor<std::pair<T1, T2>, TypeExtractors::PairExtractor<std::pair, T1, T2>>();
304 template<
typename... TArgs>
307 registerExtractor<std::tuple<TArgs...>, TypeExtractors::TupleExtractor<TArgs...>>();
313 registerExtractor<std::optional<T>, TypeExtractors::OptionalExtractor<T>>();
316 template<
typename... TArgs>
319 registerExtractor<std::variant<TArgs...>, TypeExtractors::VariantExtractor<TArgs...>>();
322 template<
typename TConverter,
int Priority>
325 static_assert(std::is_base_of<TypeConverter, TConverter>::value,
"T must implement QJsonTypeConverter");
329 template<
typename TConverter>
332 static_assert(std::is_base_of<TypeConverter, TConverter>::value,
"T must implement QJsonTypeConverter");
338 Q_DECLARE_OPERATORS_FOR_FLAGS(QtJsonSerializer::SerializerBase::ValidationFlags)
340 Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(
QMultiMap)
341 Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(
QMultiHash)
343 #endif // QTJSONSERIALIZER_SERIALIZERBASE_H