Subj : Re: object serialization To : netscape.public.mozilla.jseng From : Daniel Fournier Date : Thu Mar 25 2004 07:54 am Jens Thiele wrote: > Brendan Eich wrote: > >>Brendan Eich wrote: >> >> >>>You can turn on JS_HAS_XDR_FREEZE_THAW via jsconfig.h. If you want it >>>on by default, you'll be opening up potential (not known) security >>>holes. It really should be configured on >> >> >>*only* >> >> >>>for embeddings that trust all scripts and the strings they thaw. >>> >>>/be > > => this is not an option for me > > other solutions? > perhaps a pure javascript solution out there? > > karme > Hi Jens, 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; } //TESTING var b = true; var s = 'ABCD'; var n = 1234; var d = new Date(); function f(arg){ return arg.toString()}; var a = [9, 8, 7, 6]; var o = {one:b, two:s, three:n, four:d, five:f}; var oo = {first:a, second:o}; dump('serialized b: ' + serialize(b, 'b') + '\n'); dump('serialized s: ' + serialize(s, 's') + '\n'); dump('serialized n: ' + serialize(n, 'n') + '\n'); dump('serialized d: ' + serialize(d, 'd') + '\n'); dump('serialized f: ' + serialize(f, 'f') + '\n'); dump('serialized a: ' + serialize(a, 'a') + '\n'); dump('serialized o: ' + serialize(o, 'o') + '\n'); dump('serialized oo: ' + serialize(oo, 'oo') + '\n'); ------------------------------------------------- Daniel Fournier .