Thursday, March 29, 2007

println 8192 chars limitation

I came across this strange (or not) limitation when writing a Domino Java agent.

Did I mentioned about json+jquery+domino new loving combination ?

So, I was designing a Java agent which is using my json.jar archive (compiled for Java 1.3 used by Domino 6 server). This agent would have the sole purpose of receiving a querystring parameter, hit a database, grab a view, grab the collection of all viewentries with a getAllEntriesByKey and returning a json string, all this part of a $.ajax call (google for jQuery).

The returned string (looked internally with Fiddler, then analysed with JuJuEdit) contained the non printable 'Enter' char each 8129 characters, thus breaking the meaning of my json String returned to the browser.

Lesson learned: do not return more than 8129 chars when designing Domino Java agent in one println statement.

sketch code before:

JSONArray jsonArray = new JSONArray();
while (viewEntry != null) {
....
JSONObject jsonObject = new JSONObject();
jsonObject.put("objAttr", objValue);
......
jsonArray.put(jsonObject);
jsonObject = null;
viewEntry = viewCollection.getNextEntry(viewEntry);
}
PrintWriter toBrowser.println (jsonArray)


as you see, I'm builing an array of jSon objects (jsonArray), which is thrown away in one println statement outside of the loop. Ntz, not good. If jsonArray contains more than the limit, the meaning of it is breaking.

Instead, I am now doing this:


toBrowser.println("["); //open the array by hand, drop jsonArray variable
while (viewEntry != null) {
....
JSONObject jsonObject = new JSONObject();
jsonObject.put("objAttr", objValue);
......
toBrowser.println(jsonObject + ",");
jsonObject = null;
viewEntry = viewCollection.getNextEntry(viewEntry);
}
toBrowser.println("]");


Now it's more like it: I am throwing back to browser each of the jsonObject, making sure to respect the jSon syntax (add a comma in the string).

One quick catch: last useful object in this string will contain a ',' which means in fact last jSon Object is null. So make sure in the browser, after the eval() compilation, when you loop the results, make the loop as:
for (var i=0 ; i < resultsObj.length-1 ; i ++)

If any guru will be reading this I'm hoping to hear more details about this limit. Is it of Java println ? Is it of Domino Java ? Am I too stupid for not knowing this before hand ?

No comments: