Introduction To Serialization In .NET

Introduction

 
In the modern era of Computer Science, serialization and deserialization are very important topics and have gained merit with the passage of time. Since ancient times, human beings could communicate with each other and exchange messages in a secure way. Now, in this age, serialization comes before the communication layer used to convert one state or thing to another. When we have done an operation on a thing, the operation is referred to as "Serialization”.
 
Purpose of Serialization
 
In the modern era, an application does not consist of a single state; there are multiple states, either on the server-side, client-side, mobile-side, or even the embedded side so communication happens back and forth. Before communication or storing something, we change the state of the information, keep things light, keep it more secure, and send over the medium named Protocols in terms of Computer Science. So, Serialization is the backbone of the real-time application to exchange the data back and forth. Today, I can say that almost 99.99 % of applications use Serialization/Deserialization for communication and to exchange data-keeping things light and secure.
 
Before Start
 
Well, when I prepared for my C# certification exam, I gave more time to study about serialization and understood how to implement it in real-world applications. Also, I did learn what the natural flow is, what its benefits are, its disadvantages, and the different types of serialization. So now, I can teach about Serialization/Deserialization to the developers at beginner and intermediate levels. This is the series of training lectures that imparts the details of Serialization/Deserialization.
 
Prerequisites
 
Must have knowledge of System.IO namespace; not complete but a little knowledge of MemoryStream, Fieldstream, and some basic stream classes, etc.
 
Now, let’s start.
 
My whole lectures are revolving around Serialization/Deserialization, streams, and some basic words so it’s better to get an overview of some theories, and then, we will move towards the code and implementation. Well, personally, I don’t like words I like implantations so for me, I think it’s a boring kind of stuff but it is important to understand some basic terms which are the building block of Serialization/Deserialization.
 
Stream
 
Out of the box, “stream is the consecutive sequence of bytes which are used to read/write data to/from the storage medium in which the nature of the medium is independent”. In other words, we can say that the byte array having some specific length is known as the stream like we can say that the cricket match is streaming. The online streaming carries the bytes of arrays of a stream.
 
Storage media
 
Storage media is a place where we store the data in the form of bytes like a file stream, Memory and sockets, XML, ZIP, JSON, JPEG, and other memory sticks.
 
Object Graph
 
“Object Graph is referred to as an in-memory representation of the object."  How objects refer to each other” is known as the object graph. As we know that objects are stored in the heap memory having some reference, so in the heap memory, objects refer to each other. For example, we can say that a book object is having a reference to the page object and a Person object can have the reference of the book object. So, some connectivity between the objects is created which is known as an Object Graph.
  • Remember that the class structure “equals to” object graph,
     
    C#
This is the simple shape or example of the in-memory representation of the objects, which is known as an Object Graph.
 
Serialization
 
In simple words, “Serialization is the process in which we convert the in-memory representation of the object named as Object Graph into a sequence of the bytes which can be persisted and transmitting over the media later.
 
C#
 
Deserialization
 
The reverse process of Serialization is known as Deserialization, i.e., converting the stream of bytes into an object graph that is usable for the application. It has exact reverse steps of the serialization.
 
Media---------------> Bytes-------------> Deserialization-------> Object Graph-------->Application
 
Next, we will talk about the scenario where we use Serialization in practice.
 
C#
 
  • Windows backup, database backups, and software backups are the persistent implementations of the serialization; upon restoration, the deserialization happens and we get the data.
  • When the application is sitting idle in the memory,  we serialize the state of the memory and store it in the physical media and utilize the memory. I found an example of that type in Microsoft BizTalk Server.
  • When we work with the web-based application, we have different types of variable application states - session states, and view states. Sometimes, we want to save the value data on the physical media which resides in these variable. There, we serialize it and save it.
  • Any kind of data which is stored on the physical media must pass through the process called Serialization.
     
  • In Windows, we know that different types of processes work simultaneously. Sometimes, we want to communicate with the other process and, for that, we use the transmission mechanism of the serialization process.
  • Microsoft Communication Foundation framework extensively uses the Serialization process.
  • AJAX web request, Web API, Web Services extensively use Serialization/Deserialization.
What’s Next
 
This is the introduction lecture only. In the next few lectures, we will talk about the types of Serialization in detail with practice code.
 
We will discuss the advantages and disadvantages of each type of Serialization/Deserialization over other types.
 
Stay Tuned! 


Recommended Free Ebook
Similar Articles