<?
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('')+'}');
};
?>