Introduction to serialization and json in php _ javascript skills

Source: Internet
Author: User
Serialization is the process of converting the object state to a retained or transfer-able format. In contrast to serialization, deserialization converts a stream into an object. These two processes are combined to easily store and transmit data [serialization concept]

Serialization is the process of converting the object state to a retained or transfer-able format. In contrast to serialization, deserialization converts a stream into an object. These two processes can be combined to easily store and transmit data.

The process of converting the object state information to a form that can be stored or transmitted. During serialization, the object writes its current state to the temporary or persistent storage area. Later, you can re-create the object by reading or deserializing the object status from the bucket.

Generally, all fields of the object instance are serialized, which means that the data is expressed as the serialized data of the instance. In this way, codes in this format may be able to determine the value of the data without the access of the member. Similarly, deserialization extracts data from the serialized representation and directly sets the object status, which is irrelevant to the accessibility rules. For any object that may contain important security data, if possible, the object should not be serialized. If it must be serializable, try to generate a specific field to save important data that cannot be serialized. If this cannot be achieved, you should note that the data will be disclosed to any code with serialization permissions, and that this permission will not be obtained by any malicious code.

[JSON concept]

JSON, JavaScript Object Notation, a lighter and more friendly format for interface (AJAX, REST, etc.) data exchange. JSON is a serialized text format of structured data. As a substitute for XML, JSON is used to represent the format of data exchange between the client and the server. It is derived from the ECMAScript language standard. JSON is designed to make it small, lightweight, and textual, and a subset of JavaScript.

[Length comparison]

The following code shows the encoded string and its length of the array and object.

The Code is as follows:


Class Foo {

Public $ int = 1;
Public $ bool = TRUE;
Public $ array = array (1), 2 => 'test', 'string ');

Public function test ($ flag ){
Echo $ flag, 'test function for Foo
';
}

Public static function output ($ str ){
Echo $ str ,'
';
}

Public static function compare_serialize_and_json ($ data ){
$ Serialize_str = serialize ($ data );
Self: output ('serialized value: '. $ serialize_str. "; length = ".
Strlen ($ serialize_str ));

$ Json_str = json_encode ($ data );
Self: output ('json value: '. $ json_str. "; length =". strlen ($ json_str ));
}

}

$ Test_data = array ('wwww '=> 0, 'phppanc' => 1, 'com' => 2 );
// Serialize the Array

Echo 'array:
';
Foo: compare_serialize_and_json ($ test_data );

$ Foo = new Foo ();
Echo 'object:
';
Foo: compare_serialize_and_json ($ foo );

Output:

The Code is as follows:


Array:
Serialized value: a: 3: {s: 4: "wwww"; I: 0; s: 6: "phppan"; I: 1; s: 3: "com"; I: 2;}; length = 52
JSON value: {"wwww": 0, "phppan": 1, "com": 2}; length = 29
Object:
Serialized value: O: 3: "Foo": 3: {s: 3: "int"; I: 1; s: 4: "bool"; B: 1; s: 5: "array"; a: 3: {I: 0;
A: 1: {I: 0; I: 1;} I: 2; s: 4: "test"; I: 3; s: 6: "string" ;}}; length = 111
JSON value: {"int": 1, "bool": true, "array": {"0": [1], "2": "test ", "3": "string" }}; length = 63

Obviously, the length difference is that serialize is about twice the size of json After encoding.

Cause:

• After serialize, the string contains the length of the sub-string, which may be an optimization in speed. The typical space is changed for time, but it is still too heavy.
• Serialize has more detailed type differentiation, while json only has four types and is represented by simple symbols.

[Speed comparison]

Describe the problem using the code. The following code compares the speed:

The Code is as follows:


$ Max_index = 10;
Ini_set ("memory_limit", "512 M ");
$ Array = array_fill (0, 1000000, rand (1, 9999 ));

Echo 'serialize:
';
$ Start = xdebug_time_index ();
For ($ I = 0; $ I <$ max_index; $ I ++ ){
$ Str = serialize ($ array );
}
$ End = xdebug_time_index ();
Echo $ end-$ start ,'
';

Echo 'json:
';
$ Start = xdebug_time_index ();
For ($ I = 0; $ I <$ max_index; $ I ++ ){
$ Str = json_encode ($ array );
}
$ End = xdebug_time_index ();
Echo $ end-$ start ,'
';
Unset ($ array, $ str );

Output:

The Code is as follows:


Serialize:
9.5371007919312
Json:
1.4313209056854

The speed of serialize is an order of magnitude faster than that of json in the case of large data volumes.

From the above two points, json is better than serialize in both speed and size of the generated string. Why does serialize still exist? The reason is as follows: the implemented function.

[Processing object]

The following code:

The Code is as follows:


Header ("Content-type: text/html; charset = utf8 ");
Class Foo {
Public function test ($ flag ){
Echo $ flag, 'test function for Foo
';
}
}

$ Foo = new Foo ();

Echo 'deserialization test:
';
$ Foo-> test (1 );
$ Serialize_str = serialize ($ foo );
$ Obj = unserialize ($ serialize_str );
$ Obj-> test (2 );

$ Foo-> test (1 );
$ Json_str = json_encode ($ foo );
$ Obj = json_decode ($ json_str );
$ Obj-> test (2 );
Die ();

Output:

The Code is as follows:


Deserialization test:
1 test function for Foo
2 test function for Foo
1 test function for Foo

(! ) Fatal error: Call to undefined method stdClass: test ()

Json cannot process data such as object methods.

Scope of use]

• Serialize is used for serialization, especially for Object Storage. This is the meaning of its existence.
• Json can be used for Object-independent data storage, such as arrays containing a large number of numbers. In this case, we may need to reconstruct the database.
• JSON is used for data exchange, which is also the definition.
• Currently JSON is the data that can be used for UTF-8 encoding.

Related Article

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.