Differences between SERIALIZE and JSON serialization and deserialization operations in PHP

Source: Internet
Author: User
This article mainly introduces the differences between SERIALIZE and JSON serialization and deserialization operations in PHP, the serialization and deserialization operations in php are analyzed in detail in the form of instances, including the feelings, differences, usage methods and related operation skills, for more information about SERIALIZE and JSON in PHP, see the following example. We will share this with you for your reference. The details are as follows:

What is the difference between SERIALIZE and JSON serialization and deserialization in PHP? let's take a look at this issue with Xiaobian. the specific operation details are as follows.

In PHP, what is the difference between serialize and json to serialize or deserialize an object or array?

Assume an object and an array:

$ Web = new stdClass; $ web-> site = 'tantengvip '; $ web-> owner = 'tuntun'; $ web-> age = 5; // and $ web = array (); $ web ['site'] = 'tantengvip '; $ web ['owner'] = 'tuntun '; $ web ['age'] = 5;

Use the serialize and unserialize functions for serialization and deserialization respectively to see what the printed results are as follows:

Use the serialize method:

var_dump(serialize($web));var_dump(unserialize(serialize($web)));var_dump(json_encode($web));var_dump(json_decode(json_encode($web)));

Result:

string 'O:8:"stdClass":3:{s:4:"site";s:10:"tantengvip";s:5:"owner";s:6:"tuntun";s:3:"age";i:5;}' (length=87)object(stdClass)[127] public 'site' => string 'tantengvip' (length=10) public 'owner' => string 'tuntun' (length=6) public 'age' => int 5string '{"site":"tantengvip","owner":"tuntun","age":5}' (length=46)object(stdClass)[127] public 'site' => string 'tantengvip' (length=10) public 'owner' => string 'tuntun' (length=6) public 'age' => int 5

Json format:

var_dump(serialize($web));var_dump(unserialize(serialize($web)));var_dump(json_encode($web));var_dump(json_decode(json_encode($web),true));

Result:

string 'a:3:{s:4:"site";s:10:"tantengvip";s:5:"owner";s:6:"tuntun";s:3:"age";i:5;}' (length=74)array (size=3) 'site' => string 'tantengvip' (length=10) 'owner' => string 'tuntun' (length=6) 'age' => int 5string '{"site":"tantengvip","owner":"tuntun","age":5}' (length=46)array (size=3) 'site' => string 'tantengvip' (length=10) 'owner' => string 'tuntun' (length=6) 'age' => int 5

We found that for such an object or array as defined above, serialize and json are used for serialization. The deserialization results are the same and there is no difference, except for the serialization format.

So what are the differences between them? The text below is a good summary, so I won't explain it myself. you can write code for verification.

Use json serialization and deserialization

Advantages:

Variable readable after serialization

It can be used by other systems because the JSON format is standard.

Disadvantages:

Only valid for UFT-8 data, other code may not work very well

Valid only for stdClass examples

Serialize serialization and deserialization

Advantages:

Allow variables for non-UTF-8

Other instances except stdClass examples are supported.

Disadvantages:

The encoded text is unreadable to humans.

Cannot be referenced by systems in other languages

Well, write a code to see:

class Test{  private $pri = 'pri';  public $class = 'Test';  public function __construct()  {    $this->class = 'Test construct';    $this->pri = 'pri construct';  }}$test = new Test();var_dump(serialize($test));var_dump(unserialize(serialize($test)));var_dump(json_encode($test));var_dump(json_decode(json_encode($test)));

Result:

string 'O:4:"Test":2:{s:9:"�Test�pri";s:13:"pri construct";s:5:"class";s:14:"Test construct";}' (length=86)object(Test)[127] private 'pri' => string 'pri construct' (length=13) public 'class' => string 'Test construct' (length=14)string '{"class":"Test construct"}' (length=26)object(stdClass)[127] public 'class' => string 'Test construct' (length=14)

We found that the private member variables in the class are lost in json serialization and deserialization, while serialize serialization and deserialization can be used as long as they are class variables, however, the member methods of the class cannot be serialized or deserialized.

In general, it is better to use json because json is a common cross-platform format. besides json, xml is also better. When will the serialize method be used?

When serialize deserialization is performed on a class, the magic method _ wakeUp () is called by default, so that the object can re-establish various statuses that are not retained during serialization. For example, database connection. That's another question. I will not go into it here.

I hope this article will help you with PHP programming.

For more articles on how to analyze the differences between SERIALIZE and JSON serialization and deserialization operations in PHP, refer to the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.