openexchangerates / javascript-sandbox-console

a mini interactive javascript console for library/plugin demos and homepages
http://openexchangerates.github.io/javascript-sandbox-console/
MIT License
319 stars 58 forks source link

Display JSON instead of [object Object] #1

Closed vjeux closed 12 years ago

vjeux commented 12 years ago

Instead of

({a: 1})
  => [object Object]

That would be great to show

({a: 1})
=> {
  "a": 1
}
wjcrowcroft commented 12 years ago

Good idea - I was thinking of adding a few standard console commands, such as dir( object ) which would do an inspection of it, and could stringify JSON.

Definitely this should be an optional thing, though. If we wanted to get really smart, could definitely implement something like the way Chrome does > Object with a click-to-inspect thing.

You could also try JSON.stringify(object, 0, 4)

vjeux commented 12 years ago

Well I don't really believe that anyone would ever want to show [object Object], so a dir function might be overkill. Something like that would probably better imo:

Sandbox.display_object = function (obj) {
  var str = obj.toString();

  if (typeof obj !== 'string' && str === '[object Object]') {
    str = JSON.stringify(obj, null, 4);
  }

  // Your display code ...
};

You probably could also do the same for arrays as well.

wjcrowcroft commented 12 years ago

Yeah, that could work - I'll play around with it and see how I get on. Thanks!

wjcrowcroft commented 12 years ago

Working this into the next release. Got ~30 hours of flying to do in the next 48 but will push it up in a few days!

Thanks.

pdeschen commented 12 years ago

From my perspective, looks like this issue was resolved in commit adbe33b

If you are interested in cross-browser support, you might want to consider using Crockford's json2.js instead of relying on browser own JSON object.

Thanks

wjcrowcroft commented 12 years ago

Good call. I'll have a think about including json2 but it already has a large number of dependencies - what's more likely is including a basic stringification method of its own as part of the backbone model, like jsconsole.com does.