What is data serialization? What is data serialization? javascript javascript

What is data serialization?


What is serialization?

Serialization encodes objects into another format.
For example you have an array in PHP like this:

$array = array("a" => 1, "b" => 2, "c" => array("a" => 1, "b" => 2));

And then you want to store it in file or send to other application.

There are several format choices, but the idea is the same:The array has to be encoded (or you could say "translated"), into text or bytes, that can be written to a file or sent via the network.
For example, in PHP, if you:

$data = serialize($array);

you will get this:

a:3:{s:1:"a";i:1;s:1:"b";i:2;s:1:"c";a:2:{s:1:"a";i:1;s:1:"b";i:2;}}

This is PHP's particular serializing format that PHP understands, and it works vice versa, so you are able to use it to deserialize objects.
For example, you stored a serialized array in a file, and you want it back in your code as an array:

$array = unserialize($data);

But you could choose a different serialization format, for example, JSON:

$json = json_encode($array);

will give you this:

{"a":1,"b":2,"c":{"a":1,"b":2}}

The result is not only easily saved, read by human eye, or sent via network, but is also understandable by almost every other language (JavaScript, Java, C#, C++, ...)

Conclusion
Serialization translate objects to another format, in case you want to store or share data.

Are there any situations, where you cannot do anything, but serialize it?

No. But serialization usually makes things easier.

Are JSON and PHP format the only possible formats?
No, no, no and one more time no. There are plenty of formats.

  • XML which has successors like SOAP, WSDL, etc. (those have particular purpose)
  • Bytes, Protobuf, etc.
  • Yaml
  • ...
  • ...
  • Your own formats (you can create your own format for serialization and use it, but that is a big thing to do and is not worth it, most of the time)


Serialization is the process of converting some in-memory object to another format that could be used to either store in a file or sent over the network. Deserialization is the inverse process meaning the actual object instance is restored from the given serialized representation of the object. This is very useful when communicating between various systems.

The serialization format could be either interoperable or non-interoperable. Interoperable formats (such as JSON, XML, ...) allow for serializing some object using a given platform and deserializing it using a different platform. For example with JSON you could use javascript to serialize the object and send it over the network to a PHP script that will deserialize the object and use it.

The serialize() PHP function uses an non-interoperable format. This means that only PHP could be used to both serialize and deserialize the object back.

You could use the json_encode and json_decode() functions in order to serialize/deserialize PHP objects using the JSON interoperable format.


Serialization is the process of turning data (e.g. variables) into a representation such as a string, that can easily be written and read back from for example a file or the database.

Use cases? There are many, but generally it revolves around the idea of taking a complex, nested array or object and turning it into a simple string that can be saved and read later to retrieve the same structure. For example, provided you have in php:

$blub = array();$blub['a'] = 1;$blub['a']['b'] = 4;$blub['b'] = 27;$blub['b']['b'] = 46;

Instead of going through every array member individually and writing it one could just:

$dataString = serialize($blub);

And the serialized array is ready to be written anywhere as a simple string, in such a way that retrieving this string again and doing unserialize() over it gets you the exact same array structure you had before. Yes, it's really that simple.