Serialization Formats


There are three major formats of serialization available: binary, XML, and SOAP. The most obvious difference between these formats is the physical output of the serialization process and, of course, the source format from which the object is deserialized. Binary serialization produces a non-printable sequence of byte-oriented data that represents the source object. For example, if a class is binary serialized to a file, the file contents might look like the following code, where the left side contains the raw hex and the right side contains the printable characters (with a dot indicating a non-printable character):

 0001000000FFFFFF-FF01000000000000................ 000C020000003D73-696D706C652C2056......=simple,V 657273696F6E3D30-2E302E302E302C20ersion=0.0.0.0, 43756C747572653D-6E65757472616C2CCulture=neutral, 205075626C69634B-6579546F6B656E3DPublicKeyToken= 6E756C6C05010000-002453696D706C65null.....$Simple 42696E6172795365-726C69617A617469BinarySerliazati 6F6E2E4D79426173-6963446174610300on.MyBasicData.. 000009496E744669-656C64310C537472...IntField1.Str 696E674669656C64-3109496E74466965ingField1.IntFie 6C64320001000808-020000002A040000ld2.........*... 06030000000F4261-7369632044617461......BasicData 20496E666F380600-000BInfo8.... 

Another characteristic of binary serialization is that it retains type information within the generated data stream, which means that when the object is deserialized, the re-created object is an exact copy of the original.

The advantage of binary serialization is that the resulting serialized stream is very compact, and the disadvantage is that a binary stream is not very portable. As you ll see later in this section, the difference in the serialized size between binary and the other serialization methods can be great, which can have considerable impact if the data is being serialized to the network. If both the producer and the consumer of the binary stream use the .NET Framework, portability won t be an issue. However, if the application needs to send a serialized object to an application running on a different operating system, portability issues are likely because possible differences in byte ordering and data type sizes could introduce compatibility problems.

XML serialization, on the other hand, is all about standards and portability. When a class is serialized to XML, a character stream is created that s formatted according to the XML language standard. XML is an incredibly flexible markup language used to describe arbitrary data. The element tags used to describe the data are specific to the data itself. The following is an example of valid XML data:

 <?xmlversion="1.0?> <MyBasicDataxmlns:xsd="http://www.w3.org/2001/XMLSchema  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <IntField1>1234</IntField1> <StringField1>HelloWorld</StringField1> </MyBasicData> 

The element tags in this example are MyBasicData , IntField1 , and StringField1 . Because XML allows arbitrary element names , they could be named something completely different. Of course, if XML is being used to exchange information, the producer and the consumer need to agree on a set of standard element tags to describe the data. As you can see in the previous example, the serialized XML does not contain any type information unless the element tags chosen somehow describe the data type of the element value. For example, there s no indication whether the element value 1234 is an integer or a string.

As mentioned, XML is ideal when portability is an issue. Also, because many services today use XML to describe data, it s easy to write code that outputs XML in a specified format that can be used by other entities. The portability and interoperability gains do come with a cost: the generated XML stream is significantly larger than the equivalent binary serialized stream, and XML serialization does not retain type information. Table 4-1 compares the size of the serialized object for both binary and XML serialization in the .NET Framework version 1.

Table 4-1: Serialization Efficiency Comparison

Serialization Class

Type

Class Size (Bytes)

Field Count

Serialized Size (Bytes)

BinaryFormatter

binary

24

3

202

XmlSerializer

XML

24

3

745

BinaryFormatter

binary

4020

3 (array)

4213

XmlSerializer

XML

4020

3 (array)

16827

The class size column indicates the size of all member properties of the class. Field count indicates the number of properties contained in the class, and the serialized size column indicates the size of the resulting serialized object in bytes. Note that the second pair of entries is a class with three fields of which one is a 1000-element array. As you can see, for binary serialization, there s roughly a 180-byte fixed overhead for serialization regardless of the original class size. On the other hand, with XML serialization, the serialized data size is not directly tied to the field count or the class size because the XML element names generated depend on how the class is defined. We ll discuss the BinaryFormatter and XmlSerializer classes in the next two sections.

The last serialization type is a protocol based on XML called SOAP, which is a standard method for describing, discovering, and invoking methods. The XML language itself imposes no restrictions on how to describe the meaning of the data; it only defines the format and sequence of the tags that describe the data. The SOAP specification defines the set of common element tags and properties to describe data. The SOAP specification can be found at http://www.w3.org/TR/SOAP .

Although the SOAP protocol is transport-independent, meaning that it can be sent over a number of different transport protocols, SOAP is typically transported over the HTTP protocol in version 1 of the .NET Framework and is an integral part of XML-based Web services, which are covered in greater detail in Chapter 12. The following code listing shows a class serialized to the SOAP protocol:

 <SOAP-ENV:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance  xmlns:xsd="http://www.w3.org/2001/XMLSchema xmlns:SOAP- ENC="http://schemas.xmlsoap.org/soap/encoding/ xmlns:SOAP- ENV="http://schemas.xmlsoap.org/soap/envelope/  xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0 SOAP- ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <a1:MyBasicDataid="ref-1  xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SimpleBinarySeriali zation/simple%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20Publ icKeyToken%3Dnull"> <IntField1>999</IntField1> <StringField1id="ref-3">HelloWorld</StringField1> <IntField2>1492</IntField2> </a1:MyBasicData> </SOAP-ENV:Body> </SOAP-ENV:Envelope> 

Now it s time to get into the details of serialization, and we ll start with binary serialization.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net