Comparing json_encode and serialize in PHP

Admin   PHP   662  2021-01-19 14:15:01

Both functions json_encode and serialize in PHP can be used to convert an  object or array value  to a string type. However, the conversion logic of these two functions is not the same. Also, with the same value used for conversion, the last received string has completely different formats.

json_encode-and-serialize-in-PHP

Return value

To compare the difference between the return value json_encode and serialize let's look at a specific example below:

$obj = new stdClass();

$obj->firstProperty = "First value";

$obj->secondProperty = "Second value";

Next, we will use this json_encode to convert the $obj above object to a string type :

echo json_encode($obj);

Now when running the above code, you will receive the following results:

{"firstProperty":"First value","secondProperty":"Second value"}

Contrast when using serialize:

echo serialize($obj);

You will receive the following results:

O:8:"stdClass":2:{s:13:"firstProperty";s:11:"First value";s:14:"secondProperty";s:12:"Second value";}

Through the example above you can see that the function json_encode returns the string with less characters than serialize. In addition, the string returned by json_encode is structured in a way that is more human-friendly than using it serialize.

The advice here is when you need to convert a large number of items object and saving space (of RAM or hard drive ...) is the deciding factor you should use json_encode.

Visibility of Properties (Public, Private, Protected)

When comparing visibility between the two json_encode and serialize we will compare the ability to convert the value of the property public and private in the object. To understand this let's consider the following example:

class Obj {

    private $firstProperty = "First";

    public $secondProperty = "Second"; 

}


$obj = new Obj();

Above we define a class Obj with two properties, $firstProperty and $secondProperty however attributes $firstProperty are properties private.

Now when we call the function json_encode on the object $obj as follows:

echo json_encode($obj) . "\n";

Then the result will be:

{"secondProperty":"Second"}

Contrast for serialize:

echo serialize($obj) . "\n";

The result will be:

O:4:"Obj1":2:{s:19:"Obj1firstProperty";s:5:"First";s:14:"secondProperty";s:6:"Second";}

Thus, we can see that the function json_encode does not get the values ​​of the visibility properties private (and similar to protected) while serialize still getting the value of these attributes.

Performance

We'll use the following code to test the performance between the two functions json_encode() and serialize():

<?php

$obj = new stdClass();

$obj->firstProperty = "First value";

$obj->secondProperty = "Second value";


// json_encode

$start = microtime();

for ($i = 0; $i < 100; $i++)

        json_encode($obj);


$end = microtime();

echo $end - $start, "\n";


// serialize

for ($i = 0; $i < 1000; $i++)

        serialize($obj);


$end = microtime();

echo $end - $start, "\n";

The logic of the above code is quite simple, first, we create an object $obj with two properties firstProperty and secondPropertylike in the previous example. We will then use each function json_encode and serialize in a loop and record the time these functions take in turn.

On the computer, when I ran the above code, I received the following results:

0.00019799999999998

0.001377

As the data $obj size of the loop increases as well as the number of loops to be run, the difference in time usage increases. You can also run a test on your computer to check.

Thus, it can be seen that the function json_encode has a faster processing factor serialize.

Portability

When we talk about portability here we want to consider the return of the function json_encode and serialize whether it can be used with other programming languages. Obviously json_encode the winner of this comparison is because the data returned by this function is in JSON format . JSON is the most popular format and is supported by most programming languages. So if you need to process the returned result with a function json_encode , you won't have much trouble. For example, using JavaScript you can use function JSON.parse() to process JSON string from PHP.

Contrast serialize returns a reserved data format type in PHP. Therefore, processing the returned string of this function can take more steps. For example, in the JavaScript language there is no function that supports parsing the returned string from a function serialize.