serialize vs json in PHP

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.

Let’s look a example at below.

Suppose we have a data array and we are going to serialize the array like as below

$arr['data']= ['PHP','JAVA','C','C++','MYSQL','ORACLE','VUE','BOL'=> true,'Int'=> 100];
$serialize = serialize($arr);

Our probable output like as

a:1:{s:4:"data";a:9:{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: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.

$unserialize = unserialize($serialize) 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 outsize 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 an array $arr[‘data’], and want to serialize this array into json, our example will look like below.

$arr['data']= ['PHP','JAVA','C','C++','MYSQL','ORACLE','VUE','BOL'=> true,'Int'=> 100];

$jsonencode = json_encode($arr);

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

$jsondecode = json_decode($jsonencode,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 $jsonencode (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($jsonencode);

Output will be 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, what’s their advantages and disadvantages.

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

 


Posted

in

by

Tags:

Comments

Leave a comment