JSON.stringify versus serialization

Is JSON.stringify( ) equivalent of serialization or efficient serialization, or is it just a necessary step towards serialization?

In other words, is JSON.stringify( ) sufficient, but not necessary for serialization? Or is it necessary, but not enough? Or is it neither necessary nor sufficient to serialize JavaScript objects?

+13
json serialization
source share
3 answers

Serialization is the act of converting data to a format that can be written to disk or transferred over the network (or written on paper, if that's what you want). Usually serialization converts objects to text, but this is not necessary, as there are several serialization formats, such as bittorrent bencoding and the old / ancient standard asn.1 binary formats.

JSON is a form of text serialization format and is currently very popular due to its simplicity. This is not the only one. Other popular formats include XML and CSV .

Due to its popularity and its origin, JSON.stringify() is introduced as the text syntax of javascript ES5 object objects to generate a JSON string from the object. Previously, you had to use libraries or write a recursive parser to complete a task.

So is JSON.stringify() enough for serialization? Yes, if the desired output format is JSON. Not if you want to use other output formats like XML or CSV or bencode.

There are limitations to the JSON format. One limitation is that JSON cannot encode functions, so JSON.stringify() ignores functions / methods when serializing. JSON also cannot encode circular references. Most other serialization formats also have this limitation, but since JSON is similar to javascript syntax, some people assume that it can do what javascript object literals can use. He can not.

Thus, the relationship between JSON and serialization is similar to the relationship between Toyota Prius and car. JSON.stringify() is just a function that generates JSON strings, so I guess Toyota factory will do it.

+20
source share

An old question, but the following information may be useful for posterity.

Of course, you can serialize in any way, including any number of custom methods, but JSON has become an increasingly popular method.

The most obvious benefit of JSON is that it represents objects in the same way as JavaScript object literals, although a little less flexible. However, if you can represent normal data in JavaScript, then JSON is a good match.

The most important feature is that, since it represents objects as well as arrays, it can represent rather complex and hierarchical data.

For one reason or another, JSON has more or less extruded XML as the preferred serialization for sending data between the server and the browser. It is so useful that many languages โ€‹โ€‹include their own JSON functions (e.g. PHP, has better functions called json_encode and json_decode ), as well as some modern databases. I myself found it convenient to use JSON functions to store a more complex data structure in one database field without JavaScript anywhere).

The short answer is yes, for the most part this is a sufficient step to serialize most of the data (not binary). However, this is not necessary as there are alternatives.

Serializing binary data, on the other hand, is now a different story ...

+4
source share

The short answer is ... Serialization means the same as Stringify, IMHO.

0
source share

All Articles