To work in Java with JSON there are several libraries, like GSON, JSON-simple and even the IBM version. They are very easy to use.
In my recent J2EE projects their was always a need of consuming JSON and returning JSON. The library we used their was Jackson JSON library.
It was a refreshing way to work with JSON without building or consuming it line by line. Jackson is mapping JSON directly to POJO. So with one line of code the JSON is transformed to a real Java Object and can be used all over your project.
[dropshadowbox align=”none” effect=”lifted-both” width=”400px” height=”” background_color=”#ffffff” border_width=”1″ border_color=”#dddddd” ]
private ObjectMapper mapper = new ObjectMapper();
mapper.readValue(data, Message.class)[/dropshadowbox]
The other way around it also very easy, write your POJO directly to the output writer, like a File or mostly the response of a servlet, which we used in our projects.
[dropshadowbox align=”none” effect=”lifted-both” width=”400px” height=”” background_color=”#ffffff” border_width=”1″ border_color=”#dddddd” ]
mapper.writeValue(response.getWriter(), message);
[/dropshadowbox]
Where message is a POJO, and data is JSON as String
Inside a NSF
I was thinking to make it available to a XAgent. First I included the 3 libraries the lib directory under WEB-CONTENT inside my NSF and add them to the build path.
Immediately I a was able to use the code in my Java class. So it worked, but…. if I want to use it in another NSF I need to perform the above steps.
OSGi plugin
There started the idea to put everything in an OSGi plugin, so all the NSF’s on the Domino server can benefit from it.
I put everything in a plugin project, thanks to the great blog of John Dalsgaard,
Created a feature and update site and put everything on a Bitbucket repository, so everyone can use it.
Security
To get everything working you need to add a custom java policy file in side the <domino directory>jvm/lib/security. You can use the java.pol file who is inside the repository. The benefit of using java.pol is that after an upgrade or what ever, the java.pol file is not affected.
Have fun with it…
Great stuff. We use Jackson and have the files inside our NSF, this will help a lot with other projects, thanks!
Hi Frank,
can you share some sample code how the consumer class could look like?
Hi frank,
I am learning the Jackson library and it’s capabilities. Do you have any code to share how you can use it with XPages? E.g. I am looking for a solution to provide an infinite scroll functionality in combination with Domino Access Services.
Any tip is helpful (and appreciated) =)
What kind of code you are looking for?