This post will discuss how to serialize and deserialize objects in JavaScript.

Serializing and deserializing objects in JavaScript is a common task that involves converting an object into a string format that can be stored or transmitted, and then convert the string back to the original object. There are several ways to do this, depending on the complexity and structure of the object, as well as the desired format of the output. Here are some of the most popular functions:

1. Using JSON functions

One of the most common and standard formats for serialization and deserialization is JSON (JavaScript Object Notation), which is a lightweight and human-readable data format that can represent objects, arrays, strings, numbers, booleans, and null values. To serialize an object to JSON, we can use the built-in JSON.stringify() function, which takes an object as a parameter and returns a JSON string. For instance:

Download  Run Code

 
To deserialize a JSON string to an object, we can use the built-in JSON.parse() function, which takes a JSON string as a parameter and returns an object. For instance:

Download  Run Code

 
However, JSON has some limitations and cannot represent all types of JavaScript values, such as functions, dates, symbols, circular references, or custom types. For instance:

Download  Run Code

 
If we need to serialize and deserialize such values, we may need to use custom functions that can handle them. For instance:

1. To serialize and deserialize functions, we can use the Function.prototype.toString() function to convert a function to a string, and the Function() constructor to create a function from a string. For instance:

Download  Run Code

 
2. To serialize and deserialize dates, we can use the Date.prototype.toISOString() function to convert a date to a string in ISO format, and the Date() constructor to create a date from a string. For instance:

Download  Run Code

 
To overcome these JSON limitations, we can use the second argument of JSON.stringify() and the second argument of JSON.parse() to customize the serialization and deserialization process. These arguments are functions that can modify the values before or after the conversion. For instance:

Download  Run Code

2. Using jsan or flatted library

To serialize and deserialize objects with circular references, we can use a library such as jsan or flattted, which can handle circular dependencies by using placeholders and references. These libraries offer various functions and options for converting objects to and from strings. We can install these libraries using npm or include them in our HTML file using a script tag. Here’s an example using the jsan library:

Download Code

 
Alternatively, we can use the flatted library in the same way to handle circular references and other complex cases. It provides the flatted.stringify() function to serialize an object to a flatted string, and flatted.parse() function to deserialize a flatted string to an object, which will restore the original structure and values of the object. Here’s an example:

Download  Run Code

That’s all about serializing and deserializing objects in JavaScript.