001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     dmetzler
016 */
017package org.nuxeo.ecm.automation.jaxrs.io;
018
019import java.io.IOException;
020import java.io.OutputStream;
021
022import org.codehaus.jackson.JsonEncoding;
023import org.codehaus.jackson.JsonFactory;
024import org.codehaus.jackson.JsonGenerator;
025import org.nuxeo.ecm.webengine.JsonFactoryManager;
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * @since 5.7.3
030 */
031public final class JsonHelper {
032
033    /**
034     * Helper method to centralize the JsonEncoding to use
035     *
036     * @param jsonFactory
037     * @param out
038     * @return
039     * @throws IOException
040     */
041    public static JsonGenerator createJsonGenerator(JsonFactory jsonFactory, OutputStream out) throws IOException {
042        return jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
043    }
044
045    /**
046     * @param out
047     * @return
048     * @throws IOException
049     */
050    public static JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
051        JsonFactory jsonFactory = Framework.getLocalService(JsonFactoryManager.class).getJsonFactory();
052        return createJsonGenerator(jsonFactory, out);
053    }
054
055}