views:

366

answers:

4

Hi all.

What are Object Serialization and Deserialization? What difference does Serialization have with normal techniques like reading an object's properties and then filling a DataRow's columns with them and finally saving the DataRow in DB ?

Thank you

+1  A: 

Serialisation is, generally, the process of writing the state of an object in your runtime to the disk (but it can be anywhere), and being able to read it back again.

Effectively, storing the properties of an object into a table is a form of serialisation.

In .NET, there are other forms:

  • XmlSerialization
  • BinarySerialization

You can make up your own.

But in general, if you are saving the state of your object somewhere, and then reading it back again into a 'live' object in your runtime, you are serialising it.

Noon Silk
Serialization doesn't necessarily mean writing it to disk. It just means putting it into some binary format that can be exported. A serialized object might be sent over the network rather than written to disk, for example.
Chuck
Yes, that's why at the bottom I wrote 'somewhere'. I just missed a 'generally', in the first sentence. You're right, of course.
Noon Silk
+5  A: 

Serialization generally refers to creating a version of the data (rather than the objects) that can be used for storage (perhaps in a file), for transfer over a network, or perhaps just for transfer between processes / AppDomains /etc on a single machine.

Serialization typically means writing the data as a string (think: xml / json) or as raw binary (a byte[] etc). Deserialization is the reverse process; taking the raw data (from a file, from an incoming network socket, etc) and reconstructing the object model.

The difference between using a db is that it has no intrinsic tabular layout, and no real tie to a database; the data can be any shape, and tends to map more closely to the object-oriented layout than to the rows/columns nature of tables.

Most platforms have a range of serialization tools. For example, it sounds like you're talking about .NET - so BinaryFormatter (.NET-specific), XmlSerializer, DataContractSerializer, Json.NET and protobuf-net / dotnet-protobufs would all qualify.

Marc Gravell
+2  A: 

Serialization = putting the relevant state of the object into a streamable representation. That can mean converting it to a byte stream. This does not necessarily include copying every member variable into the stream. Classic example that is used by Joshua Bloch in Effective Java is a HashSet. You would just serialize the elements in the Hashset but not the keys.

Deserialization = restoring an object from a serial representation and ensuring the invariants of the object. Deserialization can be thought of a separate constructor for the object. In the case of the HashSet mentioned above you would create a new HashSet and then insert the values from the stream into this new data structure.

jens
upvoted for etymological hints. Serialization brings an object *tree* (hierarchical) into a serial (flat) representation, without loosing semantics.
Daren Thomas
It's got nothing to do with whether or not you condense it from a 'tree' to a 'flat' representation. You can serialise to a database that is still in a tree format, and deserialise out of that. It is not relevant.
Noon Silk
I do not think that in general a db version is a serial version of an object. The data contained in an object can be split over several tables. The idea of serialization is creating a representation that can be transferred bit by bit (i.e. serially).
jens
A: 

Serialization means, that you persist your object into a representation, that you can store somewhere. One way to do so is just to take the pointer to where your object is stored in the memory and write every byte as it is to a file. Since that representation is very specific to your programming language (and how it represents objects in the memory), an improvement would be to convert your object into a String representation which has a certain well known structure (like XML or JSON), so that you can

a) transfer it easier

b) Store and restore it easier

c) Since everybody knows how the format is defined, any other programs can read your object, too

So putting you object into a database is just another form of serialization, too.

Deserialization means, that you can load/restore that object again from where you saved it to.

Daff