Serialization and Deserialization are the fundamental concepts that help in transferring the data across the network. The data is converted to the required format for transferring the data from one place to another place. In this article you will understand the fundamentals of the Serialization and Deserialization.
What is Serialization ?
Serialization is the process of converting an object's state or data into a form that can be transferred over a network. There are many forms of serialization.
Formats of Serialization
Binary form
JSON
XML
Protocol Buffers
YAML
Avro
Importance of Serialization
Serialization helps to store the state of data in the hard-disks or other storage devices.
Serialized objects allow the user to send the data to send over the network.
Communicating between the API’s.
Sending and receiving through web sockets.
What is Deserialization ?
Deserialization is the process of converting the stream of data to human readable form which is present in the storage or received from the network.
Working of Serialization
Now we will understand the working of serialization using JSON/BSON in javascript.
const data = {
name: "Sumukha Sureban",
age: 23,
city: "Bengaluru",
country: "India"
}
// Serializing
const serializedData = JSON.stringify(data);
console.log(serializedData);
// Deserialization
const deserializedData = JSON.parse(serializedData);
console.log(deserializedData);
Output
{"name":"Sumukha Sureban","age":23,"city":"Bengaluru","country":"India"}
{
name: 'Sumukha Sureban',
age: 23,
city: 'Bengaluru',
country: 'India'
}
Converting to Byte format
const data = {
name: "Sumukha Sureban",
age: 23,
city: "Bengaluru",
country: "India"
}
// Serializing
const jsonString = JSON.stringify(data);
// Converting to bytes
const encoder = new TextEncoder();
const byteArray = encoder.encode(jsonString);
console.log(byteArray)
Output
Uint8Array(40) [
123, 34, 110, 97, 109, 101, 34, 58, 34,
65, 108, 105, 99, 101, 34, 44, 34, 97,
103, 101, 34, 58, 50, 53, 44, 34, 105,
115, 65, 100, 109, 105, 110, 34, 58, 116,
114, 117, 101, 125
]
{ encoding: 'utf-8' }
Why can’t we send the JSON objects directly ?
You might be having a question why the objects or data is not directly passed in the form
Programming Languages Store Data Differently
JavaScript stores objects in memory as complex structures with pointers.
Other languages (like Python, Java, or Go) store objects differently.
If you send a raw JavaScript object, another system (e.g., a Python server) won't understand it.
Memory Representation is Not Transferable
Objects in JavaScript exist in RAM (Random Access Memory) and are not stored in a universal format.
Memory addresses & references do not make sense outside the JavaScript runtime.
Networks & Storage Only Understand Text/Binary
Networks transmit bytes, not objects.
Databases & file systems expect structured text (JSON, XML) or binary data (Protobuf, MessagePack).
Conclusion
Serialization is a crucial part of transferring data from one device to another. It allows for persistent and quick data transfer.
Explore
- Explore serialization in other languages