View Full Version : Representing serialized object in javascript

07-14-2005, 04:39 AM
What's the best way to represent linked object (for example, a tree) in javascript literal?

In perl, you can represent hash table, array and list in literal with references.

a => 1,
b => { a => 2,
b => undef }

For JS, I found this example in treeview.net

foldersTree = gFld("<i>Treeview Demo</i>", "demoFramesetRightFrame.html")
aux1 = insFld(foldersTree, gFld("Photos example", "demoFramesetRightFrame.html"))
aux2 = insFld(aux1, gFld("United States", "http://www.treeview.net/treemenu/demopics/beenthere_america.gif"))
insDoc(aux2, gLnk("R", "Boston", "http://www.treeview.net/treemenu/demopics/beenthere_boston.jpg"))
insDoc(aux2, gLnk("R", "New York", "http://www.treeview.net/treemenu/demopics/beenthere_newyork.jpg"))

Basically it's instantiating an object and adding it to its parent using object's method.

But it's not as clean comparing to the perl way. Especially if it is a config file or it has a lot of nodes (it is not trivial, say, to move a section of the nodes around and reattach it to another place).

OTOH, we could just use array
[1, [2, undef]]
but the node will not have the method associated with it (unlike new TreeNode(...)), so I'll have to go thru the list of array and instantiate the object when needed (BTW the perl example above also doesn't address this) in a later time.

Again for very large tree there'll be performance hit.

So to look at this in a different angle, is there a way to serialize an object database (in the treeview example it's just a n-way tree) in javascript (maybe thousands of node) to string and read it back later on in a efficient way? :cool:

07-14-2005, 05:27 AM
Your user name will need to be changed (admin should be getting in touch with you).

In response to your actual question, Javascript has object literals:

var myObj = {
prop: value,
method: function

So, you could try something like:

Object.prototype.serialize = function() {
var response = "{";
var compound = false;
for (var i in this) {
compound = true;
response += "\n\t" + i + ": " + this[i].serialize() + ",";
response += "\n}";
return compound? response : this.toString();

Not tested, but the general logic is there. You might want toSource() versus toString() depending on what you need. You might also need:

Array.prototype.serialize = function() {
var response = "[";
for (var i = 0; i < this.length-1; i++)
response += this[i].serialize() + ", ";
response += (this[this.length-1] || "").serialize() + "]";
return response;

To read back in, you can just eval("var myvalue = " + serializedString);

07-14-2005, 06:21 AM
thanks for the response. i do know js literal, but forget to mention in my op.

i can visualize how this looks like in a very large tree, and it's not pretty. i'm not sure if it's going to be fast either (both serializing and loading).

so the serialized output will look something like this:

function myClass_method1 (...) {...}
function myClass_method2 (...) {...}
function myClass (x,y,children) {
this.x = x;
this.y = y;
this.children = children; // an array of myClass objects
this.method1 = myClass_method1
this.method2 = myClass_method2

// serialized tree

var root = {x:1, y:2, method1:myClass_method1,method2: myClass_method2, children : [
{x:3, y:4, method1:myClass_method1,method2: myClass_method2, children : null},
{x:5, y:6, method1:myClass_method1,method2: myClass_method2, children: {recursive definitions of other object go here.......}]

maybe there's really no good way to do this in JS.

07-14-2005, 02:33 PM
... you must serialize everything to create a textual equivalent. And the JS Object literal is pretty condensed. I don't understand how you expect to do this in less space. And this isn't much different at all from the "Perl way", and you seem to accept that version as ok.