Differences between SERIALIZE and JSON serialization and deserialization in PHP-PHP source code

Source: Internet
Author: User
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. 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.

Script ec (2); script

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 5

String '{"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 5

String '{"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. (Link)

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:

PHP


String 'O: 4: "Test": 2: {s: 9: "Fig"; 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.

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.