Reputation: 43
I understand what serialization is. But my question is; why do we have to do it? Why is it better to serialize an object for network data transfer? It is for the purpose of providing a technology agnostic web service, speed, both, neither, etc.
Upvotes: 2
Views: 156
Reputation: 366
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, or maintaining security or user-specific information across applications.
There are several reasons:
Communication: If you have two machines that are running the same code, and they need to communicate, an easy way is for one machine to build an object with information that it would like to transmit, and then serialize that object to the other machine. It's not the best method for communication, but it gets the job done.
Persistence: If you want to store the state of a particular operation in a database, it can be easily serialized to a byte array, and stored in the database for later retrieval.
Deep Copy: If you need an exact replica of an Object, and don't want to go to the trouble of writing your own specialized clone() class, simply serializing the object to a byte array, and then de-serializing it to another object achieves this goal.
Caching: Really just an application of the above, but sometimes an object takes so much time to build, but would only take 10 seconds to de-serialize. So, rather than hold onto the giant object in memory, just cache it out to a file via serialization, and read it in later when it's needed.
Check the URL:
https://msdn.microsoft.com/en-us/library/ms233843.aspx
Upvotes: 2