Editorials

Types of Serialization in Dot Net

It’s easy to overlook the importance of Serialization because it is a mature technology with many implementations. So, because it is generally so simple, we don’t talk about it much. For my discussion today I’m going to focus on Dot Net serialization based on personal familiarity.

As I stated yesterday, the topic of serialization is the conversion of data from one form to another through serialization, convert data to a different form, to deserialization, reverse back to the original form.

There are many different kinds of serialization you can use, and the choice may depend on the specific application. The simplest method of serialization is built right into the Dot Net object class, from which most types are inherited, allowing for a simple binary serialization and deserialization. This is the easiest form of serialization, but is not readily transportable over a many different network protocols. So, it works great for cloning objects, and saving objects to disk, etc. It can even be used for working with remote objects in another process. Binary serialization will serialize pretty much anything, since everything ultimately inherits from object.

Two other kinds of serialization are popular because they may be easily passed on the HTTP protocol, because they are strictly text. XML and JSON are the two specifications that fall into this category, and are the most popular.

Most XML serialization is done using Microsoft libraries. The thing I don’t like about MS XML serialization is that it often requires the use of data annotations to the class being serialized. By nature, good object oriented practices require that a class not know anything about external processes that manipulate them. If you have to annotate properties of a class in order to cause it to be serializable then the class is has too much external influence.

JSON, on the other hand, has not been implemented in this way. Microsoft has a JSON serialier. There are other JSON serializers as well, NewtonSoft JSON.Net being one of the most popular, and included in most Microsoft project templates that rely on serialization. The only thing required for Newtonsoft to serialize a class is for the class to be decorated as Serializable. In fact, this is the case for all methods of serialization.

There are few kinds of objects that don’t always serialize well, and you’ll need to read up on how to handle them. Hash tables and dictionaries, classes with Key Value Pairs, don’t always serialize well. With each release of Dot Net this is less of an issue.

Of course you can write your own serialization routines. Implementing the ISerializable interface isn’t the most complicated thing to do. Usually the serialization techniques available out of the box, or supported in open libraries are more than sufficient. I have experienced a time when I needed optimum serialization performance, and did roll my own implementation.

Before I leave the topic, you can consider Compression and Encryption as another form of serialization as well. In short you are converting a stream representing objects from one form into another.

That’s enough on serialization for a long while. It is a simple topic with large impact, because it is used an nearly every application. When it doesn’t work, or doesn’t work well, you’ll know it.

Cheers,

Ben

Facebooktwittergoogle_plusredditpinterestlinkedinmail
  • Eilenblogger

    I think the reason to use XML Serialization data annotations is so the flavor of XML is SOAP.

    Also there is nothing different between using data annotations for XML Serialization vs using data annotations for data validation such as range checks.

    In both cases the classes don’t “know” anything about the external processes that manipulate them. The decorators simply indicate that “I want to be serialized using the SOAP specification” or “I want to be range checked”. That doesn’t give it “knowledge” about the external process that manipulates them.

    To say that the Decorator pattern implementation of .Net for these things is not object oriented is to say that the Decorator pattern is not object oriented.