If you’re learning programming, you’ve probably seen words like:
- “Serialize this object”
- “Deserialize the JSON”
- “Convert object to string”
And honestly… it feels confusing.
Why do we need to convert objects at all?
Why can’t we just use them directly?
Don’t worry.
By the end of this article, you’ll clearly understand serialization and deserialization in the simplest way possible.
1️⃣ The Confusion (Why This Feels Hard)
You create an object in your program.
Everything works perfectly.
But then someone says:
- “Send this data to the frontend.”
- “Save this object in a file.”
- “Store it in database.”
- “Send it to another server.”
And suddenly, you’re told:
“First serialize it.”
Now it feels scary.
What does that even mean?
2️⃣ Why Serialization Exists (The Real Problem)
Inside your program, data lives in memory (RAM).
But here’s the problem:
- Memory disappears when program stops.
- You cannot send raw memory to another computer.
- You cannot store memory structure directly in a file.
- Another programming language cannot understand your internal object format.
Computers only understand:
- Text
- Or raw bytes (0s and 1s)
So developers needed a way to:
👉 Convert objects into something that can be saved or sent.
That solution is called Serialization.
And when we convert it back?
That is called Deserialization.
3️⃣ Real Life Analogy (Super Simple)
Imagine you build a LEGO car.
Inside your room, it’s fully assembled.
Now you want to send it to your cousin in another city.
You cannot ship it fully assembled.
So what do you do?
- Break it into pieces.
- Put pieces inside a box.
- Send the box.
Your cousin receives the box and:
- Opens it.
- Rebuilds the LEGO car.
Here:
- Breaking and packing → Serialization
- Rebuilding → Deserialization
That’s it.
Nothing magical.
4️⃣ The Simple Explanation (No Complex Words)
Serialization means:
Converting an object into text or bytes so it can be saved or sent.
Deserialization means:
Converting that text or bytes back into the original object.
In short:
Object → Text → Object
5️⃣ How Serialization Works (Step-by-Step)
Let’s imagine we have this student data:
student = {
"name": "Rahul",
"age": 15
}
This is a Python dictionary stored in memory.
🔹 Step 1: Serialize
We convert it into JSON (a text format):
{"name": "Rahul", "age": 15}
Now it is just text.
This text can:
- Be saved in a file
- Be stored in database
- Be sent through an API
- Be shared between different programming languages
🔹 Step 2: Deserialize
When someone receives this text, they convert it back into an object:
student = {"name": "Rahul", "age": 15}
Now it behaves like normal object again.
6️⃣ Very Small Code Example (Python)
Serialization Example
import json # Library to work with JSON
student = {
"name": "Rahul",
"age": 15
}
# Convert dictionary to JSON string
serialized_data = json.dumps(student)
print(serialized_data)
Output:
{"name": "Rahul", "age": 15}
Deserialization Example
# Convert JSON string back to dictionary
original_data = json.loads(serialized_data)
print(original_data["name"])
Output:
Rahul
7️⃣ Dry Run (Most Important Part)
Let’s slowly walk through it.
Step 1
We create:
student = {"name": "Rahul", "age": 15}
This is a dictionary stored in memory.
Step 2
We do:
serialized_data = json.dumps(student)
Now:
serialized_data = '{"name": "Rahul", "age": 15}'
Important:
This is now a string, not a dictionary.
You cannot do:
serialized_data["name"]
Because it’s just text.
Step 3
We do:
original_data = json.loads(serialized_data)
Now it becomes a dictionary again.
So now:
original_data["name"]
Works correctly.
So the full journey is:
Object → JSON String → Object
8️⃣ Where Serialization Is Used in Real World
Serialization is used almost everywhere:
- 🌐 Frontend sending data to backend
- 📱 Mobile app calling server
- 💾 Saving data in file
- 🗄️ Storing objects in database
- 🔐 Sending data between microservices
- 🎮 Saving game progress
- 📨 Sending data over network
Every time you use an API, serialization is happening behind the scenes.
When Instagram loads your profile…
Serialization and deserialization are working silently.
9️⃣ Common Beginner Mistakes
❌ Thinking JSON is the same as an object
→ JSON is text. Object lives in memory.
❌ Forgetting serialization converts object into string
→ After that, you cannot access properties directly.
❌ Trying to access JSON like dictionary without deserializing
❌ Thinking serialization only means JSON
→ It can also be XML or binary formats.
🔟 Serialization vs Deserialization (Quick Comparison)
| Serialization | Deserialization |
|---|---|
| Object → Text | Text → Object |
| Used before sending or saving | Used after receiving or reading |
| Happens on sender side | Happens on receiver side |
1️⃣1️⃣ When Should You Use Serialization?
Use serialization when:
- You want to send data to another system
- You want to save object permanently
- You want different languages to understand your data
- You are building APIs
- You are working with databases
If data is moving outside your program, serialization is involved.
1️⃣2️⃣ Quick Revision Summary
- Serialization = Object → Text/Bytes
- Deserialization = Text/Bytes → Object
- Needed to send or save data
- JSON is most common format
- After serialization, data becomes string
- Must deserialize before using like object
- Used in APIs, databases, mobile apps, and servers
Now if someone says:
“Serialize this object before sending it.”
You won’t panic.
You’ll just think:
“Ohhh… we’re packing it into a box.”
And that’s it.
Top comments (0)