Safe JSON parsing in XPages SSJS
Posted by mark roden on December 18, 2013
Douglas Crockford was very concerned about using eval() to convert strings to objects and so he created json2.js which can be found here https://github.com/douglascrockford/JSON-js/blob/master/json2.js. If you look at the code there is a huge RegEx in the middle of it which purposefully ensures that there is no dangerous code which the eval statement is run on. You should read the comments in the code – very insightful !
All browsers (IE8+) now support JSON.stringify or JSON.parse by default and we no longer need these functions to be added as an external library.
I found however that XPages SSJS does not seem to recognize the JSON object. So I added the json2.js code to an SSJS library and then added it as a resource within my XPage.
And with this I am now able to take a string of text – convert it safely to a JSON object and then use as the source for my repeat control.
This message was inspired by Tim’s Tripcony’s blog post.
I love the idea of storing data as a JSON string inside of a single notes document field. This would move using Lotus Notes closer and closer to a modern NoSQL system, making applications more and more portable – love that idea.
However – this renders searching by field useless – which is usually one of the requirements of an application.