Analysis of serialize and JSON serialization and deserialization in PHP _php techniques

Source: Internet
Author: User
Tags json php and serialization advantage

This article illustrates the difference between serialization of Serialize and JSON in PHP and deserialization operations. Share to everyone for your reference, specific as follows:

What is the difference between serialize and JSON serialization and deserialization in PHP, we can take a look at the problem with the small set, the specific operational details are shown below.

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

Suppose 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;

They are serialized and deserialized, respectively, with the Serialize function and the Unserialize function, 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)));

Results:

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

Use JSON method:

Var_dump (Serialize ($web));
Var_dump (Unserialize (Serialize ($web)));
Var_dump (Json_encode ($web));
Var_dump (Json_decode (Json_encode ($web), true));

Results:

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 a previously defined object or array, serialized with Serialize and JSON, the deserialized result is the same as it was, and there is no difference except that the serialized format is different.

So what's the difference between them? The following text summary is very good, do not explain yourself, you can write code validation.

Serializing and deserializing using JSON

Advantage:

Variable can still be read after serialization

Can be used for other systems because the JSON format is standard

Disadvantage:

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

Valid only for examples of stdclass classes

Serialization and deserialization using the Serialize method

Advantage:

Allow non-UTF-8 variables

Support for other instances except the Stdclass exception

Disadvantage:

Encoded text is unreadable to people

Cannot be referenced by a system in another language

OK, 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)));

Results:

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 constr UCT ' (length=14)
string ' {' class ': ' Test construct '} ' (length=26)
object (StdClass) [127] public
 ' class ' = > string ' Test construct ' (length=14)

We find that JSON serialization and deserialization lose private member variables in the class, while serialize serialization and deserialization can be as long as a variable of the class, but the member methods of the class cannot be serialized and deserialized.

In general, it's better to use JSON, because JSON is a common format across platforms, and in addition to JSON, it's good to use XML. When do you use the Serialize method?

The Magic Method __wakeup () is invoked by default when serialize deserialization of a class, which allows the object to re-establish various states that could not be preserved when serialized. For example, database connections, and so on. That's another question.

For more information about PHP interested readers can view the site topics: "PHP string (String) Usage summary", "PHP array Operation techniques Encyclopedia", "PHP basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP Network programming Skills Summary" , "Introduction to PHP object-oriented Programming", "PHP+MYSQL database operations to get Started" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.