There are a couple of ways to do this online, but having a Java agent to do view JSON gave me a good opportunity to do it my own way. I have a couple things going for me at the moment - I don’t need rich text (yet), and I don’t need to be able to create or update documents (yet).
The ‘Document’ class in Java has a function called generateXML which has been around for a long time (5.0.3 according to the doc) so it should be relatively stable, but as with all things Domino… we shall see if it is in practice. That got me the DXL version of a Notes document. To get the JSON I went to github and got the org.json classes and imported them into a Java script library in Domino:
This has a lot of really handy classes in it, but what I really wanted was the XML class that has a toJSONObject method. With all these pieces, it was really easy to put the following method together:
private void writeDocument(Database db, String documentId, boolean asJSON) throws NotesException, IOException, JSONException {
Document doc = db.getDocumentByID(documentId);
if( null== doc){
doc = db.getDocumentByUNID(documentId);
}
if( null== doc){
System.out.println("document not found");
return;
}
// write the document out
java.io.Writer sw = new java.io.StringWriter();
doc.generateXML(sw);
sw.flush();
PrintWriter ao = getAgentOutput();
if( asJSON ){
ao.println("Content-type: application/json");
ao.print(XML.toJSONObject(sw.toString()).toString());
}
else{
ao.println("Content-type: text/xml");
ao.print(sw.toString());
}
ao.flush();
}
I threw that into the dataProxy agent and added 2 URL parameters:
- documentId= Lets me specify the document I want to access
- outputformat=JSON Omitting this parameter keeps the output in DXL.
That’s it! With the next post I’ll show you how to integrate this into the mobile app.