js-serialize-unserialize
Gesponsorde koppelingen
PHP script bestanden
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?
String.prototype.unserialize = function() {
/* A nice regular expression to break up the array */
x=this.match(new RegExp('"[^"]+?"|[^{};:]+','g'));
/* Check length */
if(!x || x.length < 4) return null;
/* Launch unserializer with flat array */
return _unserialize(x).result;
};
function _unserialize(x) {
/* Setup all variables */
var cnt = 2, i = 0, ret = [], type = 'key', y,
pat = new RegExp('^"|"$','g');
/* While i is smaller then item count */
while(i<x[1]) {
switch(x[cnt]) {
/* Array, loop recursive */
case 'a':
y = _unserialize(x.slice(cnt));
ret.push(y.result);
cnt+=y.count;
break;
/* Integer */
case 'i':
if(type=='value')ret.push(parseInt(x[cnt+1]));
cnt++;
break;
/* String */
case 's':
if(type=='value')ret.push(x[cnt+2].replace(pat,''));
cnt+=2;
break;
/* Boolean */
case 'b':
if(type=='value')ret.push(parseInt(x[cnt+1])?true:false);
cnt++;
break;
}
/* Switch key or value */
type = (type=='key') ? 'value' : 'key';
/* Increment itemcount on value */
i+= (type=='key') ? 1 : 0;
/* Increment object count */
cnt++;
};
/* Return object with results and item count */
return {result:ret,count:cnt};
}
function serialize(obj) {
var x = ['a:',obj.length,':{'];
for(var i in obj) {
x.push('i:',i,';');
switch(typeof obj[i]) {
case 'string':
x.push('s:',obj[i].length,':"',obj[i],'";');
break;
case 'boolean':
x.push('b:',obj[i]?1:0,';');
break;
case 'number':
x.push('i:'+obj[i]+';');
break;
case 'object':
x.push(serialize(obj[i]));
break;
}
}
return (x.join('')+'}');
};
?>
String.prototype.unserialize = function() {
/* A nice regular expression to break up the array */
x=this.match(new RegExp('"[^"]+?"|[^{};:]+','g'));
/* Check length */
if(!x || x.length < 4) return null;
/* Launch unserializer with flat array */
return _unserialize(x).result;
};
function _unserialize(x) {
/* Setup all variables */
var cnt = 2, i = 0, ret = [], type = 'key', y,
pat = new RegExp('^"|"$','g');
/* While i is smaller then item count */
while(i<x[1]) {
switch(x[cnt]) {
/* Array, loop recursive */
case 'a':
y = _unserialize(x.slice(cnt));
ret.push(y.result);
cnt+=y.count;
break;
/* Integer */
case 'i':
if(type=='value')ret.push(parseInt(x[cnt+1]));
cnt++;
break;
/* String */
case 's':
if(type=='value')ret.push(x[cnt+2].replace(pat,''));
cnt+=2;
break;
/* Boolean */
case 'b':
if(type=='value')ret.push(parseInt(x[cnt+1])?true:false);
cnt++;
break;
}
/* Switch key or value */
type = (type=='key') ? 'value' : 'key';
/* Increment itemcount on value */
i+= (type=='key') ? 1 : 0;
/* Increment object count */
cnt++;
};
/* Return object with results and item count */
return {result:ret,count:cnt};
}
function serialize(obj) {
var x = ['a:',obj.length,':{'];
for(var i in obj) {
x.push('i:',i,';');
switch(typeof obj[i]) {
case 'string':
x.push('s:',obj[i].length,':"',obj[i],'";');
break;
case 'boolean':
x.push('b:',obj[i]?1:0,';');
break;
case 'number':
x.push('i:'+obj[i]+';');
break;
case 'object':
x.push(serialize(obj[i]));
break;
}
}
return (x.join('')+'}');
};
?>