Serialization
What is it?
Serialization is converting in-memory data (like an object) into a format that can be stored or sent, such as JSON or bytes.
Explain like I'm 5
Why was it created?
Programs hold data in memory structures that can't be sent or saved directly. Serialization was created to turn them into a portable form.
Where is it used?
- Sending data over APIs
- Saving data to files or databases
- Messaging between services
- Caching objects
Why should developers care?
Every API call, saved file, or message between systems involves serialization, so it's a constant in everyday development.
How does it work?
A serializer walks an object's fields and writes them in a chosen format — text like JSON or compact binary. The data can then be stored or transmitted, and later turned back into an object by deserialization.
Real-world example
Before an API sends a user object, it serializes it to JSON text; the receiver later deserializes that JSON back into its own object.
Common use cases
- API request/response bodies
- Persisting data
- Inter-service messaging
- Storing cached values
Advantages
- Makes data portable
- Enables storage and transmission
- Format choices for size or readability
- Language-interoperable (with standard formats)
Disadvantages
- Overhead in time and size
- Format mismatches cause errors
- Deserializing untrusted data can be a security risk
- Schema changes need care
When should you use it?
Whenever data must leave memory — sent, stored, or shared.
When should you avoid it?
Not avoidable for I/O; the choice is which format and how carefully you handle it.
Alternatives
Related terms
Interview questions
Beginner
- What is serialization?
- Name a common serialization format.
Intermediate
- What's the trade-off between JSON and binary formats?
- Why can deserializing untrusted data be risky?
Senior
- How do you handle schema evolution in serialized data?
- When would you choose Protocol Buffers over JSON?
Common misconceptions
- "Serialization always means JSON" — JSON is common, but binary formats are often used for speed and size.
- "Deserializing data is always safe" — untrusted input can be a security risk if not handled carefully.
Fun facts
- JSON is human-readable; formats like Protocol Buffers are compact binary.
- Serialization is sometimes called marshalling.
Timeline
- 2000s — JSON rises as the dominant web serialization format
Learning resources
Quick summary
Serialization turns in-memory data into a storable, sendable format like JSON or bytes, so it can be saved or transmitted.
Cheat sheet
- Object → portable format
- JSON, binary, etc.
- Needed for I/O and messaging
- Mind size, schema, and security