JSON (JavaScript Object Notation) serialization converts a JavaScript object into a JSON string. It is a good way of exchanging information between server and client or between servers.
What is JSON :
JSON is a lightweight data-exchange formater(string), that is easy for humans to read and write and for machines to parse and generate.
In JavaScript, JSON serialization is the process of converting a JavaScript object into a JSON string by using the JSON.stringify() method. It serializes JavaScript objects so that they can be easily transmitted over a network or stored in a file.
Basic example of.. JSON.stringify() to serialize a JavaScript object:
Replace function : JSON.stringify() method accepts an optional second parameter, a replacer function that filters out values, transforms values, or adds values. If you return a value different from the original value, it is replaced.
It is a replacer function, which filters out values, transforms values, or adds values.
JSON.stringify() method can only convert JSON-safe data types, such as numbers, strings, booleans, arrays, and objects. All others like functions and undefined values when encountered are converted to null in the final JSON string.
Serialization and deSerialization
Serialization:
writing the state of an objed into a bytes stream.
It revenge operation of Serialization of called deseridization codone byte-stream of convealed to an Object.
• Sestilation • Despacialization
Write Object() Read object()
Limitations of JSON serialization
JSON is simple, lightweight, and easy to read and write, it also has some limitations…
Limited data types: JSON only supports a limited set of data types, including strings, numbers, booleans, arrays, and objects.
It does not support more complex data types, such as binary data, date and time, and null values.
No support for comments: JSON does not support comments, which can make it harder for developers to understand the meaning of certain sections of JSON code.
Limited support for object-oriented concepts: JSON is based on a subset of JavaScript and does not support some object-oriented concepts, such as inheritance.
No support for circular references: JSON.stringify() method will throw an error if the object has circular references.
A circular reference occurs when an object references itself directly or indirectly.
Limited support for large numbers: JSON does not support large numbers with decimal places, which can lead to a loss of precision.
Handling limitations of JSON.stringify():
JSON.stringify() Limitation
Functions are Ignored by Default:
- Without a replacer function, JSON.stringify(obj) would return {} (an empty object) since functions are ignored.
Functions Are Not Executable When Converted to Strings:
- When converted to a string, the function loses its behavior and cannot be executed when parsed back.
Storing Functions as Strings and Evaluating Later:
- You can store the function as a string and later use eval() (not recommended for security reasons).
Using toJSON Method in Objects:
Define a toJSON method inside the object:
Using a Custom Serialization Library (like circular-JSON) .These libraries help serialize more complex objects, including functions and circular references.
Conclusion
JSON.stringify() is a powerful method in JavaScript that allows developers to convert JavaScript objects into JSON strings, which can then be transmitted over a network or stored in a file. It accepts an optional replacer function that filters out values, transforms values, or adds values. It also accepts an optional space argument that formats the final JSON string with proper indentation to make it more human-readable. In addition to JSON.stringify(), JavaScript also provides the JSON.parse() method to deserialize JSON strings into JavaScript objects.
Let’s Enjoy this Tec.Knowledge by Ankit