PDA

View Full Version : Serializing/DeSerializing Nested QHash objects



jncarlic
17th June 2016, 02:01
So im at a bit of a loss and im hoping someone here can help.

My object being serialized looks like this


QHash<QString, QHash<QString, QHash<QString, QVariant>*>*>* hosthash = new QHash<QString, QHash<QString, QHash<QString, QVariant>*>*>();

Then as my application is running the lowest level QHash<QString,QVariant> gets populated and all is good when it comes time to serialize, where i then nest it into a few more hashes to properly organize all the data.
At the lowest hash i have a deviceModel that contains all the data i want, which i wrap in another hash whose key differentiates that device from other devices on the system, and then that is wrapped once again in a hash whose key differentiates different systems. Its preferred to keep it this way because it will allow me to easily recreate a nested QTree with all the pertinent information(in theory).

I wrote two overridden >> operators for datastream to dereference the pointer and pass it into the stream.


QDataStream &operator <<(QDataStream &stream, const QHash<QString, QVariant>* deviceModelHash) {
stream << *deviceModelHash;
return stream;
}

QDataStream &operator <<(QDataStream &stream, const QHash<QString, QHash<QString, QVariant>*>* deviceHash) {
stream << *deviceHash;
return stream;
}

All seems well, when i write it to a file it comes out to be 3100ish bytes which seems realistic for how much data im storing.

But the problem comes with Deserializing. Say i have another hash i want to read into that does not consist of pointers because its not needed when coming back in.


QHash<QString, QHash<QString, QHash<QString, QVariant>>> testhash

How do i get this data back into a workable hash? I assume i need to make some sort datastream >> testHash override but i am confused how to go about doing that. Just doing datastream >> TestHash after opening the stream from file results in an empty testHash. But im at a loss for how to do it manually in the overridden >> function because of how nested the data is. I cant create the top level of the hash without knowing what the 2nd level of the hash is, and i cant create the 2nd level of the hash without having read in the 3rd level of the hash. Hopefully that makes sense?

Thanks.

anda_skoa
17th June 2016, 10:30
The first question you should ask is why you need all those pointers in your structure.
That makes the function signatures more complicated because then you have references of pointers instead of just the normal references.

Cheers,
_