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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With