17.6 Serialization

Section 17.3 demonstrated how to write the individual fields of a Record object to a text file, and Section 17.4 demonstrated how to read those fields from a file and place their values in a Record object in memory. In the examples, Record was used to aggregate the information for one record. When the instance variables for a Record were output to a disk file as text, certain information was lost, such as the type of each value. For instance, if the value "3" is read from a text file, there’s no way to tell if the value came from an int, a string or a decimal. We have only data, not type information, on disk. If the program that’s going to read this data “knows” what object type the data corresponds to, then the data can be read directly into objects of that type.

In Fig. 17.6, we know that we’re inputting an int account number, followed by first name and last name strings and a decimal balance. We also know that these values are separated by commas, with only one record on each line. So, we are able to parse the strings and convert the account number to an int and the balance to a decimal. Sometimes it would be easier to read or write entire objects. C# provides such a mechanism, called object serialization. A serialized object is an object represented as a sequence of bytes that includes the object’s data, as well as information about the object’s type and the types of data stored in the object. After a serialized object has been written to a file, it can be read from the file and deserialized—that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

Class BinaryFormatter (namespace System.Runtime.Serialization.Formatters.Binary) enables entire objects to be written to or read from a stream in binary format. BinaryFormatter method Serialize writes an object’s representation to a file. Binary-Formatter method Deserialize reads this representation from a file and reconstructs the original object. Both methods throw a SerializationException if an error occurs during serialization or deserialization. Both methods require a Stream object (e.g., the FileStream) as a parameter so that the BinaryFormatter can access the correct stream.

In Sections 17.7–17.8, we create and manipulate sequential-access files using object serialization. Object serialization is performed with byte-based streams, so the sequential files created and manipulated will be binary files. Binary files are not human-readable. For this reason, we write a separate app that reads and displays serialized objects. Other serialization formats are available that are both human- and machine-readable. For example,

  • the XmlSerializer class (namespace System.Xml.Serialization) can read and write objects in XML (Extensible Markup Language) format and

  • the DataContractJsonSerializer class (namespace System.Runtime.Serial-ization.Json) can read and write objects in JSON (JavaScript Object Notation) format.

XML and JSON are popular formats for transferring data over the Internet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset