Posts

Showing posts with the label stringify

JSON.stringify Recursion + Max Execution Stack Exceeded

I believe this is a common problem, and we had a similar one today while debugging. JSON methods do not support recursion ... which is the only thing I am really missing back to PHP serialize days. Recursion Is Bad Well, I would say cyclic references are never that good but sometimes these may happen and, specially while testing and debugging, it's more than useful to understand what happened there. If you have cyclic/cross references in your code I suggest you to use approaches which aim is to avoid these kind of direct links. Harmony Collections , specially Map and WeakMap, are indeed good helpers to reference indirectly objects without creating, hopefully, first level links and/or recursions. How To Serialize Anyway JSON.stringify() accepts a second argument called replacer . I won't explain more than MDN about its potentials, but it can be really handy to avoid recursions. A simple way to do it is indeed to store in a stack already parsed objects, included the object itse