最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "too much recursion" error when calling JSON.stringify on a large object with circular dependenci

programmeradmin3浏览0评论

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:{}};
myObject.member.child = {};
myObject.member.child.parent = myObject.member;

and try to call

JSON.stringify(myObject);

I get the error "too much recursion", not surprisingly. The "child" object has a reference to its "parent" and the parent has a reference to its child. The JSON representation does not have to be perfectly accurate, as I'm only using it for debugging, not for sending data to a server or serializing the object into a file or anything like that. Is there a way to tell JSON.stringify to just ignore certain properties (in this case the parent property of the child object), so that I would get:

{
    "member" : {
        "child" : {}
    }
}

The closest I can think of is to use getChild() and getParent() methods instead of just members, because JSON.stringify ignores any properties that are functions, but I'd rather not do that if I don't have to.

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:{}};
myObject.member.child = {};
myObject.member.child.parent = myObject.member;

and try to call

JSON.stringify(myObject);

I get the error "too much recursion", not surprisingly. The "child" object has a reference to its "parent" and the parent has a reference to its child. The JSON representation does not have to be perfectly accurate, as I'm only using it for debugging, not for sending data to a server or serializing the object into a file or anything like that. Is there a way to tell JSON.stringify to just ignore certain properties (in this case the parent property of the child object), so that I would get:

{
    "member" : {
        "child" : {}
    }
}

The closest I can think of is to use getChild() and getParent() methods instead of just members, because JSON.stringify ignores any properties that are functions, but I'd rather not do that if I don't have to.

Share Improve this question asked Oct 9, 2010 at 2:07 TylerTyler 22.1k11 gold badges65 silver badges92 bronze badges 1
  • Both of the approaches that have been offered are still giving me "too much recursion" errors -- maybe there's some other weird dependency that I'm not seeing. – Tyler Commented Dec 8, 2010 at 0:23
Add a ment  | 

2 Answers 2

Reset to default 8

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());

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));​

http://jsfiddle/feUtk/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论