[Gambas-user] Code for object serialization

Tobias Boege taboege at ...626...
Sat Feb 23 08:51:43 CET 2013


On Fri, 22 Feb 2013, Jussi Lahtinen wrote:
> Hi!
> This is my implementation for object serialization, it's still bit
> incomplete, but it's already usable.
> And I thought some of you might find it very useful. I use it now to save
> objects to file.
> 

*Very* useful, indeed!

> Right now it can handle objects with native datatypes (if you use it
> recursively it can be made to handle all types).
> 

Yes, if it hits an object property then set a marker in the file and call
itself on that object. There is only one problem with this: your code can
only handle (public) properties. Actually an object serialised by your code
and then deserialised could stop working properly because Private variables
not bound to properties (which weren't saved therefore) may not contain the
right values anymore.

> What do you think? Is there better way to do this?
> Maybe with Object.Address() & Object.SizeOf() ?
> 

I think you can use these to generate a binary dump of an object to a file,
but the thing is: the backing structures you dump most likely contain
pointers to other structures (Gambas objects or structures allocated by a
shared library!) which will become invalid pointers when loaded into another
process.

I think the solution to both above problems is already in Settings.Write().
It is capable of _correctly_ writing and restoring objects if they export a
Settings property. As a matter of fact, the object itself only really knows
what must be serialised and can also provide its private variables.

Alas, I don't see how the serialisation process could be done _without_ the
object being deeply involved by providing its internal information.

Regards,
Tobi




More information about the User mailing list