Understanding Serialization and Deserialization in JavaScript with an example of Teleporting a Human
As developers, we often hear about serialization and Deserialization, but why do these terms really mean ? 🤔
Lets understand this with an example of Teleporting a Human. Now assume that teleportation is possible in real life , then one possible approach is Convert the human into a binary form ,and then send him through the internet .In web development, we do something similar by teleporting objects (data structures) in JavaScript. We convert them into JSON or other formats, send them through a network, and once they reach their destination, we convert them back to their original objects. Let's explore the concept of serialization and deserialization in JavaScript using the analogy of human teleportation.
Now the process of converting a User into Binary(storable/sharable format) is Called as Serialization and again the process of converting back to normal form is called Deserialization.
Now lets understand this concept with a example in JavaScript in detail.
Serialization
Serialization is the process of converting a data structure, such as JavaScript object or arrays into a format that can be easily stored.
In JavaScript the most common serialization format is JSON(JavaScript Object Notation) . The JSON format is easy for both storing and sharing.
Example of serialization in JavaScript -
Deserialization
Deserialization is the opposite process of serialization. It converts data from serialized format into its original data structure such as JavaScript Object or array, to make data usable and accessible in the application.
Example of Deserialization in JavaScript -
Now JSON is not only the format for serialization. There are other methods also present such BSON and ESJON.
Why we use Serialization and Deserialization?
Now you know what is But you might think why do we need them . lets understand this with real life example
when you log in to a website, and your session details (like username and preferences) are stored in local Storage or sent to a backend server.
Serialization: Your user object ({username: ”sourav”, isloggedIn:”true”}) is converted into a JSON string using JSON.stringify() and stored in local Storage or sent via an API.(through internet).
Deserialization: When you revisit the site, the JSON string is retrieved, parsed with JSON.parse(), and used to restore your session without requiring another login.
Conclusion
In summary, Serialization is the process of converting data into a compact and structured format suitable for storage and transmission, while Deserialization is the process of converting the serialized data back into its original format
If you found this helpful, like it ❤ and feel free to share your thoughts.🚀.