Serialize or json in PHP?

So I need to encode the array in PHP and save it as plain text in the MySQL database, my question is whether to use serialize () or json_encode ()? What are the advantages and disadvantages of each?

I think that any of them will do in this situation. But which one would you prefer and why? If this is for something other than an array?

+44
json php serialization
Apr 04 '10 at 14:35
source share
6 answers

The main benefit of serialize : it is specific to PHP, which means it can represent PHP types , including instances of your own classes, and you will return your objects, still instances of your classes, when you do not serialize your data.


The main advantage of json_encode : JSON is not specific to PHP: there are libraries for reading / writing in several languages, which means that it is better if you want to be able to control a different language than PHP.

A JSON string is also easier to read / write / modify manually than serialized.

On the other hand, since JSON is not specific to PHP, it does not know what is specific to PHP-like data types.


As a couple of side effects:

  • Even if there is a slight speed difference between the two, it does not make much difference: you probably are not serializing / not serializing a lot of data.
  • Are you sure this is the best way to store data in a database?
    • You cannot make many queries on serialized rows in the database: you cannot use your data in where clauses or update it without PHP intervention ...
+49
Apr 04 '10 at 14:39
source share

I did some analysis of Json Encoding vs Serialization in PHP. And I found that Json is best suited for simple and simple data like an array.

See the results of my experiments at https://www.shozab.com/php-serialization-vs-json-encoding-for-an-array/

+11
Nov 01 '12 at 11:18
source share

Another advantage of json_encode serialize is size. I noticed that, as I tried to understand why our memcache used memory is getting so large, and trying to find ways to reduce this:

 <?php $myarray = array(); $myarray["a"]="b"; $serialize=serialize($myarray); $json=json_encode($myarray); $serialize_size=strlen($serialize); $json_size=strlen($json); var_dump($serialize); var_dump($json); echo "Size of serialized array: $serialize_size\n"; echo "Size of json encoded array: $json_size\n"; echo "Serialize is " . round(($serialize_size-$json_size)/$serialize_size*100) . "% bigger\n"; 

What gives you:

 string(22) "a:1:{s:1:"a";s:1:"b";}" string(9) "{"a":"b"}" Size of serialized array: 22 Size of json encoded array: 9 Serialize is 59% bigger 

Obviously, I used the most extreme example, because the shorter the array, the more important the overhead with serialization (relative to the original size of the object, due to formatting that imposes a minimum number of characters no matter how small the content is). Even from the production website, I see a serialized array that is 20% larger than their json equivalent.

+6
Apr 13 2018-12-12T00:
source share

Well, firstly, serializing an array or object and storing it in a database is usually a smell of code. Sometimes people insert a comma-separated list into a column, and then get into all kinds of problems when they later find out that they need to request it.

Therefore, think carefully about this if this is the situation.

As for the differences. PHP serialization is probably more compact, but only applicable to PHP. JSON is cross-platform and perhaps slower for encoding and decoding (although I doubt it is).

+5
Apr 04 '10 at 14:39
source share

Firstly, thanks to Shozab Hasan and user359650 for these tests. I was wondering which choice was the best, and now I know:

To encode a simple array, JSON, which works with both PHP and javascript, possibly with other languages.

To encode a PHP object, serialization is the best choice due to the specifics of PHP objects, which may not be compatible with PHP.

To store data, either store the encoded data in a file, or use MySQL with a standard format. It would be much easier to get your data back. MySQL has great features for retrieving data as you would like to retrieve it without PHP processing.

I have never done any tests, but I believe that storing files is the best way to save your data if sorting system files is enough to return your files in alphabetical order. MySQL craves for this kind of cure and uses the file system too ...

+3
Nov 04 '12 at 23:28
source share

If you never have to leave your PHP application, I recommend serialize () because it offers many additional features, such as the __sleep () and __wakeup () methods for your objects. It also restores objects as instances of the correct classes.

If you pass serialized data to another application, you must use JSON or XML for compatibility.

But saving a serialized object to a database? Perhaps you should think about it again. This may be a real problem later.

+2
Apr 04 '10 at 2:55 a.m.
source share



All Articles