serialize vs json in PHP

Mohammed Minuddin Peal
4 min readOct 1, 2018

--

Today I am discussing, serialize vs json_encode and their opposite unserialize vs json_decode, maybe this topic allreay very simple to someone, but there is many whom are not still clear about serialize and json_encode.

When we use serialize? use serialize when your application is specific to PHP, This is useful for storing or passing PHP values around without losing their type and structure, 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.

Look an example at below.

Suppose we have a PhpSerialize class and we are going to serialize an array using this class like as below.

class PhpSerialize implements Serializable {

private $arr;
public function __construct($arr) {
$this->arr = $arr;
}
public function serialize() { return serialize($this->arr); } public function unserialize($data) { return $this->arr = unserialize($data); }}
$arr['data']= ['PHP','JAVA','C','C++','MYSQL','ORACLE','VUE','BOL'=> true,'Int'=> 100];
$serialize = new PhpSerialize($arr);echo $serializedata = $serialize->serialize();Our probable output like as{i:0;s:3:"PHP";i:1;s:4:"JAVA";i:2;s:1:"C";i:3;s:3:"C++";i:4;s:5:"MYSQL";i:5;s:6:"ORACLE";i:6;s:3:"VUE";s:3:"BOL";b:1;s:3:"Int";i:100;}}

Main problem is here that many of us actually don’t understand the output of serialize, Although this is not topic of this tutorial but I am going to explain it here for your better realizations.Your attention please.

a for => Array
a:size (Here $arr['data'] has one element that's why a:1)
s for => String
s:size:value (Here $arr['data'] key which string size is 4 that's why s:4:"data" and here "data" is value)
i for => Integer
i:value
b for => boolean
b:value; (does not store "true" or "false", does store '1' or '0')
Null
N;
Object
O:strlen(object name):object name:object size:{s:strlen(property name):property name:property definition;(repeated per property)}

Hope, now you realize serialize data. Now if you want to unserialize the serialized data just do it like below.

$serialize->unserialize($serializedata);

then you will get back previous array as like as same of $arr[‘data’] like below.

Array ( [data] => Array ( [0] => PHP [1] => JAVA [2] => C [3] => C++ [4] => MYSQL [5] => ORACLE [6] => VUE [BOL] => 1 [Int] => 100 ) ).

Note: There is a disadvantage of serialize is that serialize data not working outside php.

On the other hand json_encode to Returns a JSON encoded string on success or FALSE on failure. And it is not PHP specific. Almost every and each languages have libraries to read/write json data. A JSON string is also easier to read write modify by hand than a serialized string. Here we are doing same thing like as serialize with json_encode, I am going to explain it here.

Suppose we have a JsonSerialize class and using this class we want to serialize an array into json, than our example will look like below.

class JsonSerialize implements JsonSerializable {

private $array;

public function __construct($arr) {
$this->array = $arr;
}
public function jsonSerialize() {
return json_encode($this->array);
}
}
$arr['data']= ['PHP','JAVA','C','C++','MYSQL','ORACLE','VUE','BOL'=> true,'Int'=> 100];$jsonserialize = new JsonSerialize($arr);$serialize = $jsonserialize->jsonSerialize();Here probable output like below{"data":{"0":"PHP","1":"JAVA","2":"C","3":"C++","4":"MYSQL","5":"ORACLE","6":"VUE","BOL":true,"Int":100}}

Here data looks more readable than before. Simple key value pairs in the object.Of course this json serialize data you can manipulate in any languages using there various types of libraries.So than you can say that json serialize data actually universal.

The beauty is almost same in serialize and json_encode but there is some difference between them, where serialize is PHP specific, but json is not PHP specific, it is universal and json is faster than serialize.

If you want to back your array , just do it like as below.

json_decode($serialize,true)

Then the output is will be an associative array, as before it was.

Array ( [data] => Array ( [0] => PHP [1] => JAVA [2] => C [3] => C++ [4] => MYSQL [5] => ORACLE [6] => VUE [BOL] => 1 [Int] => 100 ) )

Here I have passed TRUE along with $serialize (although this is optional) inside json_decode because When TRUE, returned objects will be converted into associative arrays otherwise json_decode will return an object.

$jsondecode = json_decode($serialize);

Output will be an object like below.

stdClass Object ( [data] => stdClass Object ( [0] => PHP [1] => JAVA [2] => C [3] => C++ [4] => MYSQL [5] => ORACLE [6] => VUE [BOL] => 1 [Int] => 100 ) )

So, in conclusion I hope now you realize it better when to use serialize and json, and their advantages and disadvantages.

Note: When we want to create an api for third party application, then json_encode and json_decode taking place.

--

--

Mohammed Minuddin Peal

Domain expert, Full Stack Developer, Java script, PHP, Laravel, Zend, Symfony, Vue Js, React Js, Node, Express, MongoDB, Oracle, MySql, PgSql