Serialization & Deserialization

Lets start with a simple defination:

Serialization : Means Converting an Object into String to send it over a network.

Deserialization: Means converting String back into Object at destination.

Let Understand with a simple example:

Teleporting Humans with JavaScript;

Have you ever watched a sci-fi movie and wondered how teleportation works? Someone steps into a machine, vanishes, and then reappears miles away in an instant. While we can't teleport people (yet!), we use the exact same concept every single day to move information around the world. In the world of programming, this magic is called serialization and deserialization.

Let's imagine we want to teleport a person named Ada from Lucknow to London.

Step 1: Serialization – The Dematerializer ⚛️

First, the teleporter in Lucknow needs to scan Ada and convert her into information. It can't send her physical body through a wire, but it can send a detailed description of her. This description would include everything: her height, her name, her age, even the fact that she knows how to code!

This process of converting a complex object (like a person) into a simple, storable, or transmittable format (like a text file) is called serialization.

In JavaScript, our "person" is an object. An object is just a way to group related information together. Here’s what Ada looks like as a JavaScript object:

// This is an object representing our person, Ada
let ada = {
  name: "Ada Lovelace",
  age: 36,
  isProgrammer: true,
  skills: ["math", "logic", "coding"]
};

Now, we need to serialize this ada object so we can send it over the internet. We'll convert it into a special text format called JSON (JavaScript Object Notation). JSON is a universal language that many different computer systems can understand.

We use the JSON.stringify() command to do this:

// Serialize the 'ada' object into a JSON string
let adaAsAString = JSON.stringify(ada);

console.log(adaAsAString);
// Output: '{"name":"Ada Lovelace","age":36,"isProgrammer":true,"skills":["math","logic","coding"]}'

This string is our "instruction manual" for rebuilding Ada. It's now just a piece of text that can be easily sent to the teleporter in London.

Step 2: Transmission & Deserialization – The Rematerializer

The string of text containing all of Ada's information travels from Lucknow to London through the internet—our data stream.

Once the teleporter in London receives this string, it needs to read the instructions and rebuild Ada exactly as she was. This process of converting the simple string format back into a complex, usable object is called deserialization.

In JavaScript, we use the JSON.parse() command to turn the string back into an object:

// This is the string received in London
let receivedString = '{"name":"Ada Lovelace","age":36,"isProgrammer":true,"skills":["math","logic","coding"]}';

// Deserialize the string back into a JavaScript object
let rebuiltAda = JSON.parse(receivedString);

console.log(rebuiltAda.name); // Output: Ada Lovelace
console.log(rebuiltAda.skills); // Output: ["math", "logic", "coding"]

The rebuiltAda object in London is a perfect copy of the original ada object from Lucknow. Teleportation complete!

The Perils of Teleportation: What Can Go Wrong? ⚠️

Just like in sci-fi movies, teleportation can have its challenges. These are similar to the problems programmers face with serialization and deserialization.

  • Data Loss (Missing Pieces): What if the teleporter's scanner can't read certain things? For instance, it can scan Ada's physical attributes but can't scan her ability to speak French. When she's rebuilt, that skill is lost. Similarly, JSON.stringify() ignores certain data types, like functions. If our ada object had a function (e.g., sayHello: function() { ... }), it would be lost during serialization.

  • Data Corruption (Scrambled Data): What if there's static during the transmission and the data string gets scrambled? The London teleporter might receive "name":"Ada Lovelace","age":36..##@!. It won't know how to read these garbled instructions and will throw an error, failing to rebuild Ada. This is like trying to use JSON.parse() on a broken or incomplete string.

  • Format Incompatibility (Speaking Different Languages): Imagine the Lucknow teleporter creates its instruction manual in English, but the London teleporter only understands French. It won't be able to rebuild Ada. This is why standardized formats like JSON are so important. They act as a universal language that ensures different systems (like a web server and your browser) can understand each other perfectly.

So, while we're not beaming people across the globe yet, we are constantly serializing and deserializing data to make the modern internet work. Every time you load a webpage, watch a video, or send a message, you're using this fundamental concept of turning complex information into simple text and back again.

0
Subscribe to my newsletter

Read articles from Syed Wasif Hussain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Syed Wasif Hussain
Syed Wasif Hussain