JSON vs. Serialized Array in database [closed]
- JSON encode() & decode()
- PHP Version >= 5.0.0
- Nesting Limit of 20.
- PHP Version >= 5.2.3
- Nesting Limit of 128.
- PHP Version >= 5.3.0
- Nesting Limit of 512.
- Small footprint vs PHP's serialize'd string.
- PHP Version >= 5.0.0
-
serialize() & unserialize()
- PHP Version >= 4.0.0
- Methods are not lost on PHP Datatype Object.
- __wakeup() magic method called on any object being unserialize. (VERY POWERFUL)
- It has been noted that it is some times best the base64 encode strings put into the database, and base64 decode strings taken out of the database with this function, as there are some issues with the handling of some white space characters.
- PHP Version >= 4.0.0
The choice is yours.
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>
Portability: Winner JSON. JSON is supported on a wider variety of platforms, while PHP de-serialization is only supported (as far as I know) by PHP. While it's possible to parse either format in any language, JSON has more pre-built libraries.
Future Proof: Winner JSON. JSON is a "standard", in the sense that Javascript is a standard, and isn't likely to change anytime in the future. The PHP group has made no promises about the future of the serialization format, and while it's unlikely to change in the future, the fact that a single group controls the format means you may end up with future data that's unreadable.
Fidelity: Winner PHP. PHP serialization will allow you to store data with native PHP data types, including objects defined by custom classes. JSON will only allow you to store generic primitive types, lists of primitive types ("arrays") and key/value pair Objects. PHP Serialization may provide some advantages here if you're developing a PHP application.
File Size: JSON has a slight win here, as PHP's current serialization format is more verbose (as it's storing more information).
Performance: Who knows, it depends, profile.
Conclusion: Go with JSON unless you have a compelling reason to use PHP Serialization .