Complete implementation of PHP serialize/unserialize in Javascript version

Source: Internet
Author: User

This article from: http://www.coolcode.cn /? P = 171
Author: Ma bingyao

Previously written phprpc implementation, the Javascript version of the serialization implementation is modified from the http://www.devpro.it/code/102.html, this implementation is still updated, but it does not fully implement all the PHP serialization mark, therefore, it cannot serialize complex objects, such as nested objects, or deserialize all PHP serialized content. So I re-compiled an implementation that is fully compatible with PHP 5 serialization and can deserialize PHP 4, PHP 5, and Php 6 serialization content. The magic methods _ sleep and _ wakeup are supported, and the serializable interface serialization and deserialization are supported (there is no interface concept in Javascript, therefore, as long as the object contains the serialize and unserialize methods, for more information about the serializable interface and custom serialization, see: PhP serialization (serialize) format description-custom object serialization ). This implementation is compatible with browsers of the following versions (tested ):

// IE 4 + (including Windows Vista's IE 7 + and Windows Mobile 2003/Windows moblie 5's IE 4)
// Mozilla/Firefox
// Opera (including opera mini and opera mobile, but the serialization of object names supported by opera mobile 8.5 beta 2 for Windows Mobile 5 Pocket PC objects is incomplete, other versions are normal .)
// Konqueror
// Safari
// Epiphany
// Other browsers are not tested, but they should basically be supported by any browser that supports JavaScript (conditional users hope to help with the test ).

This program needs to callUTF. jsThe utf16to8 and utf8to16 functions in to convert the byte stream to the javascript string.

Download:Phpserializer. js

/* Phpserializer. js-JavaScript to PhP serialize/unserialize class.
*
* This class is designed to convert PHP variables to Javascript
* And JavaScript variables to PHP with a PHP serialize unserialize
* Compatible way.
*
* Copyright (c) 2006 Ma bingyao <andot@ujn.edu.cn>
* Version: 3.0c
* Lastmodified: Jun 1, 2006
* This library is free. You can redistribute it and/or modify it.
* Http://www.coolcode.cn /? P = 171
*/

Function serialize (o ){
VaR p = 0, SB = [], ht = [], HV = 1;
Function classname (o ){
If (typeof (o) = "undefined" | typeof (O. constructor) = "undefined") return '';
VaR C = O. constructor. tostring ();
C = utf16to8 (C. substr (0, C. indexof ('(')). replace (/(^/S * function/S *) | (/S * $)/ig ,''));
Return (C = '')? 'Object': C );
}
Function is_int (n ){
VaR S = n. tostring (), L = S. length;
If (L> 11) return false;
For (VAR I = (S. charat (0) = '-')? 1: 0; I <L; I ++ ){
Switch (S. charat (I )){
Case '0 ':
Case '1 ':
Case '2 ':
Case '3 ':
Case '4 ':
Case '5 ':
Case '6 ':
Case '7 ':
Case '8 ':
Case '9': break;
Default: Return false;
}
}
Return! (N <-2147483648 | n> 2147483647 );
}
Function in_ht (o ){
For (K in HT) if (HT [k] = O) return K;
Return false;
}
Function ser_null (){
SB [p ++] = 'n ;';
}
Function ser_boolean (B ){
SB [p ++] = (B? 'B: 1;': 'B: 0 ;');
}
Function ser_integer (I ){
SB [p ++] = 'I:' + I + ';';
}
Function ser_double (d ){
If (D = number. positive_infinity) d = 'inf ';
Else if (D = number. negative_infinity) d = '-INF ';
SB [p ++] = 'd: '+ D + ';';
}
Function ser_string (s ){
VaR utf8 = utf16to8 (s );
SB [p ++] ='s: '+ utf8.length + ':"';
SB [p ++] = utf8;
SB [p ++] = '";';
}
Function ser_array (){
SB [p ++] = 'a :';
VaR Lp = P;
SB [p ++] = 0;
SB [p ++] = ':{';
For (var k in ){
If (typeof (A [k])! = 'Function '){
Is_int (k )? Ser_integer (k): ser_string (k );
_ Serialize (A [k]);
SB [LP] ++;
}
}
SB [p ++] = '}';
}
Function ser_object (o ){
VaR Cn = classname (O );
If (CN = '') ser_null ();
Else if (typeof (O. serialize )! = 'Function '){
SB [p ++] = 'O: '+ CN. Length + ':"';
SB [p ++] = cn;
SB [p ++] = '":';
VaR Lp = P;
SB [p ++] = 0;
SB [p ++] = ':{';
If (typeof (O. _ sleep) = 'function '){
VaR A = O. _ sleep ();
For (var kk in ){
Ser_string (A [Kk]);
_ Serialize (O [A [Kk]);
SB [LP] ++;
}
}
Else {
For (var k in O ){
If (typeof (O [k])! = 'Function '){
Ser_string (k );
_ Serialize (O [k]);
SB [LP] ++;
}
}
}
SB [p ++] = '}';
}
Else {
VaR cs = O. serialize ();
SB [p ++] = 'C: '+ CN. Length + ':"';
SB [p ++] = cn;
SB [p ++] = '":' + CS. Length + ':{';
SB [p ++] = cs;
SB [p ++] = "}";
}
}
Function ser_pointref (r ){
SB [p ++] = "R:" + R + ";";
}
Function ser_ref (r ){
SB [p ++] = "R:" + R + ";";
}
Function _ serialize (o ){
If (O = NULL | O. constructor = function ){
HV ++;
Ser_null ();
}
Else switch (O. constructor ){
Case Boolean :{
HV ++;
Ser_boolean (O );
Break;
}
Case number :{
HV ++;
Is_int (o )? Ser_integer (o): ser_double (O );
Break;
}
Case string :{
HV ++;
Ser_string (O );
Break;
}
Case array :{
VaR r = in_ht (O );
If (r ){
Ser_pointref (R );
}
Else {
HT [HV ++] = O;
Ser_array (O );
}
Break;
}
Default :{
VaR r = in_ht (O );
If (r ){
HV ++;
Ser_ref (R );
}
Else {
HT [HV ++] = O;
Ser_object (O );
}
Break;
}
}
}
_ Serialize (O );
Return sb. Join ('');
}

Function unserialize (SS ){
VaR p = 0, ht = [], HV = 1; r = NULL;
Function unser_null (){
P ++;
Return NULL;
}
Function unser_boolean (){
P ++;
VaR B = (ss. charat (P ++) = '1 ');
P ++;
Return B;
}
Function unser_integer (){
P ++;
VaR I = parseint (ss. substring (P, P = ss. indexof (';', p )));
P ++;
Return I;
}
Function unser_double (){
P ++;
VaR d = ss. substring (P, P = ss. indexof (';', p ));
Switch (d ){
Case 'inf ': D = number. positive_infinity; break;
Case '-INF': D = number. negative_infinity; break;
Default: D = parsefloat (d );
}
P ++;
Return D;
}
Function unser_string (){
P ++;
VaR L = parseint (ss. substring (P, P = ss. indexof (':', p )));
P + = 2;
VaR S = utf8to16 (ss. substring (p, p + = L ));
P + = 2;
Return S;
}
Function unser_array (){
P ++;
VaR n = parseint (ss. substring (P, P = ss. indexof (':', p )));
P + = 2;
VaR A = [];
HT [HV ++] =;
For (VAR I = 0; I <n; I ++ ){
VaR K;
Switch (ss. charat (P ++ )){
Case 'I': K = unser_integer (); break;
Case's ': K = unser_string (); break;
Case 'U': K = unser_unicode_string (); break;
Default: Return false;
}
A [k] = _ unserialize ();
}
P ++;
Return;
}
Function unser_object (){
P ++;
VaR L = parseint (ss. substring (P, P = ss. indexof (':', p )));
P + = 2;
VaR Cn = utf8to16 (ss. substring (p, p + = L ));
P + = 2;
VaR n = parseint (ss. substring (P, P = ss. indexof (':', p )));
P + = 2;
If (eval (['typeof (', CN,') = "undefined" ']. Join (''))){
Eval (['function', CN, '() {}']. Join (''));
}
VaR o = eval (['new', CN, '()']. Join (''));
HT [HV ++] = O;
For (VAR I = 0; I <n; I ++ ){
VaR K;
Switch (ss. charat (P ++ )){
Case's ': K = unser_string (); break;
Case 'U': K = unser_unicode_string (); break;
Default: Return false;
}
If (K. charat (0) = '/0 '){
K = K. substring (K. indexof ('/0', 1) + 1, K. Length );
}
O [k] = _ unserialize ();
}
P ++;
If (typeof (O. _ wakeup) = 'function') O. _ wakeup ();
Return O;
}
Function unser_custom_object (){
P ++;
VaR L = parseint (ss. substring (P, P = ss. indexof (':', p )));
P + = 2;
VaR Cn = utf8to16 (ss. substring (p, p + = L ));
P + = 2;
VaR n = parseint (ss. substring (P, P = ss. indexof (':', p )));
P + = 2;
If (eval (['typeof (', CN,') = "undefined" ']. Join (''))){
Eval (['function', CN, '() {}']. Join (''));
}
VaR o = eval (['new', CN, '()']. Join (''));
HT [HV ++] = O;
If (typeof (O. unserialize )! = 'Function') P + = N;
Else O. unserialize (ss. substring (p, p + = n ));
P ++;
Return O;
}
Function unser_unicode_string (){
P ++;
VaR L = parseint (ss. substring (P, P = ss. indexof (':', p )));
P + = 2;
VaR sb = [];
For (I = 0; I <L; I ++ ){
If (SB [I] = ss. charat (P ++) = '//'){
SB [I] = string. fromcharcode (parseint (ss. substring (p, p + = 4), 16 ));
}
}
P + = 2;
Return sb. Join ('');
}
Function unser_ref (){
P ++;
VaR r = parseint (ss. substring (P, P = ss. indexof (';', p )));
P ++;
Return HT [R];
}
Function _ unserialize (){
Switch (ss. charat (P ++ )){
Case 'N': Return HT [HV ++] = unser_null ();
Case 'B': Return HT [HV ++] = unser_boolean ();
Case 'I': Return HT [HV ++] = unser_integer ();
Case 'D': Return HT [HV ++] = unser_double ();
Case's ': Return HT [HV ++] = unser_string ();
Case 'U': Return HT [HV ++] = unser_unicode_string ();
Case 'r': Return HT [HV ++] = unser_ref ();
Case 'A': Return unser_array ();
Case 'O': Return unser_object ();
Case 'C': Return unser_custom_object ();
Case 'r': Return unser_ref ();
Default: Return false;
}
}
Return _ unserialize ();
}

Test page: http://test.coolcode.cn/phpserializer

 

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.