JavaScript serialization object implementation code

Source: Internet
Author: User


The author sends a picture, which can be enlarged.

The Literal Syntax problem about JavaScript was mentioned a few days ago and it was quite interesting. So I studied it again. Can I convert the object into the Literal form? Just like what we usually call serialization and deserialization. Of course. Because JavaScript objects provide a toString () method, the Literal form of simple objects is returned by default.

What we need to do is to determine the specific type of the Object, and then Serialize each Object separately, and then output the Literal syntax form of the Object. To accurately determine the object type, use the _ typeof _ method that I once mentioned. The code for serializing the object instance is as follows:
Copy codeThe Code is as follows:
Object. prototype. Serialize = function ()
{
Var type = _ typeof _ (this );
Switch (type)
{
Case 'array ':
{
Var strArray = '[';
For (var I = 0; I <this. length; ++ I)
{
Var value = '';
If (this [I])
{
Value = this [I]. Serialize ();
}
StrArray + = value + ',';
}
If (strArray. charAt (strArray. length-1) = ',')
{
StrArray = strArray. substr (0, strArray. length-1 );
}
StrArray + = ']';
Return strArray;
}
Case 'date ':
{
Return 'new Date ('+ this. getTime () + ')';
}
Case 'boolean ':
Case 'function ':
Case 'number ':
Case 'string ':
{
Return this. toString ();
}
Default:
{
Var serialize = '{';
For (var key in this)
{
If (key = 'serialize') continue;
Var subserialize = 'null ';
If (this [key]! = Undefined)
{
Subserialize = this [key]. Serialize ();
}
Serialize + = '\ r \ n' + key +': '+ subserialize + ',';
}
If (serialize. charAt (serialize. length-1) = ',')
{
Serialize = serialize. substr (0, serialize. length-1 );
}
Serialize + = '\ r \ n }';
Return serialize;
}
}
};

Actually, it is difficult to compare the attributes of Array and Object. We need to perform this Serialize operation recursively. However, the Serialize method does not need to be serialized. The following is a test example. However, this serialization method does not check ring references, and the objects that can be serialized are limited.
Copy codeThe Code is as follows:
Var obj1 = [];
Alert (obj1.Serialize ());

Var obj2 = [1, [2, [3, [4, [5, [6, [7, [8, [9, [0];
Alert (obj2.Serialize ());

Var obj3 =
{
Properties1: 1, Properties2: '2', Properties3: [3],
Method1: function () {return this. Properties1 + this. Properties3 [0];},
Method2: function () {return this. Preperties2 ;}
};
Alert (obj3.Serialize ());

Var obj4 = [null, 1, 'string', true, function () {return 'kekeke';}, new Object ()];
Alert (obj4.Serialize ());

As for deserialization, it is very easy to execute the above serialization result with eval and you will get the class instance.

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.