Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't every type of object serializable?

Why isn't every type of object implicitly serializable?

In my limited understanding, are objects not simply stored on the heap and pointers to them on the stack?

Shouldn't you be able to traverse them programatically, store them in a universal format and also be able to reconstruct them from there?

like image 573
Matt Avatar asked Aug 21 '09 01:08

Matt


3 Answers

Some objects encapsulate resources like file pointers or network sockets that can't be deserialized to the state they were in when you serialized the object that contained them.

Example: you shouldn't deserialize an object that serves as an authenticated database connection, because to do so, you'd need the serialized form to contain a plaintext password. This would not be a good practice, because someone might get a hold of the saved serialized form. You also have no idea when you deserialize that the database server is still running, can be accessed, the authentication credentials still valid, etc.

like image 196
Bill Karwin Avatar answered Sep 22 '22 06:09

Bill Karwin


Even if you only consider objects that don't include OS state, the problem is harder than it looks at first glance. The graph may have cycles. Entities may be referenced from multiple top-level entities.

I tried to outline a universal serialization library in c in a previous answer, and found that there are some hard cases.

like image 32
dmckee --- ex-moderator kitten Avatar answered Sep 25 '22 06:09

dmckee --- ex-moderator kitten


No, because sometimes you don't have all the information in the place that you reconstruct them. Remember that you may not be reconstructing the object in the same context as where you had it; it may be a different machine or even different language.

like image 38
Noon Silk Avatar answered Sep 22 '22 06:09

Noon Silk