Serializing any object (including from classes from external libraries)

Hi folks,

I'd like to know if there's any way to serialize (or more generally speaking, save to file) an object without having to modify its class.

Now, every serialization option that I've come across is intrusive to a lesser or greater extent. It either requires inheritance, or adding extra methods, or adding friends etc.

What if I have an object of a class from an external library? I can't really modify its class in any way. Can't add friends, can't add methods, can't make the members public etc. So how would I go about serializing it?
first of all my first question to be why would want to serialize any object?
if were going to serialize an object i would only serialize data that i cant recreate.

the closest thing you could do is write parser for c++.
with input parsed start generating a serialize function that generates a serialised object.
that then has multiples method to generate string or binary output.

in even more
parse c++ -> generate serialize function file -> use serialize function in code.
this is if you wanted to do it in automated way.

recommnedation:
dont try serializing an entire application.
serialize the important parts like the game status or level data or the data set.
dont serialize things you dont have control over in some way.
Unfortunately, serializing something requires knowledge about it. So there is no catch-all method of doing it. Someone must write code that specifically does it.

If you have access to source code, you definitely want to check out Boost Serialization
http://www.boost.org/doc/libs/1_58_0/libs/serialization/doc/index.html

External libraries will make it more difficult.

Sorry.
I think unless you have a resource-managing object inside the object (like a container or a pointer) then you can just take the binary representation and send it over to the other side, right?

Please correct me if I am mistaken.
Last edited on
That fails if:
- there are pointers to data being managed ("deep data")
- the serialized data is saved on one machine and loaded on another that uses different:
  - endianness
  - data sizes
  - alignment(s)
hm, that's to much if you don't know anything about the destination mashine...

@OP, you don't have access to the data of the object, do you at least know something about the destination system?
I was looking for something along the lines of

1
2
template <typename T>
unsigned char * serialize(const T &data);


OK, the above is a simplistic manner of putting it, but what I mean is a way to save an object to file without knowing the inner workings of its class (such as when the respective class comes from an external library).

I know about Boost::serialization, which also requires modifying the classes.

And, of course, I can't just make a memory dump of the object. If it points to other memory locations (i.e. has pointer members), the objects which are pointed to require serialization as well (yes, I know Boost does this kind of object tracking).

As for the destination systems, they are, for now, Windows machines.
Topic archived. No new replies allowed.