"too Much Recursion" Error When Calling Json.stringify On A Large Object With Circular Dependencies
I have an object that contains circular references, and I would like to look at the JSON representation of it. For example, if I build this object: var myObject = {member:{}}; myOb
Solution 1:
You can pass a function as the second argument to stringify. This function receives as arguments the key and value of the member to stringify. If this function returns undefined, the member will be ignored.
alert(JSON.stringify(myObject, function(k, v) {
return (k === 'member') ? undefined : v;
}));
...or use e.g. firebug or use the toSource()-method, if you only want to see whats inside the object.
alert(myObject.toSource());
Solution 2:
From the crockford implementation (which follows the ECMA specification):
If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation.
Then something like this should work just fine:
var myObject =
{
member: { child: {} }
}
myObject.member.child.parent = myObject.member;
myObject.member.child.toJSON = function ()
{
return'no more recursion for you.';
};
console.log(JSON.stringify(myObject));
Post a Comment for ""too Much Recursion" Error When Calling Json.stringify On A Large Object With Circular Dependencies"