Serialize and Json_encode differences

Source: Internet
Author: User

(1) Serialize is mainly used for the serialization of PHP, stored in a file or database, Json_encode is also serialized, but mainly used in other languages such as JS for interactive use, for the transmission, JSON has many advantages.

(2) on display, the Serialize serialized string contains additional content, which is the encoding of the type and length of the value

(3) in the Json_decode decoding, it is important to note that if it is a serialized numeric array, then Json_decode can revert to the original array (numeric array oh), but, if the serialized is an associative array (or object), then because the Json_ Decode cannot tell if it is an associative array or an object (we can understand that these two long images cannot be distinguished), so Json_decode will restore the object resolved to Stdclass by default, and if True is resolved to an associative array

(4) Json_encode cannot serialize the member method of the object, the code is as follows:

Serialize serialization, the deserialization of the data, you can recall the member method, but Json_decode not, will be error, specifically can test,

(5) __sleep and __wakeup Magic method

When an object is serialized, PHP invokes the __sleep method (if present, and operates, such as assigning a value to a variable). After crossdress an object, PHP calls the __wakeup method. Both methods do not accept parameters. The __sleep method must return an array containing the properties that need to be serialized. PHP discards the values of other properties. If there is no __sleep method, PHP will save all properties.
Before the program executes, the Serialize () function first checks for the existence of a magic method __sleep. If present, the __sleep () method is called first before performing a serialization (serialization) operation. This feature can be used to clean up an object and return an array containing all the variable names in the object (which must be returned. In contrast, unserialize () checks for the existence of a __wakeup method. If present, the __wakeup method is called first, such as a reconnection that can be used for the database

Serialize and Json_encode differences

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.