My desire is a “safe” way to encode a given object to JSON. This is for a logging any abstract object in Node.js but can apply to other needs as well. My research pointed me to util.inspect in Node, but this isn’t valid JSON, and my desire is to have valid JSON that I can re-hydrate later. Because some Error instances can have recursion issues, I needed to limit the response. Also, in some cases forward and back-references to objects can be an issue as well, so I want to avoid duplicate entries.
Share
NEW ANSWER
Today, in case anyone is looking at this, I would just use fclone which at this point is much more thorough than my original.
Original Answer:
I’m answering my own question below, as this was a bit difficult to come up with a working solution. It’s the combination of a few solutions I’ve found, and seems to work well.
There is some extra logic for dealing with objects that are errors, and making sure to get necessary data from said errors, and from a given object’s prototype. functions are expressly ignored in the response. When recursion goes too deep, it will return undefined for a given object.