Subj : Re: object serialization To : netscape.public.mozilla.jseng From : Jens Thiele Date : Thu Mar 25 2004 11:38 am > Here is the code of my own pure JS solution: > ------------------------------------------------- > function serialize(object, id) { > if (typeof(object) == 'string') return (id == undefined) ? "'" + object > + "'" : id + "='" + object + "';"; > if (typeof(object) == 'function') return object.toString(); > if (typeof(object) != 'object') return (id == undefined) ? object : id + > "=" + object + ";"; > if (object instanceof Date) return (id == undefined) ? object.toSource() > : id + "=" + object.toSource() + ";"; > var openSymbol, closeSymbol, objectString = ''; > if (object.length) { > openSymbol = '['; > closeSymbol = ']'; > if (id != undefined) objectString = id + '='; > objectString += openSymbol; > for (var k in object) objectString += serialize(object[k]) > + ','; > } > else { > openSymbol = '{'; > closeSymbol = '}'; > if (id != undefined) objectString = id + '='; > objectString += openSymbol; > for (var k in object) objectString += k + ':' + > serialize(object[k]) + ','; > } > objectString = objectString.substr(0, objectString.length - 1); > objectString += closeSymbol; > if (id != undefined) objectString += ';'; > return objectString; > } > [...] > Daniel Fournier this is great but i have 2 problems so far: // prototype relationship test Foo.prototype.bar=function(){ print(this.f); } function Foo(f){ this.f=f; } foo=new Foo(10); print('serialized foo: ' + serialize(foo, 'foo') + '\n'); eval(serialize(foo, 'foo2')); foo2.bar(); print(foo.__proto__.bar); // should print function print(foo2.__proto__.bar); // graph test print("-----------------------------------------"); graph={x:foo, y:foo}; graph.x.f=12; graph.y.bar(); print('serialized graph: ' + serialize(graph, 'graph') + '\n'); eval(serialize(graph, 'graph2')); graph2.x.f=77; // should print 77 graph2.y.bar(); .