001    /* 
002     * JBoss, Home of Professional Open Source 
003     * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
004     * as indicated by the @author tags. All rights reserved. 
005     * See the copyright.txt in the distribution for a 
006     * full listing of individual contributors.
007     *
008     * This copyrighted material is made available to anyone wishing to use, 
009     * modify, copy, or redistribute it subject to the terms and conditions 
010     * of the GNU Lesser General Public License, v. 2.1. 
011     * This program is distributed in the hope that it will be useful, but WITHOUT A 
012     * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
013     * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details. 
014     * You should have received a copy of the GNU Lesser General Public License, 
015     * v.2.1 along with this distribution; if not, write to the Free Software 
016     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
017     * MA  02110-1301, USA.
018     */
019    package org.switchyard.serial.jackson.format;
020    
021    import java.io.BufferedOutputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    
026    import org.codehaus.jackson.JsonFactory;
027    import org.codehaus.jackson.JsonGenerator;
028    import org.codehaus.jackson.JsonParser;
029    import org.codehaus.jackson.map.ObjectMapper;
030    import org.codehaus.jackson.map.ObjectWriter;
031    import org.switchyard.common.io.CountingOutputStream;
032    import org.switchyard.serial.BaseSerializer;
033    import org.switchyard.serial.FormatType;
034    
035    /**
036     * A Jackson serializer that performs {@link FormatType.JSON} serialization/deserialization.
037     *
038     * @author David Ward &lt;<a href="mailto:dward@jboss.org">dward@jboss.org</a>&gt; &copy; 2012 Red Hat Inc.
039     */
040    public final class JSONJacksonSerializer extends BaseSerializer {
041    
042        private static final ObjectMapper OBJECT_MAPPER;
043        static {
044            JsonFactory jsonFactory = new JsonFactory();
045            jsonFactory.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
046            jsonFactory.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
047            OBJECT_MAPPER = new ObjectMapper(jsonFactory);
048            OBJECT_MAPPER.enableDefaultTyping();
049        }
050    
051        /**
052         * Default constructor.
053         */
054        public JSONJacksonSerializer() {
055            super(FormatType.JSON);
056        }
057    
058        /**
059         * {@inheritDoc}
060         */
061        @Override
062        public <T> int serialize(T obj, Class<T> type, OutputStream out) throws IOException {
063            out = new CountingOutputStream(new BufferedOutputStream(out, getBufferSize()));
064            try {
065                ObjectWriter writer = OBJECT_MAPPER.writerWithType(type);
066                if (isPrettyPrint()) {
067                    writer = writer.withDefaultPrettyPrinter();
068                }
069                writer.writeValue(out, obj);
070            } finally {
071                if (isCloseEnabled()) {
072                    out.close();
073                }
074            }
075            return ((CountingOutputStream)out).getCount();
076        }
077    
078        /**
079         * {@inheritDoc}
080         */
081        @Override
082        public <T> T deserialize(InputStream in, Class<T> type) throws IOException {
083            T obj;
084            try {
085                obj = OBJECT_MAPPER.readValue(in, type);
086            } finally {
087                if (isCloseEnabled()) {
088                    in.close();
089                }
090            }
091            return obj;
092        }
093    
094    }