Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  171] [ 2]  / answers: 1 / hits: 173730  / 13 Years ago, tue, february 21, 2012, 12:00:00

I have an object (parse tree) that contains child nodes which are references to other nodes.



I'd like to serialize this object, using JSON.stringify(), but I get




TypeError: cyclic object value




because of the constructs I mentioned.



How could I work around this? It does not matter to me whether these references to other nodes are represented or not in the serialized object.



On the other hand, removing these properties from the object when they are being created seems tedious and I wouldn't want to make changes to the parser (narcissus).


More From » json

 Answers
3

Use the second parameter of stringify, the replacer function, to exclude already serialized objects:



var seen = [];

JSON.stringify(obj, function(key, val) {
if (val != null && typeof val == object) {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
}
return val;
});


http://jsfiddle.net/mH6cJ/38/



As correctly pointed out in other comments, this code removes every seen object, not only recursive ones.



For example, for:



a = {x:1};
obj = [a, a];


the result will be incorrect. If your structure is like this, you might want to use Crockford's decycle or this (simpler) function which just replaces recursive references with nulls:





function decycle(obj, stack = []) {
if (!obj || typeof obj !== 'object')
return obj;

if (stack.includes(obj))
return null;

let s = stack.concat([obj]);

return Array.isArray(obj)
? obj.map(x => decycle(x, s))
: Object.fromEntries(
Object.entries(obj)
.map(([k, v]) => [k, decycle(v, s)]));
}

//

let a = {b: [1, 2, 3]}
a.b.push(a);

console.log(JSON.stringify(decycle(a)))




[#87314] Monday, February 20, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sonja

Total Points: 541
Total Questions: 113
Total Answers: 114

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
sonja questions
Mon, Nov 30, 20, 00:00, 4 Years ago
Sun, Oct 11, 20, 00:00, 4 Years ago
Thu, May 21, 20, 00:00, 4 Years ago
Sun, Nov 10, 19, 00:00, 5 Years ago
Mon, Aug 26, 19, 00:00, 5 Years ago
;