datasn.io
datasn.io

Reputation: 12867

Serialize or json in PHP?

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

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

Upvotes: 52

Views: 55728

Answers (6)

Max
Max

Reputation: 13334

Another advantage of json_encode over serialize is the size. I noticed that as I was trying to figure out why our memcache used memory was getting so big, and was trying to find ways to reduce is:

<?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";

Which 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've taken the most extreme example, as the shorter the array, the more important the overhead with serialize (relative to the initial object size, due to formatting which imposes a minimum number of characters no matter how small the content). Still from a production website I see serialized array that are 20% bigger than their json equivalent.

Upvotes: 8

Shozab H.
Shozab H.

Reputation: 575

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

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

Upvotes: 13

Vidda
Vidda

Reputation: 31

First, 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 is OK with both PHP AND javascript, maybe other languages.

To encode a PHP object, serialize is a better choice because of specificity of PHP Objects only instanciable with PHP.

To store datas, either store encoded datas in a file or use MySQL with standard format. It would be much easier to get your datas back. MySQL has great functions to get datas the way you'd like to get them without PHP treatment.

I've never made any test but i think that file storage is the best way to store your datas if system file sorting is enough to get back your files in alphabetical/numeral order. MySQL is to greedy for this kind of treatment and uses file system too...

Upvotes: 3

selfawaresoup
selfawaresoup

Reputation: 15832

If you data will never has to leave your PHP application, I recommend serialize() because it offers a lot of extra functionality like __sleep() and __wakeup() methods for your objects. It also restores objects as instances of the correct classes.

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

But storing a serialized objet into a database? Maybe you should think about that again. It can be real trouble later.

Upvotes: 3

Pascal MARTIN
Pascal MARTIN

Reputation: 401032

Main advantage of serialize : it's specific to PHP, which means it can represent PHP types, including instances of your own classes -- and you'll get your objects back, still instances of your classes, when unserializing your data.


Main advantage of json_encode : JSON is not specific to PHP : there are libraries to read/write it in several languages -- which means it's better if you want something that can be manipulated with another language than PHP.

A JSON string is also easier to read/write/modify by hand than a serialized one.

On the other hand, as JSON is not specific to PHP, it's not aware of the stuff that's specific to PHP -- like data-types.


As a couple of sidenotes :

  • Even if there is a small difference in speed between those two, it shouldn't matter much : you will probably not serialize/unserialize a lot of data
  • Are you sure this is the best way to store data in a database ?
    • You won't be able to do much queries on serialized strins, in a DB : you will not be able to use your data in where clauses, nor update it without the intervention of PHP...

Upvotes: 55

cletus
cletus

Reputation: 625097

Well firstly serializing an array or object and storing it in a database is typically a code smell. Sometimes people end up putting a comma separated list into a column and then get into all sorts of trouble when they later find out they need to query on it.

So think very carefully about that if this is that kind of situation.

As for the differences. PHP serialize is probably more compact but only usable with PHP. JSON is cross-platform and possibly slower to encode and decode (although I doubt meaningfully so).

Upvotes: 5

Related Questions