JavaScript
The serialization implementation of the JavaScript version was modified from http://www.devpro.it/code/102.html when the PHPRPC implementation was previously written, although it is still being updated, but it does not fully implement all the tags of PHP serialization, As a result, it cannot serialize complex objects, such as nested objects, or deserialize all the contents of PHP serialization. So I rewritten an implementation that was completely compatible with the serialization of PHP 5 and could deserialize PHP 4, PHP 5, and PHP 6 serialized content. Support for Magic methods __sleep and __wakeup, enabling serialization and deserialization of the Serializable interface (no interface in JavaScript, so long as the object contains the serialize and Unserialize methods, Serializable interface and custom serialization for more information see: PHP Serialization (serialize) format detailed-custom object serialization. This implementation is compatible with the following version of the browser (tested):
IE 4+ (including Windows Vista IE 7+ and Windows Mobile 2003/windows moblie 5 of IE 4)
Mozilla/firefox
Opera (including opera Mini and Opera Mobile, but for the Opera Mobile 8.5 Beta 2 for Windows Mobile 5 Pocket PC object serialization, the serialization of object name support is incomplete, while other versions are normal. )
Konqueror
Safari
Epiphany
Other browsers are not tested, but they should basically be supported by JavaScript-enabled browsers (conditional users want to be able to help test).
In this program you need to call the Utf16to8 and UTF8TO16 functions in utf.js to convert between the byte stream and the JavaScript string.
Download: phpserializer.js
/* Phpserializer.js-javascript to PHP Serialize/unserialize class.
*
* This class are 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 2, 2006
* This library are 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 > one) 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 (a) {
sb[p++] = ' A: ';
var LP = P;
sb[p++] = 0;
sb[p++] = ': {';
for (var k in a) {
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 a) {
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++] = A;
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 A;
}
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) = = ' = ') {
k = k.substring (K.indexof (' 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