1 #ifndef QTJSONSERIALIZER_JSONSERIALIZER_H
2 #define QTJSONSERIALIZER_JSONSERIALIZER_H
4 #include "QtJsonSerializer/qtjsonserializer_global.h"
5 #include "QtJsonSerializer/serializerbase.h"
7 #include <QtCore/qjsonobject.h>
8 #include <QtCore/qjsonarray.h>
9 #include <QtCore/qjsondocument.h>
13 class JsonSerializerPrivate;
20 Q_PROPERTY(
ByteArrayFormat byteArrayFormat READ byteArrayFormat WRITE setByteArrayFormat NOTIFY byteArrayFormatChanged)
22 Q_PROPERTY(
bool validateBase64 READ validateBase64 WRITE setValidateBase64 NOTIFY validateBase64Changed)
31 Q_ENUM(ByteArrayFormat)
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;
45 typename QtJsonSerializer::__private::json_type<T>::type serialize(
const T &data)
const;
48 void serializeTo(
QIODevice *device,
const T &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact)
const;
51 QByteArray serializeTo(
const T &data, QJsonDocument::JsonFormat format = QJsonDocument::Compact)
const;
62 T deserialize(
const typename QtJsonSerializer::__private::json_type<T>::type &json,
QObject *parent =
nullptr)
const;
71 ByteArrayFormat byteArrayFormat()
const;
73 bool validateBase64()
const;
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;
80 void setByteArrayFormat(ByteArrayFormat byteArrayFormat);
82 void setValidateBase64(
bool validateBase64);
86 void byteArrayFormatChanged(ByteArrayFormat byteArrayFormat, QPrivateSignal);
88 void validateBase64Changed(
bool validateBase64, QPrivateSignal);
92 bool jsonMode()
const override;
93 QCborTag typeTag(
int metaTypeId)
const override;
94 QList<int> typesForTag(QCborTag tag)
const override;
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)));
112 static_assert(__private::is_serializable<T>::value,
"T cannot be serialized");
113 serializeTo(device, __private::variant_helper<T>::toVariant(data), format);
119 static_assert(__private::is_serializable<T>::value,
"T cannot be serialized");
120 return serializeTo(__private::variant_helper<T>::toVariant(data), format);
126 static_assert(__private::is_serializable<T>::value,
"T cannot be deserialized");
127 return __private::variant_helper<T>::fromVariant(
deserialize(json, qMetaTypeId<T>(),
parent));
133 static_assert(__private::is_serializable<T>::value,
"T cannot be deserialized");
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));
146 #endif // QTJSONSERIALIZER_JSONSERIALIZER_H