27 September 2011

Java JSONWriter Domino Agent Class

If you're like me, in the coarse of your web development years with Domino you've had to create plenty of agents that need to output little bits of JSON as output to the browser for some reason or another. Since these were just bits of JSON here and there, I created the code to do this from scratch every time I made a new agent -- not the best way to do things, but it worked. I eventually got tired of that and created a small helper class for Java that simplified this process.

Simply add it to your Java agent, and then initialize it with a "JSONWriter.start()", passing "getAgentOutput()" as the sole parameter. This can be done from anywhere in your code, as long as it's before whenever you try to start printing JSON output. Just pass the string name of your JSON parameter, followed by either a string, variable, or boolean value. Close up the stream with "JSONWriter.end()" whenever you're finished.

(I have a git repo of this code at https://github.com/mdmadph/mdmjava.)
package util;

import java.io.PrintWriter;
import lotus.domino.NotesException;

/**
 * @author mdm-adph
 *
 */
public class JSONWriter {

	/**
	 * 
	 */
	private static PrintWriter printOutput = null;
	
	/**
	 * 
	 */
	private static boolean firstParam = true;
	
	/**
	 * Default constrctor.
	 * @constructor
	 */
	private JSONWriter() throws NotesException {
	}
	
	/**
	 * Initializes the JSONWriter singleton with the desired PrintWriter
	 * @param pOutput {PrintWriter}
	 * The PrintWriter object to associate with JSONWriter,
	 * usually the result of getAgentOutput()
	 */
	public static synchronized void start(PrintWriter pOutput) {
		if (printOutput == null) {
			printOutput = pOutput;
			
			printOutput.println("Content-type: application/json");
			printOutput.println("{");
		}
	}
	
	/**
	 * Internal function that prints the actual JSON information
	 * @param param {String}
	 * The string value of the JSON parameter name.
	 * @param value {String}
	 * The value of the JSON parameter.
	 */
	private static synchronized void printLine(String param, String value) {
		if (firstParam) {
			firstParam = false;
		}
		else {
			printOutput.println(",");
		}
		
		printOutput.println("\"" + param + "\":" + value);
	}
	
	/**
	 * Public interface for printLine() for string params
	 * @param param {String}
	 * The string value of the JSON parameter name.
	 * @param value {String}
	 * The value of the JSON parameter.
	 */
	public static void print(String param, String value) {
		printLine(param, "\"" + value + "\"");
	}
	
	/**
	 * Public interface for printLine for integer params
	 * @param param {String}
	 * The string value of the JSON parameter name.
	 * @param value {int}
	 * The value of the JSON parameter.
	 */
	public static void print(String param, int value) {
		printLine(param, Integer.toString(value));
	}
	
	/**
	 * Public interface for printLine for Boolean params
	 * @param param {String}
	 * The string value of the JSON parameter name.
	 * @param value {Boolean}
	 * The value of the JSON parameter.
	 */
	public static void print(String param, Boolean value) {
		printLine(param, Boolean.toString(value));
	}
	
	/**
	 * Closes the JSON stream
	 */
	public static synchronized void end() {
		printOutput.println("}");
	}
	
	@Override
	public Object clone() throws CloneNotSupportedException { 
		throw new CloneNotSupportedException(); // that'll teach 'em 
	}
}

No comments:

Post a Comment