[Interest] Serializing QSensorReading derived classes

Jérôme Godbout godboutj at amotus.ca
Wed Feb 21 19:48:48 CET 2018


You only need to known which object you (un)serialized, I had a coworker who made a Xml where the Object value was base64 encoded byte stream for the object value. This is fairly simple to have the following xml tag

<data type="MyQObjectMetaTypeName" typeId="3245">base64 encoded string from QDataStream goes here</data>

or even JSON format could do the same.


This should help you, if the type id is a basic type or a user defined type you can react differently. So for your QObject class type only need to register to QMetaType you can use the name into the type field to create the object and then apply the data to that object operator>>() with that given object. Not sure you need a factory with QMetaType if done properly.


________________________________
From: Jason H <jhihn at gmx.com>
Sent: Wednesday, February 21, 2018 10:08:50 AM
To: Jérôme Godbout
Cc: interestqt-project.org
Subject: Re: [Interest] Serializing QSensorReading derived classes

Yeah, tha's about what I was guessing. So QObject-derived classes (which the readings are) are not copy constructable.  I thought long and hard about it, and I'm having to implement a ReadingFactory class, which will serialize any reading using non-QObject-derived class[es], then set*() for each property on the QSensorReading class. But to not have to make 13 (or however many sensorreadings) classes, I just made one class to handle them all.
About 1000 lines of code later, I think I have something that will work.

I was wondering if there was a generic QObject serialization facility, where I can save a QObject instance to a file, probably as a class name or id, and then a QVariantMap of properties and their values {QString,QVariantMap}? For narive classes like the sensor readings, it would have been helpful. I'm going to look at Qt Remote Objects later today. https://doc.qt.io/qt-5.10/qtremoteobjects-gettingstarted.html

If anyone has any pointers (NPI) about deficiencies with this approach let me know!

Sent: Wednesday, February 21, 2018 at 9:09 AM
From: "Jérôme Godbout" <godboutj at amotus.ca>
To: "Jason H" <jhihn at gmx.com>, "interestqt-project.org" <interest at qt-project.org>
Subject: Re: [Interest] Serializing QSensorReading derived classes

The serialization assume you known what type you are reading. If you have a dynamic structure, you will need to add some info about the next data coming (type or something). DataStream convert the object minimal data to byte and no information about what those byte represent. You might have the type intelace inside the datastream or you might do an header that tell the sequence of object (I personnaly would prefer the 2nd choice since you can optimize the sequence with a commun series). Ex:



type bytes series [1,2,354] where those type could be QVariant basic type or user defined type that you could make a switch (could represent a single Type of a series of chained types ex: 354 --> [QString,  int, QVector3D, MyCustomType]. This way you could reduce the type bytes series length and could add the real data validation with data length and checksum if ever needed.



As for QDataStream it can serialize a lot of data type of of the box, but you might need to add some operator to serialize some types. Here's the list of supported data types:

http://doc.qt.io/qt-5/datastreamformat.html



You might need to extend the support to the QAcceleromterReading and QAltimeterReading to do you own data reading and call the constructor with the proper deserialized data.



________________________________
From: Interest <interest-bounces+godboutj=amotus.ca at qt-project.org> on behalf of Jason H <jhihn at gmx.com>
Sent: Wednesday, February 21, 2018 1:05:35 AM
To: interestqt-project.org
Subject: [Interest] Serializing QSensorReading derived classes

I'd like to serialize these values.
QAcceleromterReading has x,y,z properties, but to serialize this one, the accelerometer mode should also be included.

But aside from that, I'm not clear on what I need to do. There's adding the QDataStream << >> (QDataScream stream, QSensorReading reading) operators. But that won't allow me to know what I'm reading, so I need to add a byte to know what I'm deserializing, which means I should really do it via QVariant in the QMetaType system.
qRegisterMetaType() and DECLARE_META_TYPE(). However, when I do this I get:
qmetatype.h:766: error: call to implicitly-deleted copy constructor of 'QAltimeterReading'
            return new (where) T(*static_cast<const T*>(t));
                               ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
for each of the types.

What can I do to get this to work without having to write (derive) my own classes?

I'm also wondering why this isn't supported already?
_______________________________________________
Interest mailing list
Interest at qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180221/b188c32f/attachment.html>


More information about the Interest mailing list