What is Serialization?
Definition of Serialization:
Serialization is the process of converting a data structure or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and “resurrected” later in the same or another computer environment. [Wikipedia]
In other words, Object serialization is the process of saving an object’s state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.
The object is serialized to a stream, which carries not just the data, but information about the object’s type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.
It lets you take an object or group of objects, put them on a disk or send them through a wire or wireless transport mechanism, then later, perhaps on another computer, reverse the process: resurrect the original object(s). The basic mechanisms are to flatten object(s) into a one-dimensional stream of bits, and to turn that stream of bits back into the original object(s).
Data serialization formats:
These are various ways to convert complex objects to sequences of bits. In .NET, BinaryFormatter and SoapFormatter are prominently used. You can define your own format too. Some other widely used data serialization formats are JSON, XML-RPC, BSON, YAML etc. Each format is having its encoding and decoding rules.
For more details: http://en.wikipedia.org/wiki/Comparison_of_data_serialization_formats
Why it named as Serialization:
Serialization is all about converting objects to raw data. Then how it lends itself towards anything that is related to the term “serial”?
Serialization transforms data in memory with data structures and pointers into a series of bytes designed to be saved on a sequential media, such as a hard disk or transferred through something like a serial connection.
It’s probably from networking and communications, where it is necessary to convert data into a serial stream of ones and zeros. The fact that data is sent to disk or over a network as a “stream” is almost certainly the origin of this term.
Another way to think of it, however, is like this: The state of a program is spread all over memory, with pointers/links pointing all over the place. You have arrays, lists, trees, call stacks, heaps of allocated memory, etc., in no particular order. When you want to save some state, you cannot use much of the information in your program. For example, any pointer values or stack offsets (whether used directly or used internally by the language runtime, etc.) will likely not be valid the next time your program runs because the heap will have been used to allocate slightly different series of memory blocks. Saving these pointer values would be useless. To save some state, you have to “put your affairs in order” such that the information being saved is only that part that will be relevant later on.
Uses of Serialization:
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like:
- Sending the object to a remote application by means of a Web Service
- Passing an object from one domain to another
- passing an object through a firewall as an XML string
- Maintaining security or user-specific information across applications
- Passing an object from on application to another
- Storing user preferences in an object
- Modification of XML documents without using the DOM
Are methods also serialized along with the data members?
No. Method bodies do not hold any information on the state of an object, so they are not needed in order to save or restore the state.
Useful links on Serialization: