The difference between serialize and JSON serialization and deserialization in PHP

Source: Internet
Author: User
Tags json serialization advantage

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. Link

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:

Php


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 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.

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.