Skip to content

Serialization and Deserialization

Shiva Karthick edited this page Oct 19, 2023 · 3 revisions

Guiding questions

  • I am trying to wrap my head around sending stuff over the wire. Regardless of whether I use binary encoding or use json, xml or proto buf - the data always has to be in bytes before they can be sent over the wire. Is that true? Yes
  • A mnemonic device I use to remember the difference is that "Serialization turns objects into serial numbers"
  • What is Serialization?
    • Serialization is the process of converting a data object into a byte stream.
  • What is byte stream
    • Byte stream is just a stream of binary data. Because only binary data can be stored or transported.
  • What is byte string vs byte stream
    • Sometime you see people use the word byte string as well. String encodings of bytes are called byte strings. Then it can explain what is JSON as below.

Concepts

  1. What’s the relationship between JSON and serialization
    • JSON is a string format representational of byte data. JSON is encoded in UTF-8. So while we see human readable strings, behind the scenes strings are encoded as bytes in UTF-8.
  • Difference between serialization and JSON https://stackoverflow.com/questions/3316762/what-is-deserialize-and-serialize-in-json
    • JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

    • When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format. Serialization can convert these complex objects into byte strings for such use. After the byte strings are transmitted, the receiver will have to recover the original object from the byte string. This is known as deserialization.

    • Say, you have an object:

    • {foo: [1, 4, 7, 10], bar: "baz"} serializing into JSON will convert it into a string:

    • '{"foo":[1,4,7,10],"bar":"baz"}' which can be stored or sent through wire to anywhere. The receiver can then deserialize this string to get back the original object. {foo: [1, 4, 7, 10], bar: "baz"}.

  1. What is serialization? https://stackoverflow.com/questions/633402/what-is-serialization
    • Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network. Deserialization is the reverse process: turning a stream of bytes into an object in memory.

    • Simply speaking Serialization is a process of converting an Object into stream of bytes so that it can be transferred over a network or stored in a persistent storage. Deserialization is the exact opposite - Fetch a stream of bytes from network or persistence storage and convert it back to the Object with the same state.

    • The thing to understand is how those stream of bytes are interpreted or manipulated so that we get the exact same Object/ same state. There are various ways to achieve that. Some of them are -

    • XML: Convert Object to XML, transfer it over a network or store it in a file/db. Retrieve it and convert it back to the object with same state. In Java we use JAXB(Java architecture for XML binding) library.(From java 6 it comes bundled with JDK).

    • JSON: Same can be done by converting the Object to JSON (JavaScript Object notation). Again there is GSON library that can be used for this. Or we can use the Serialization that is provided by the OOP language itself. For example, in Java you can serialize an Object my making it implement Serializable interface and writing to Object Stream.