Postgres QVariant Serialization
Posted: Fri Aug 03, 2018 9:19 pm
We've turned off BOOST Serialization and are left with the stock QDataStream serialization.
Postgres by default will store BYTEA as hex because apparently that is the preferered format. I doesn't appear that the qxlibrary does anything special outgoing and this implicitly happens when the write happens. Reading and deserialization has become a big problem because the data is overlooked because it is not decoded from hex before it goes back through this function.
The function before this looks like we can setup format flags in the orm so that we can handle this use case.
Is this the best way we should be handling this scenario? Looking for some advice here, I just need to be able to reliably serialize and deserialize QList<QString> to and from the db.
If we wanted to register our own Converter for custom datatypes how would we do that?
Thanks!
Postgres by default will store BYTEA as hex because apparently that is the preferered format. I doesn't appear that the qxlibrary does anything special outgoing and this implicitly happens when the write happens. Reading and deserialization has become a big problem because the data is overlooked because it is not decoded from hex before it goes back through this function.
- Code: Select all
template <class T>
inline qx_bool from_byte_array(T & obj, const QByteArray & data, unsigned int flags = 1 /* boost::archive::no_header */)
{
Q_UNUSED(flags);
qx_bool result = false;
if (data.isEmpty()) { return qx_bool(false, "input binary data is empty"); }
QDataStream stream(data);
quint32 magic = 0; stream >> magic;
if (magic != 9438) { return qx_bool(false, "input binary data is not valid"); }
try { stream >> obj; result = true; }
catch (const std::exception & e) { result.setDesc(QString("deserialization error '%ERR%'").replace("%ERR%", e.what())); }
catch (...) { result.setDesc(QString("deserialization error '%ERR%'").replace("%ERR%", "unknown error")); }
if (! result.getDesc().isEmpty()) { QString msg = result.getDesc(); qDebug("[QxOrm] qx::serialization::qt::from_byte_array() : %s", qPrintable(msg)); }
return result;
}
The function before this looks like we can setup format flags in the orm so that we can handle this use case.
- Code: Select all
template <typename T> struct QxConvert_FromVariant< QList<T> > {
static inline qx_bool fromVariant(const QVariant & v, QList<T> & t, const QString & format, int index, qx::cvt::context::ctx_type ctx)
{
Q_UNUSED(index);
Q_UNUSED(ctx);
if (format.compare("hex", Qt::CaseInsensitive) == 0)
{
QByteArray local = QByteArray::fromHex(v.toByteArray());
return QX_CVT_DEFAULT_ARCHIVE::from_byte_array(t, local);
}
else
return QX_CVT_DEFAULT_ARCHIVE_FROM_VARIANT;
}
Is this the best way we should be handling this scenario? Looking for some advice here, I just need to be able to reliably serialize and deserialize QList<QString> to and from the db.
If we wanted to register our own Converter for custom datatypes how would we do that?
Thanks!