TextSerializer.deserialize Member
deserialize()
Syntax
object.deserialize();
Returns
Remarks
Reads in enough of the file to deserialize (realize) a single object. The object deserialized is returned; all sub-properties of the object are deserialized with it.
See Also
Source Code
function ts_deserialize()
{
if (!this._open)
this.open("<");
if (!ASSERT(this._open, "Unable to open the file for reading!"))
return false;
var obj = null;
var rv = null;
var objs = new Array();
while (true)
{
if (this._lines.length == 0)
{
var newData = this._fileStream.read();
if (newData)
this._buffer += newData;
else if (this._buffer.length == 0)
break;
// Got more data in the buffer, so split into lines. Unless we're
// done, the last one might not be complete yet, so save that one.
var lines = this._buffer.split(/[\r\n]+/);
if (!newData)
this._buffer = "";
else
this._buffer = lines.pop();
this._lines = this._lines.concat(lines);
if (this._lines.length == 0)
break;
}
// Split each line into "command params...".
var parts = this._lines[0].match(/^\s*(\S+)(?:\s+(.*))?$/);
var command = parts[1];
var params = parts[2];
// 'start' and 'end' commands are special.
switch (command.toLowerCase())
{
case "start":
var paramList = new Array();
if (params)
paramList = params.split(/\s+/g);
var className = "";
if ((paramList.length > 0) && /^<\w+>$/i.test(paramList[0]))
{
className = paramList[0].substr(1, paramList[0].length - 2);
paramList.shift();
}
if (!rv)
{
/* The top-level objects are not allowed a property name
* in their START command (it is meaningless).
*/
ASSERT(paramList.length == 0, "Base object with name!");
// Construct the top-level object.
if (className)
rv = obj = new window[className]();
else
rv = obj = new Object();
}
else
{
var n;
if (paramList.length == 0)
{
/* Create a new object level, but with no name. This is
* only valid if the parent level is an array.
*/
if (!ASSERT(isinstance(obj, Array), "Parent not Array!"))
return null;
if (className)
n = new window[className]();
else
n = new Object();
objs.push(obj);
obj.push(n);
obj = n;
}
else
{
/* Create a new object level, store the reference on the
* parent, and set the new object as the current.
*/
if (className)
n = new window[className]();
else
n = new Object();
objs.push(obj);
obj[ecmaUnescape(paramList[0])] = n;
obj = n;
}
}
this._lines.shift();
break;
case "end":
this._lines.shift();
if (rv && (objs.length == 0))
{
// We're done for the day.
return rv;
}
// Return to the previous object level.
obj = objs.pop();
if (!ASSERT(obj, "Waaa! no object level to return to!"))
return rv;
break;
default:
this._lines.shift();
// The property name may be enclosed in quotes.
if (command[0] == '"')
command = command.substr(1, command.length - 2);
// But it is always escaped.
command = ecmaUnescape(command);
if (!obj)
{
/* If we find a line that is NOT starting a new object, and
* we don't have a current object, we just assume the START
* command was missed.
*/
rv = obj = new Object();
}
if (params[0] == '"') // String
{
// Remove quotes, then unescape.
params = params.substr(1, params.length - 2);
obj[command] = ecmaUnescape(params);
}
else if (params[0] == "/") // RegExp
{
var p = params.match(/^\/(.*)\/(\w*)$/);
if (ASSERT(p, "RepExp entry malformed, ignored!"))
{
var re = new RegExp(ecmaUnescape(p[1]), p[2]);
obj[command] = re;
}
}
else if (params == "null") // null
{
obj[command] = null;
}
else if (params == "undefined") // undefined
{
obj[command] = undefined;
}
else if ((params == "true") || (params == "false")) // boolean
{
obj[command] = (params == "true");
}
else // Number
{
obj[command] = Number(params);
}
break;
}
}
return null;
}