JSON.stringify versus serialization

11,798

Solution 1

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

JSON is one form of text-based serialization format and is currently very popular due to it's simplicity. It's not the only one though. Other popular formats include XML and CSV.

Due to its popularity and its origin as javascript object literal syntax ES5 introduced JSON.stringify() to generate a JSON string from an object. Previously you had to use libraries or write a recursive descent parser to do the job.

So, is JSON.stringify() enough for serialization? Yes, if the output format you want is JSON. No, if you want other output formats such as 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 can't encode circular references. Most other serialization formats have this limitation as well but since JSON looks like javascript syntax some people assume it can do what javascript object literals can. It can't.

So the relationship between "JSON" and "serialization" is like the relationship between "Toyota Prius" and "car". JSON.stringify() is simply a function that generates JSON strings so I guess that would make it a Toyota factory.

Solution 2

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

Of course, you can serialise any way you want, 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 that JavaScript object literals do, though it is slightly less flexible. Nevertheless, if you can represent normal data in JavaScript then JSON is a good match.

The most significant feature is that, since it represents objects as well as arrays, it can represent fairly complex & hierarchical data.

For one reason or another, JSON has more-or-less supplanted XML as the preferred serialisation for sending data between the server and browser. It is so useful that many languages include their own JSON functions (PHP, for example, has the better named json_encode & json_decode functions), as do some modern Databases. I myself have found it convenient to use JSON functions to store a more complex data structure in a single field of a database without JavaScript anywhere in sight).

The short answer is yes, for the most part it is a sufficient step to serializing most data (non-binary). It is not, however, necessary as there are alternatives.

Serializing binary data, on the other hand, now that’s another story …

Share:
11,798

Related videos on Youtube

Alexander Mills
Author by

Alexander Mills

Dev, Devops, soccer coach. https://www.github.com/oresoftware

Updated on September 21, 2022

Comments

  • Alexander Mills
    Alexander Mills over 1 year

    Is JSON.stringify( ) equivalent to serialization or effectively serialization or is it just a necessary step towards serialization?

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

    • Manngo
      Manngo about 7 years
      I might add that JSON is designed to represent a subset of a JavaScript object, and, apart from its own peculiar notation (not as flexible as a JavaScript object literal), does not include function properties, AKA methods.
  • Admin
    Admin almost 4 years
    well, strings are serialized data, so yeah