Preferred Method to Store PHP Arrays (Json_Encode VS Serialize)

Preferred method to store PHP arrays (json_encode vs serialize)

Depends on your priorities.

If performance is your absolute driving characteristic, then by all means use the fastest one. Just make sure you have a full understanding of the differences before you make a choice

  • Unlike serialize() you need to add extra parameter to keep UTF-8 characters untouched: json_encode($array, JSON_UNESCAPED_UNICODE) (otherwise it converts UTF-8 characters to Unicode escape sequences).
  • JSON will have no memory of what the object's original class was (they are always restored as instances of stdClass).
  • You can't leverage __sleep() and __wakeup() with JSON
  • By default, only public properties are serialized with JSON. (in PHP>=5.4 you can implement JsonSerializable to change this behavior).
  • JSON is more portable

And there's probably a few other differences I can't think of at the moment.

A simple speed test to compare the two

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

// Make a big, honkin test array
// You may need to adjust this depth to avoid memory limit errors
$testArray = fillArray(0, 5);

// Time json encoding
$start = microtime(true);
json_encode($testArray);
$jsonTime = microtime(true) - $start;
echo "JSON encoded in $jsonTime seconds\n";

// Time serialization
$start = microtime(true);
serialize($testArray);
$serializeTime = microtime(true) - $start;
echo "PHP serialized in $serializeTime seconds\n";

// Compare them
if ($jsonTime < $serializeTime) {
printf("json_encode() was roughly %01.2f%% faster than serialize()\n", ($serializeTime / $jsonTime - 1) * 100);
}
else if ($serializeTime < $jsonTime ) {
printf("serialize() was roughly %01.2f%% faster than json_encode()\n", ($jsonTime / $serializeTime - 1) * 100);
} else {
echo "Impossible!\n";
}

function fillArray( $depth, $max ) {
static $seed;
if (is_null($seed)) {
$seed = array('a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10);
}
if ($depth < $max) {
$node = array();
foreach ($seed as $key) {
$node[$key] = fillArray($depth + 1, $max);
}
return $node;
}
return 'empty';
}

PHP: json_encode vs serialize for storing in a MySQL database?

Found this in the PHP docs...

function mb_unserialize($serial_str) { 
$out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );
return unserialize($out);
}

I don't quite understand it, but it worked to unserialize the data that I couldn't unserialize before. Moved to JSON now, i'll report in a couple of weeks whether this solved the problem of randomly getting some records "corrupted"

Serialize or json in PHP?

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...

JSON vs. Serialized Array in database

  1. 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.
  2. 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.

The choice is yours.

In what scenarios is it better to use json_encode() than to use serialize()?

If you're transferring data between one application and another, it's usually almost always better to use JSON encoded data rather than PHP serialized data as the later is a format specific to PHP and not as portable as JSON.

Even in a situation where both the server and client are both PHP-based, it behooves you to use a portable format like JSON to allow the creation of new clients in the future without having to change response format from the server.

I haven't done any benchmarking of these two myself, but if you're finding that json encoding is faster than serialization, than all the more reason to use it.

Also, I prefer JSON encoded data as it is easier to read than serialized data and can quickly be thrown into firebug to be visualized.

Is the PHP serialize a must if I want to store an object or aray to mySQL?

This is a question without a single good answer, very opinion-based. A couple things to keep in mind:

Is it useful to keep raw data, besides the data you're going to store in your database columns?

How are you going to use the data? Generally, you would store each data in its own database table column. If you only keep the original data "just in case I need it later", you might as well store it in a single column in serialized form.

How are you going to store it? I personally prefer NOT to use the PHP serialize format, but rather JSON instead. This is because:

  1. JSON is a format that can be decoded by languages other than PHP out of the box, which makes it more portable to other systems.
  2. Certain database types and versions have dedicated support for JSON in the form of JSON data column, which allows for faster processing.


Related Topics



Leave a reply



Submit