@@ -0,0 +1,2360 @@
+/**%0D%0A *   Copyright (C) 2012 Angelo Zerr %3Cangelo.zerr@gmail.com%3E and Pascal Leclercq %3Cpascal.leclercq@gmail.com%3E%0D%0A *%0D%0A *   Licensed under the Apache License, Version 2.0 (the %22License%22);%0D%0A *   you may not use this file except in compliance with the License.%0D%0A *   You may obtain a copy of the License at%0D%0A *%0D%0A *      http://www.apache.org/licenses/LICENSE-2.0%0D%0A *%0D%0A *   Unless required by applicable law or agreed to in writing, software%0D%0A *   distributed under the License is distributed on an %22AS IS%22 BASIS,%0D%0A *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0D%0A *   See the License for the specific language governing permissions and%0D%0A *   limitations under the License.%0D%0A */%0D%0Apackage com.mongodb.jee.servlet;%0D%0A%0D%0Aimport java.io.IOException;%0D%0Aimport java.io.Writer;%0D%0A%0D%0Aimport javax.servlet.http.HttpServletResponse;%0D%0A%0D%0A/**%0D%0A * Servlet Helper to serialize object to JSON with %7B@link HttpServletResponse%7D.%0D%0A * %0D%0A */%0D%0Apublic class ServletHelper %7B%0D%0A%0D%0A%09private static final String UTF_8 = %22UTF-8%22;%0D%0A%09private static final String APPLICATION_JSON = %22application/json%22;%0D%0A%0D%0A%09/**%0D%0A%09 * Serializes the given object into its JSON form and write the result in%0D%0A%09 * the given HTTP response writer.%0D%0A%09 * %0D%0A%09 * @param o%0D%0A%09 *            object to serialize.%0D%0A%09 * @param response%0D%0A%09 *            the HTTP response.%0D%0A%09 * @throws IOException%0D%0A%09 */%0D%0A%09public static void writeJson(Object o, HttpServletResponse response)%0D%0A%09%09%09throws IOException %7B%0D%0A%09%09writeJson(o, response, UTF_8);%0D%0A%09%7D%0D%0A%0D%0A%09/**%0D%0A%09 * Serializes the given object into its JSON form and write the result in%0D%0A%09 * the given HTTP response writer.%0D%0A%09 * %0D%0A%09 * @param o%0D%0A%09 *            object to serialize.%0D%0A%09 * @param response%0D%0A%09 *            the HTTP response.%0D%0A%09 * @param encoding%0D%0A%09 *            the character encoding to use for the HTTP response.%0D%0A%09 * @throws IOException%0D%0A%09 */%0D%0A%09public static void writeJson(Object o, HttpServletResponse response,%0D%0A%09%09%09String encoding) throws IOException %7B%0D%0A%09%09// HTTP response is JSON%0D%0A%09%09response.setCharacterEncoding(encoding);%0D%0A%09%09response.setContentType(APPLICATION_JSON);%0D%0A%0D%0A%09%09// Loop for DB cursor and write each DB document in the HTTP writer%0D%0A%09%09Writer writer = response.getWriter();%0D%0A%0D%0A%09%09// Serialize object as JSON and write the result in the HTTP response%0D%0A%09%09// writer.%0D%0A%09%09com.mongodb.jee.util.JSON.serialize(o, writer);%0D%0A%0D%0A%09%09writer.flush();%0D%0A%0D%0A%09%7D%0D%0A%7D%0D%0A

Add Servlet Helper to serialize object to JSON with {@link HttpServletResponse}.