Serialization vs Deserialization Explained Simply
Whenever two applications communicate over a network, they need a common language to exchange data.
For example:
- Frontend → JavaScript (React, Angular)
- Backend → Java, Spring Boot
- Another Service → Python, Go, Rust
Since every language has different data types and internal structures, they cannot directly understand each other’s objects.
This is where Serialization and Deserialization come into the picture.
What is Serialization?
Serialization is the process of converting an object into a standard format that can be:
- Sent over a network
- Stored in a file
- Stored in a cache
- Sent to another application
Example
Java Object:
Employee emp = new Employee(1, "Prakash");After Serialization (JSON):
{
"id": 1,
"name": "Prakash"
}The Java object is converted into JSON, which any system can understand.
Simple Definition
Serialization = Converting an object into a transferable format like JSON, XML, or Protocol Buffers.
What is Deserialization?
Deserialization is the reverse process.
It converts the received JSON (or another format) back into an object.
Example
Received JSON:
{
"id": 1,
"name": "Prakash"
}Converted back to Java Object:
Employee emp = new Employee(1, "Prakash");Simple Definition
Deserialization = Converting JSON/XML/Binary data back into an application object.
Why Do We Need Serialization?
Imagine the following architecture:
React Application
|
| HTTP Request
v
Spring Boot Service
|
| REST Call
v
Python ServiceThe React application cannot send a JavaScript object directly to Spring Boot.
Similarly, Spring Boot cannot send Java objects directly to Python.
Instead, everyone agrees on a common format:
{
"id": 1,
"name": "Prakash"
}This common format makes communication language-independent.
Real-World Request Flow
Step 1: Frontend Creates Object
const employee = {
id: 1,
name: "Prakash"
};Step 2: Serialization Happens
The object is converted into JSON:
{
"id": 1,
"name": "Prakash"
}Step 3: JSON Travels Through Network
Frontend
|
| JSON
v
Internet
|
v
BackendStep 4: Backend Deserializes
Spring Boot converts JSON into:
Employee employee;Step 5: Business Logic Executes
employeeService.save(employee);Step 6: Backend Sends Response
{
"message": "Employee Created"
}Step 7: Frontend Deserializes Response
const response = {
message: "Employee Created"
};This entire process is Serialization and Deserialization.
Common Serialization Formats
1. JSON (Most Popular)
{
"id": 1,
"name": "Prakash"
}Advantages
- Human readable
- Lightweight
- Supported everywhere
- Most commonly used in REST APIs
2. XML
<Employee>
<id>1</id>
<name>Prakash</name>
</Employee>Advantages
- Structured
- Used in legacy enterprise systems
Disadvantages
- Verbose
- Larger payload size
3. YAML
id: 1
name: PrakashMostly used for:
- Kubernetes
- Docker Compose
- Configuration files
4. Protocol Buffers (Protobuf)
Binary format developed by Google.
Example:
message Employee {
int32 id = 1;
string name = 2;
}Advantages
- Extremely fast
- Smaller payload
- Used in gRPC
JSON Structure Rules
A valid JSON must follow these rules:
Keys Must Be Strings
✅ Correct
{
"name": "Prakash"
}❌ Wrong
{
name: "Prakash"
}Values Can Be
String
{
"name": "Prakash"
}Number
{
"age": 25
}Boolean
{
"active": true
}Array
{
"skills": ["Java", "Spring Boot"]
}Nested Object
{
"address": {
"city": "Bangalore",
"country": "India"
}
}Serialization in Spring Boot
Spring Boot uses the library:
Jackson
When returning:
@GetMapping("/employee")
public Employee getEmployee() {
return new Employee(1, "Prakash");
}Spring Boot automatically serializes:
Employeeinto
{
"id": 1,
"name": "Prakash"
}before sending the response.
Deserialization in Spring Boot
When client sends:
{
"id": 1,
"name": "Prakash"
}and controller receives:
@PostMapping("/employee")
public void createEmployee(
@RequestBody Employee employee) {
}Jackson automatically converts JSON into:
Employee employeeThis is Deserialization.
Serialization in Microservices
Consider:
User Service
|
| JSON
v
Order Service
|
| JSON
v
Payment ServiceEvery service:
- Serializes Java objects to JSON.
- Sends JSON over HTTP.
- Deserializes JSON back to Java objects.
Without serialization, microservices would not be able to communicate.
Interview Definition
Serialization
Serialization is the process of converting an object into a standard format such as JSON, XML, or Protocol Buffers so that it can be transmitted over a network or stored.
Deserialization
Deserialization is the process of converting serialized data back into an object that an application can understand and use.
One-Line Memory Trick
Java Object
|
| Serialization
v
JSON
|
| Network
v
JSON
|
| Deserialization
v
Java ObjectRemember:
- Object → JSON = Serialization
- JSON → Object = Deserialization
This is one of the most fundamental concepts behind REST APIs, microservices, Kafka messaging, Redis caching, file storage, and distributed systems.