Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Representing serialized object in javascript

    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?

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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:
    Code:
    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:
    Code:
    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);
    Last edited by jkd; 07-14-2005 at 04:33 AM.

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    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.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    ... 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •