You can provide two functions for packing QVariant with custom type inside to QByteArray and vise versa.
For example here is how QColor packer and unpacker looks like:
..code-block:: cpp
QByteArray pack_qcolor(const QVariant &variant)
{
QColor color = variant.value<QColor>();
if (!color.isValid())
return QByteArray(1, 0);
QByteArray data;
data.resize(4);
quint8 *p = (quint8 *)data.data();
p[0] = color.red();
p[1] = color.green();
p[2] = color.blue();
p[3] = color.alpha();
return data;
}
QVariant unpack_qcolor(const QByteArray &data)
{
if (data.length() == 1)
return QColor();
quint8 *p = (quint8 *)data.data();
return QColor(p[0], p[1], p[2], p[3]);
}
And that's it!
Now register this two functions:
..code-block:: cpp
MsgPack::registerPacker(QMetaType::QColor, 3, pack_qcolor); // 3 is msgpack ext type id
MsgPack::registerUnpacker(3, unpack_qcolor);
After that ``MsgPack::pack(QColor(127, 127, 127))`` will start to work!
Custom stream
=============
You can provide stream operators for any other type you might want to work with. But there are some pitfalls to consider if you want to unpack something with other MsgPack implementations.
Of course you can just stream out everything without any ext header and user type id's, like this: ``s << point.x() << point.y(); return s;`` but in that case you will not be able to unpack anything useful with MsgPack::unpack() or in other MsgPack implementations.