Pro JSON:
- The JSON data can be used by many different languages, not just PHP
- JSON data is human readable and writable.
- It takes up less space
- It is faster to encode JSON than to serialize
Pro Serialized Array:
- It is faster do unserialize than to JSON decode
As the comments indicate, JSON takes up less space than a serialize array. I also checked whether JSON or Serializing is faster, and surprisingly, it is faster to JSON encode than to Serialize. It is faster to unserialize than to JSON decode though.
This is the script I used to test:
<?php
function runTime(){
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
return $mtime;
}
?>
<pre>
<?php
$start = runTime();
$ser;
for($i=0; $i<1000; $i++){
$a = array(a => 1, x => 10);
$ser = serialize($a);
}
$total = runTime() - $start;
echo "Serializing 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$json;
for($i=0; $i<1000; $i++){
$a = array(a => 1, x => 10);
$json = json_encode($a);
}
$total = runTime() - $start;
echo "JSON encoding 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$ser;
for($i=0; $i<1000; $i++){
$a = unserialize($ser);
}
$total = runTime() - $start;
echo "Unserializing 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$json;
for($i=0; $i<1000; $i++){
$a = json_decode($json);
}
$total = runTime() - $start;
echo "JSON decoding 1000 times took \t$total seconds";
?>
</pre>