Skip to main content

Command Palette

Search for a command to run...

Serialization and Deserialization

Updated
•4 min read•View as Markdown
Serialization and Deserialization

Lets us get an overview first

Problem

Suppose we have a client who operates on javascript. Data is sent on an server which operates on some other language such as cpp,c,python or java. So they will not understand standard JS objects. And in turn JS will not understand responses sent by the other languages in their respective syntax. So it will create multiple conflicts

Solution

So a standard is made in which data is sent and received over the internet by different servers and clients to avoid these conflicts.

There are 2 main standards

1. Text-based standards

  • JSON (JavaScript Object Notation}

  • XML (Extensible Markup Language)

  • YAML

2. Binary-based standards

  • Protocol Buffers (Protobuf)

  • MessagePack

  • Apache avro

Out of these the most commonly used format is JSON format. So we will use JSON format to explain the different processes in this article.

Lets understand serialization in detail

Serialization

It is the process of converting and object into a format (JSON) that can be stored and transmitted over the internet.

Example : Suppose we have a JS object

const user = {
    name : 'Abhinab',
    age  : 21,
    isDev : true,
    skills : ['JS','React','NodeJS']
}

Now this user object cannot be sent to internet directly.

So what we do is convert it into JSON format by using

JSON.stringify(user)

Now the object becomes :

{
    "name": "Abhinab",
    "age"  : 21,
    "isDev" : true,
    "skills" : ["JS","React","NodeJS"]
}

Now this JSON data is sent over internet.

This is SERIALIZATION.

So , the flow for serialization is

Object
   ↓

Serialize

   ↓

 String

Now lets understand deserialization.

Deserialization

It is the process of converting incoming data from internet which is in standard format (JSON) into a language specific format.

Lets first understand the flow

String
  ↓

Deserialize

  ↓

Object

Suppose a client is sending an object which in JSON format looks like

{"id":1,"name":"Abhinab"}

Suppose the server is an NodeJS server

To convert this JSON format into an object we do

JSON.parse(data)

It becomes

{id:1,name:"Abhinab"}

So this is basically all there is to serialization and deserialization.

Complete Flow

Now let us consider a JS client and an express server operating on HTTP.

Lets understand the whole process with an example.

Suppose we are sending a post request to the server. So the request is sent like

fetch("/menu", {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        name: "Coffee",
        price: 200
    })
});

JSON.stringify() coverts the object into string and it is sent over to the server over the '/menu' route.

Over the internet it will lt will look like

POST /menu HTTP/1.1

Content-Type: application/json

{"name":"Coffee","price":200}

Now on reaching the server , express has a middleware

app.use(express.json())

This performs deserialization.

express.json() is middleware that parses (deserializes) an incoming JSON request body into a JavaScript value and makes it available through req.body.

So if we try to console.log the the body using the code snippet

app.post("/menu", (req, res) => {
    console.log(req.body);
});

Output is

{
   name: "Coffee",
   price: 200
}

Again when sending response back to the client , it is sent in JSON format.

Suppose response is

res.json({
    message: "Success",
    id: 10
});

Here again express automatically converts it into string by

JSON.stringify({
    message: "Success",
    id: 10
});

and it sends back

Content-Type: application/json

{"message":"Success","id":10}

On reaching browser client or browser again the response is converted into JS object using JSON.parse() for further operation.

This serialization → transmission → deserialization cycle is one of the core patterns we encounter in backend development, APIs, databases, caching systems, message queues, and many network protocols.