57

First of all, I couldn't get clear definition of it from WikiPedia or even from serialize function in the PHP manual. I need to know some cases where we need the term serialization and how things are going without it? In other words, Where you need serialization and without it your code will be missing some important feature.

TechGeek49
  • 476
  • 1
  • 7
  • 24
  • What do you mean by *we need the **term** serialization*? Serialization is a technique for describing a data structure with information about the structure itself embedded in the data. JSON is a lightweight type of serialization, e.g., `{prop:{prop:1}}`. Transfer that to another computer and minimally you can then work with that object's properties with the same basic relationship of `prop.prop`. – Jared Farrish Aug 05 '12 at 16:17
  • 1
    I mean when we need it? simple! –  Aug 05 '12 at 16:20
  • When you need to take a data structure across some boundary in which you then need to have that same structure recoverable at some later point. You're minimally describing the data in a more verbose way than just any specific, singular key-variable mapping. – Jared Farrish Aug 05 '12 at 16:21
  • @JaredFarrish so do you mean that I can serialize data in my own way and the word serialize came from making series. i.e. could we able to regard arrays as serialized data? –  Aug 05 '12 at 16:23
  • If you want to, sure I suppose you could make your own serialization method, but it's really not necessary in most cases. There's plenty of ways to do this, `json_encode` being one that's lightweight and handles arrays already. – Jared Farrish Aug 05 '12 at 16:30

5 Answers5

115

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 (e.g. using a schema like WSDL or XHTML)
  • 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)
Till Helge
  • 9,253
  • 2
  • 40
  • 56
Dovydas Navickas
  • 3,533
  • 1
  • 32
  • 48
  • 2
    How do you send in-memory object references to other computers or save to disk without serialization? In my mind it's not just harder, but impossible. – Esailija Aug 05 '12 at 16:32
  • 5
    In many cases, I think a large part of the point of serialization it that it is *language-agnostic*. You're not picking a language when you choose a serialization method, you're picking representation that is supported *between* two points, which could be the same or different languages interpreting it from or into a language-native structure. – Jared Farrish Aug 05 '12 at 16:37
  • *XML which has predecessors like SOAP, WSDL* - This statement is wrong. SOAP and WSDL are not predecessors to XML; WSDL *is* XML, and SOAP is a means to describe and call objects between systems. XML has been around for much longer than either of these, as well, and is a basic syntax for data description. – Jared Farrish Aug 05 '12 at 16:39
  • 1
    To Esailija: sometimes it is not about how hard it is, but how efficient. For example, if you need to send only Name, Surname and Credentials of Person class object, until you send it once in a week - you won't feel it. But should you send it once a second... And what about security? If you serialize whole object and send it wirelessly without encryption, people may scan it and get Social Security numbers, bank info and so on... So in these kind of situations I do believe that serialization is not an option. – Dovydas Navickas Aug 05 '12 at 16:42
  • To Jared Farrish: my bad. There should have been word successor, not predecessor. Edited already :) – Dovydas Navickas Aug 05 '12 at 16:43
  • @DovydasNavickas: but you still have to serialize it, into some stream of Bits and Bytes. The serialisation format is of course dependent of performance requirements, and to add a security layer you will need to encrypt the communication. Nonetheless this is still called serialisation. – Bergi Aug 05 '12 at 18:18
  • Technically it is possible to write everything in bytes right away, but, of course, pretty much no one does that. And I see what you're saying and yes, even thought we do not serialize objects themselves, we do convert parts of them into bytes. At least in situation I described earlier. But the question was "Are there any situations, where not serializing an object makes solution impossible?" (I paraphrased that, but Idea is the same). And the answer is yes, but it is more difficult. And, I do believe, that's what I said :) – Dovydas Navickas Aug 06 '12 at 15:05
  • Good Overview. Thanks! To supplement, I found this blog post that quickly shows the structure of PHP serialized arrays. From their example, it's easy to "read" (and edit) serialized items in a WordPress database ;-) https://wp-staging.com/serialized-data-wordpress-important/ It looks like `a` stands for array, followed by its number of items. Inside that array's braces, the `s` stands for string and indicates the number of characters in the following string. Strings alternate between the key name, and its value, for each item in the array. Any "value" can also be a serialized array. – SherylHohman Jun 15 '19 at 16:29
23

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.

Wolverine
  • 1,712
  • 1
  • 15
  • 18
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This means that I could able to serialize data in any way I want but it should listed as a series of blocks, so we could call arrays as serialized data?! –  Aug 05 '12 at 16:28
  • 2
    No exactly. Arrays are not serialized data. Arrays live in memory. They are objects. Once you serialize them using some format they are converted to some representation of bytes that could be saved or sent over the wire. The exact representation will depend on the serialization format you choose. – Darin Dimitrov Aug 05 '12 at 16:30
  • 1
    @DarinDimitrov Thank you for your comment, made me have a look at your answer and learn some things ;-) – aaaaaa Aug 05 '12 at 16:36
  • 7
    This should be the accepted answer, the other one has unresolved controversy :P – Esailija Aug 05 '12 at 16:36
  • It is good answer too, but I feel that the answer I chosen it more clear and direct. –  Aug 05 '12 at 16:43
  • I've always wondered if deep down, within the common language runtime, if that was based on serialization. I can't remember what the low-level "language" is, but that was what struck me when I read an article about it some time ago. – Jared Farrish Aug 05 '12 at 16:49
  • It's really a nice explanation. – Wolverine Dec 23 '16 at 15:15
6

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.

Mahn
  • 16,261
  • 16
  • 62
  • 78
3

I need to know some cases we need the term serialization and how things are going without it?

Serialization can become handy if you need to store complete structures (like an invoice with all associated data like customer address, sender address, product positions, tax caclulcations etc) that are only valid at a certain point in time.

All these data will change in the future, new tax regulations might come, the address of a customer changes, products go out of life. But still the invoice needs to be valid and stored.

This is possible with serialization. Like a snapshot. The object in memory are serialized into a (often like in PHP) binary form that can be just stored. It can be brought back to live later on (and in a different context). Like with this invoice example: In ten years, the data can still be read and the invoice object is the same as it was ten years earlier.

In other word, Where you must need serialization and without it your code will be missing some important feature.

That was one example. It's not that you always needs that, but if things become more complex, serialization can be helpful.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • I've done the same thing with a training exam result. I store the questions and answers in the row with the record, so that in case the question is later updated, the content of the exam as it was taken is still kept. Of course it could have been as descriptive and engineered more completely, but it really wasn't necessary, so serializing it was a perfect compromise, and has been for 5+ years. – Jared Farrish Aug 05 '12 at 16:46
1

Since you've tagged it with javascript, one kind of serialization could be form serialization.

Here are the references for the jQuery and prototype.JS equivalents.

What they basically do is serialize form input values into comma-separated name-value pairs.

So considering an actual usage..

$.ajax({
   url : 'insert.php?a=10,b=15' //values serialized via .serialize()
   type: 'GET'
});

And you would probably do $GET["a"] to retrieve those values, I'm not familiar with PHP though.

Robin Maben
  • 22,194
  • 16
  • 64
  • 99
  • I don't know if that's strictly *serialization*. – Jared Farrish Aug 05 '12 at 16:19
  • @Said Bakr: More so for usage than language I would say. The definition is always the same. It all comes down to passing data from A to B such that it can be retrieved back and de-constructed. – Robin Maben Aug 05 '12 at 16:19