var re = /[\x00-\x1f\\"]/;
function toJSONString(obj)
{
	switch (typeof obj)
	{
		case 'string':
			return '"' + (re.test(obj) ? encodeString(obj) : obj) + '"';
		case 'number':
		case 'boolean':
			return String(obj);
		case 'object':
			if (obj)
			{
				switch (obj.constructor)
				{
					case Array:
						var a = [];
						for (var i = 0, l = obj.length; i < l; i++)
							a[a.length] = toJSONString(obj[i]);
						return '[' + a.join(',') + ']';
					case Object:
						var a = [];
						for (var i in obj)
							if (obj.hasOwnProperty(i))
								a[a.length] = '"' + (re.test(i) ? encodeString(i) : i) + '":' + toJSONString(obj[i]);
						return '{' + a.join(',') + '}';
					case String:
						return '"' + (re.test(obj) ? encodeString(obj) : obj) + '"';
					case Number:
					case Boolean:
						return String(obj);
					case Function:
					case Date:
					case RegExp:
						return 'undefined';
				}
			}
			return 'null';
		case 'function':
		case 'undefined':
		case 'unknown':
			return 'undefined';
		default:
			return 'null';
	}
}
