JSON stringify revisited
July 19, 2006
Update May 17, 2008: Small sanitizer added to the toObject-method.
Now toObject() will not eval() the string if it finds any malicious code in it.
For even more security: Don't set the includeFunctions flag to true.
It's my changes to Steve's version that I'd like to share with you. Basically they stemmed from my wish to make the stringifier:
- handle and restore cyclical references
- exclude object members from Object.prototype if needed.
Download the stringifier (3 kB) -- or just try out the interactive example on this page, which pretty much show how all of this can be done.
In the example below we create a small array consisting of two objects, add a method to one of the objects, and add a new member to Object.prototype. We also set two circular references between the objects (parent and child).
This is an interactive example, by clicking the small Change this buttons you can remove the circular references and set different flags that affect JSONstring, our stringifier. You'll see the result immediately in the code comments.
// An array of people
// Peter gets his own method
// And we extend the Object.prototype just for fun
// Mary is Peter's child
// Which logically means Peter is Mary's parent
// Flags that affects the stringifier
// Now let us stringify this
// And check how converting the string back to an object works
var bTest=b+", "+b.name;
Traditional stringifiers choke on circular references, or get them wrong, creating two different objects out of one. This one doesn't.
If you want to use the restoreCirculars option to restore the circular structures you shouldn't change the structure of the JSON code with other parsers in between -- you can change values of non-objects though.
It is also nice to be able to include functions if you want to -- it doesn't really break the language independence of the JSON format either, but you might have to train JSON parsers written in other languages to ignore them.
And finally, for us slobs who occasionally extend the Object.prototype it is nice not to have to include those members in the data structure.
By the way, the stringifier part -- the make method, will work in ActionScript as well. (Just include the script in your movie with #include "jsonStringify.js"). But the toObject method with its restoration of circular structures won't. And ActionScript functions/methods can not be included in the data structure.
You will find other ActionScript stringifiers with "toObject" portions at json.org.