1

I've been looking for something capable of serializing totally unknown objects.

I know that Qt has a serialization feature that also works with qvariant objects. Does that mean that I can pass a whatever complex object to a QVariant (i.e. something that also has pointers and/or pointer cycles) and serialize that object?

It also seems boost::any isn't supported by serialization.

Is this impossible to do without writing my own specific-type-serialization routine due to the lack of reflection in C++?

Community
  • 1
  • 1
Dean
  • 6,610
  • 6
  • 40
  • 90
  • What do you mean by "unknown object"? Can you edit your question with an example of what you want to achieve? – rocambille Aug 10 '16 at 11:36
  • @wasthishelpful `unknown object` means `unknown object`.. it could be any type of C++ object you can find on github on any project of your choice. – Dean Aug 10 '16 at 11:38
  • That's not unknown, that's undefined, maybe undeclared :) That's why I'm asking for an example of how you want your final code to be working, to figure out what your unknown means from a compiler perspective – rocambille Aug 10 '16 at 11:42
  • @wasthishelpful something that compiles in your program and that you can pass to a `template void serialize(T&& obj);` function. – Dean Aug 10 '16 at 11:46
  • 2
    A `QVariant` can't hold arbitrary types. From [the documentation](http://doc.qt.io/qt-5.7/qvariant.html): "The QVariant class acts like a union for the most common Qt data types." – molbdnilo Aug 10 '16 at 11:54
  • @molbdnilo you can add user defined objects to QVariant by registering them as metatype. I think you still need to declare your class as QObject. edit: added [documentation](http://doc.qt.io/qt-4.8/qmetatype.html#Q_DECLARE_METATYPE), pointers would obviously have to be reconnected. – Anže Aug 10 '16 at 12:22

3 Answers3

2

You can serialize any QVariant as long as all the types you expect the QVariant to carry have their QDataStream operators defined. So, for custom types of your own, you need to have these operators in place and then it'll work fine.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
1

Yes, it is impossible to do this without writing your own serialization code, because C++ does not have reflection. You, pretty much, figured it out.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • QVariant is just a union of different types, but if you use a custom type, you still need to tell Qt about it. Check out this tutorial http://www.bogotobogo.com/Qt/Qt5_QVariant_meta_object_system_MetaType.php – jwernerny Aug 10 '16 at 14:20
0

While C++ does not have reflection, it is possible to get limited reflection in Qt if you QObjects and the Qt Property System.

It is fairly easy to write code to serialize and deserialize all of the properties of any QObject. [hint: There are methods to retrieve all properties of a QObject.] The trade off here is that everything you wish to serialize must be a QObject (and QObjects can not have a copy constructor), and your getters and setters must work with properties. With some clever macros, this is not too big an issue from a coding standpoint.

jwernerny
  • 6,978
  • 2
  • 31
  • 32