We use GSON here. It's really simple to use and satisfies all our needs, we have basically the following, nothing more is needed:
/**
* Write given Java object as JSON to the output of the current response.
* @param object Any Java Object to be written as JSON to the output of the current response.
* @throws IOException If something fails at IO level.
*/
public void writeJson(Object object) throws IOException {
String json = new Gson().toJson(object);
this.response.setContentType("application/json");
this.response.setCharacterEncoding("UTF-8");
Writer writer = null;
try {
writer = this.response.getWriter();
writer.write(json);
} finally {
close(writer);
}
}
Our choice for GSON was based on the fact that its ideology fits our requirements, especially the full support of generic types. Besides that all it's also very performant.