1

I've been searching and reading about object serialization and related subjects. However I can't think of any use-cases.

Or what is the motivation or situation in which one must use it?

Follow up question: If serialization is to save the state of an object, why is Java's object serialization necessary? Can't the object's configuration be saved in a text file and send that over network instead?

drum
  • 5,416
  • 7
  • 57
  • 91
  • Sometimes you need to store the state of an object (measured by the values which describe the object) into some medium outside of the application. Whether it's to save it for later, transfer it to another application, etc. Technically when you save data to a database you're "serializing" an object in a specific format. – David Aug 02 '13 at 19:22
  • @David I've posted a follow up question. – drum Aug 02 '13 at 19:29
  • 2
    If the serialization is for the purpose of sharing the object with other systems (even entirely other platforms) then standards in the data formats are important. Yes, one can just manually write to a text file. But then one needs to define the standards of that file, accounting for all variations of the object, and anybody who wants to consume that data has to write custom code to meet that standard. It's a lot easier to use existing standards. – David Aug 02 '13 at 19:32
  • Saving an object's configuration in a text file is nothing but a special case of serialization. – 500 - Internal Server Error Aug 03 '13 at 07:07

3 Answers3

2

Serialization is very powerful and has multiple uses, besides the already mentioned by other users:

Sometimes you want to deep copy an object and a simple way to do it without falling into a reflection nightmare is by serializing it: How do you do a deep copy of an object in .NET (C# specifically)?

Another could be data validation. Normally when you plan to transfer objects across the wire you create an XML Schema that defines your message structure, you then generate classes from that schema which will turn into objects that you can transfer. Before sending your objects you want to serialize , for example to XML, to preserve the object state, then send that stream of data and on the other end validate your incoming XML message against the schema to make sure it is complies with your pre-defined structure and then deserialize into an object to do any object-oriented manipulation required.

Community
  • 1
  • 1
Adolfo Perez
  • 2,834
  • 4
  • 41
  • 61
1

Check MSDN: http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx

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.

Martijn van Put
  • 3,293
  • 18
  • 17
1

Sometimes you need to store the state of an object. This article covers the topic pretty well. Quoting from the article on reasons to serialize:

Serialization provides:

  • a method of persisting objects, for example writing their properties to a file on disk, or saving them to a database.
  • a method of remote procedure calls, e.g., as in SOAP.
  • a method for distributing objects, especially in software componentry such as COM, CORBA, etc.
  • a method for detecting changes in time-varying data.
Adam Lear
  • 38,111
  • 12
  • 81
  • 101
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740