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
QByteArraypack_qcolor(constQVariant&variant)
{
QColorcolor=variant.value<QColor>();
if(!color.isValid())
returnQByteArray(1,0);
QByteArraydata;
data.resize(4);
quint8*p=(quint8*)data.data();
p[0]=color.red();
p[1]=color.green();
p[2]=color.blue();
p[3]=color.alpha();
returndata;
}
QVariantunpack_qcolor(constQByteArray&data)
{
if(data.length()==1)
returnQColor();
quint8*p=(quint8*)data.data();
returnQColor(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.