
try { now_loading('Data serialisation support.'); } catch (e) {}

//|
//| return the type or class of the supplied expression
//|
function gettype(w)
{
	var t = typeof(w);
	if (t == 'object')
	{
		t = w.constructor.toString().split("\n").join('');
		if (/\[native code\]/.test(t)) t = t.toLowerCase(); 
		t = t.replace(/^function /, '');
		t = t.replace(/\(.*/, '');
		if (/^\s*$/.test(t)) t = 'function';
	}
	return t;
}

function __unserialise__functor001()
{
  //--------- generic portion -------------------
	var ac = ([]).merge(arguments);
	var al = ac.length;      				// number of arguments supplied to the functor
	var nm = al - 3;							  // number of $N match variables supplied to the functor
	  var ss = ac.shift();	  			// the matched substring
  	var fs = ac.pop();		  			// the current state of the source string?
  	var os = ac.pop();     				// the offset of the match into the source string 
	var matches = ac;               // array of match variables
  //---------------------------------------------

	return '"'+unescape(matches[0])+'"';
}

function unserialise(v)
{
	return eval('_result = '+unescape(new String(v)).replace(/"([^"]*)"/g, __unserialise__functor001));
}

//|
//| return a string representing the 
//|
function serialise(v)
{
	var i, s;
	s = '';

	if (v == null || gettype(v) == 'undefined') return 'undef';
	switch (gettype(v))
	{
		case 'boolean':
			s = v ? 1 : 0;
			break;

		case 'number':
			s = 0+v;
			break;

		case 'string':
			s = '<S:>'+v+'<:S>';
			break;

		case 'array':
			s += '[';
			var ta = [];
			for (i=0; i<v.length; i++) {
				var e = serialise(v[i]);
				ta.push(e);
			}
			s += ta.join(', ');
			s += ']';
			break;

		case 'object':
			s += '{';
			var to = [];
			for (i in v) {
				var e = serialise(v[i]);
				if (gettype(v[i]) != 'function') to.push(i+' => '+e);
			}
			s += to.join(', ');
			s += '}';
			break;

		default:
			return 'undef'
			break;
	}
	return s;
}


