Providing JSON output from xPages application ... (avoiding GSON library)

David Marko  28 January 2013 09:09:46
Building the simple API for your xPages application can be very easy on Domino server if know how to do it :-). This little overview can help to serve data in JSON format from your xPages application. JSON is very easy way how to provide data to your web application. We are using it extensively for our hybrid mobile  applications built using Trigger.IO toolkit. We just have a native(hybrid) mobile (IOS and Android) application and we need Domino server to provide data we download and store into mobile device for offline availability (we use jQuery $.json here). To get this up and running the following steps are required:

1. Step ONE - create xPage as an entry point to your little API

a) create a new xpage and mark it as rendered = 'false' means xpage is not going to be rendered in standard way with HTML markup. We need our xpage to provide JSON data with proper headers ...
b) in afterRenderResponse page event we put the following code (comments available inline)  

Image:Providing JSON output from xPages application ... (avoiding GSON library)

2. Step TWO - provide JAVA code that gather your data and transform them into JSON format

Formerly we were using GSON (Google)  library to provide JAVA objects to JSON transformation. Even though its great and very popular library, in Domino environment it requires adjusting JAVA permissions for Domino server (otherwise you are getting security related errors) which requires to update XML file on file system in Notes installation folder. And this could be a real problem when you don't have access to customers server operating system or you have to maintain application running on many servers in different locations. The later was our case (app running on 9 servers in different locations - not easy to maintain such edits on file system and you still have to keep it in mind to avoid breaking the application when you reinstall the server  after some time ... ) Fortunately Domino provides similar functionality to GSON so we can use in-house solution for our purposes without adjusting the permissions ..

The following code is just a snippet of  JAVA API method that returns JSON data. The key of the concept is that we create JAVA structure representation of our database data and then transform this structure into JSON using the Domino tools ...

... comments inline ...

Image:Providing JSON output from xPages application ... (avoiding GSON library)

... hopefully someone find it usefull ...

1Toby Samples  28.01.2013 14:31:55  Providing JSON output from xPages application ... (avoiding GSON library)

Nice tip. Somewhere I have a snippet that converts a document to a Map based on the item types. I'll post it on an xsnippet and link to it here if I can find it.

2Nick Wall  28.01.2013 17:28:19  Providing JSON output from xPages application ... (avoiding GSON library)

Yup, super useful.

We use the same method as you above, accept we either just build the json, if simple structure, or for more complex "objects" we use Gson.

Just haven't got round to switching over, but now think I will.

Thanks for posting.

3Simon O’Doherty  28.01.2013 17:39:56  Providing JSON output from xPages application ... (avoiding GSON library)

Why not just use the JSON component in XPages? It's part of the Extension Library, which is now part of Notes/Domino9 built in.

Then you only need to tell the component what view/document and what fields you want to render.

4Jeff Byrd  29.01.2013 22:11:44  Providing JSON output from xPages application ... (avoiding GSON library)

Love It! I have just the place to use this. Thanks for posting.