Archive

How to convert JavaScript object to string

I have run across the problem that I want to store my JavaScript objects on the server, for session purposes. In order to do so, I need to convert the object to strong, send it to the server, and get back, eval() it, so that it becomes object again.

Here is the js function, that with little inspiration from the web, I came up with:

function objectToString(o){
    
    var parse = function(_o){
    
        var a = [], t;
        
        for(var p in _o){
        
            if(_o.hasOwnProperty(p)){
            
                t = _o[p];
                
                if(t && typeof t == "object"){
                
                    a[a.length]= p + ":{ " + arguments.callee(t).join(", ") + "}";
                    
                }
                else {
                    
                    if(typeof t == "string"){
                    
                        a[a.length] = [ p+ ": \"" + t.toString() + "\"" ];
                    }
                    else{
                        a[a.length] = [ p+ ": " + t.toString()];
                    }
                    
                }
            }
        }
        
        return a;
        
    }
    
    return "{" + parse(o).join(", ") + "}";
    
}

This is how you can test it:

console.log(objectToString({parent: {child: "value"}}));

Oh and you can through in functions (methods) to the object too, and it works!

I don't really recommend this one, but you could add this to the "Object" object as a method:

Object.prototype.toString(o){
    
    var parse = function(_o){
    
        var a = [], t;
        
        for(var p in _o){
        
            if(_o.hasOwnProperty(p)){
            
                t = _o[p];
                
                if(t && typeof t == "object"){
                
                    a[a.length]= p + ":{ " + arguments.callee(t).join(", ") + "}";
                    
                }
                else {
                    
                    if(typeof t == "string"){
                    
                        a[a.length] = [ p+ ": \"" + t.toString() + "\"" ];
                    }
                    else{
                        a[a.length] = [ p+ ": " + t.toString()];
                    }
                    
                }
            }
        }
        
        return a;
        
    }
    
    return "{" + parse(o).join(", ") + "}";
    
}

Here are some additional thoughts: for some, the idea that we are converting object to string as it is may sound too wild, so there is an option to encode the object to a JSON. This will strip out all functions, make all values "strings" (then you have to deal with stuff such as type conversion, which opens another can of worms). Such parser/encoder would be little more complicated, so I will refer you to the one ExtJs has: Ext.util.JSON.encode(). If that's what you want. Here is the ExtJs JSON encode source code.

Comments:

Rafael
2/25/2011 2:20 AM
I like it, and i used :D TY