For debugging I want to serialize javascript objects with JSON.stringify(myobject). But this gives:
TypeError: Converting circular structure to JSON
<
You can now use Douglas Crockford's JSON Stringify plugin:
https://github.com/douglascrockford/JSON-js
This has a decycle option in the download file cycle.js
. You could also use console.log()
and inspect the JSON in your browsers console.
JSON.stringify(obj) does not support circular referencing such as:
var car = {}
car.myself = car;
JSON.stringify(car);
However dojox.json.ref does support circular referencing, if you wanted to explore another option.
However if your purposes are strictly to debug, I'd suggest using the built in browser debugger such as Chrome's, IE's or Firebug(for firefox).
For node.js json-ref is a nice lightweight alternative to the dojox.json.ref function suggested by Mike Lewis.
You can use console.log() and the chrome javascript debug console, which will happily let you inspect your object even if it has cyclic references.