Serialization vs Deserialization with Example
A simple explanation of serialization and deserialization in web development. Learn how data is converted and transferred between frontend and backend using JSON, with clear examples.
As a developer, sometimes you may have questions either from others or yourself, about what serialization and deserialization are. Let’s look at them in detail based on what I know.
First of all, whenever we work with both frontend and backend, there is always some form of input and output being exchanged, and it must be in a specific format. For example, if you have a JavaScript-based client app and a backend written in Java, Rust, or Go, they don’t share the same data types or memory.
To communicate, they need a common representation of data. One of the most common formats we use is JSON. During this communication, serialization and deserialization happen behind the scenes even if we don’t always notice them.
Serialization
Serialization is the process of converting an in-memory object or array into a format that can be easily stored or transmitted over a network.
In JavaScript, the most common serialization format is JSON (JavaScript Object Notation) because it is readable by both humans and machines.
Example of serialization in JavaScript using JSON:
1const data = { id: 123, name: "yash", role: "developer" };23const jsonString = JSON.stringify(data);4// Output: '{"id":123,"name":"yash","role":"developer"}'
Deserialization
Deserialization is the reverse process of serialization. It converts the formatted data back into native objects or arrays.
When a client sends data over an HTTP request, the data is first serialized into JSON format. It is then transmitted over the network (along with other protocol-related information). When the receiver gets it, the data is deserialized back into the required format.
1const jsonString = '{"id":123,"name":"yash","role":"developer"}';23const data = JSON.parse(jsonString);4// Output: { id: 123, name: "yash", role: "developer" }
How the interaction works
A typical interaction looks like this:
- The client creates an object
- The object is serialized into JSON (or another format)
- The serialized data is sent over HTTP
- The server deserializes the data back into an object
- Business logic is executed
- The response is serialized again and sent back
- The client deserializes the response
JSON has become the default format because it is simple and easy to understand. However, there are other formats as well:
- Text-based: JSON, XML, YAML
- Binary formats: Protocol Buffers (Protobuf)
Conclusion
In summary, serialization is the process of converting data into a structured format suitable for storage or transmission, while deserialization converts that data back into its original form.
Serialization and deserialization often go unnoticed because they work silently in the background. However, they are fundamental to communication in distributed systems and are used in almost every backend system.