Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function toJSONWithFuncs(obj) {
      Object.prototype.toJSON = function() {
        var sobj = {}, i;
        for (i in this) 
          if (this.hasOwnProperty(i))
            sobj[i] = typeof this[i] == 'function' ?
              this[i].toString() : this[i];
  
        return sobj;
      };
     Array.prototype.toJSON = function() {
        var sarr = [], i;
        for (i=0 ; i<this.length; i++) 
            sarr.push(typeof this[i] == 'function' ? this[i].toString() : this[i]);
  
        return sarr;
      };
      var str = JSON.stringify(obj);
      delete Object.prototype.toJSON;
      return str;
    }
  
var dude = { 
  zomg: 'wtf', 
  d: {
    srsly: '444', 
    func: function(hai) { return 5+5; } 
  }, 
  f: function() { alert('dude'); }, 
  z: 'sss',
  x: ['Hola', 'amigo', function(){}]
};
    
alert('With Functions:\n' +toJSONWithFuncs(dude));
alert('Stock JSON:\n' + JSON.stringify(dude));
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers