001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.http.converter.smile;
018
019import com.fasterxml.jackson.databind.ObjectMapper;
020import com.fasterxml.jackson.dataformat.smile.SmileFactory;
021
022import org.springframework.http.MediaType;
023import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
024import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
025import org.springframework.util.Assert;
026
027/**
028 * Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
029 * that can read and write Smile data format ("binary JSON") using
030 * <a href="https://github.com/FasterXML/jackson-dataformats-binary/tree/master/smile">
031 * the dedicated Jackson 2.x extension</a>.
032 *
033 * <p>By default, this converter supports {@code "application/x-jackson-smile"} media type.
034 * This can be overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
035 *
036 * <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
037 *
038 * <p>Compatible with Jackson 2.9 and higher.
039 *
040 * @author Sebastien Deleuze
041 * @since 5.0
042 */
043public class MappingJackson2SmileHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
044
045        /**
046         * Construct a new {@code MappingJackson2SmileHttpMessageConverter} using default configuration
047         * provided by {@code Jackson2ObjectMapperBuilder}.
048         */
049        public MappingJackson2SmileHttpMessageConverter() {
050                this(Jackson2ObjectMapperBuilder.smile().build());
051        }
052
053        /**
054         * Construct a new {@code MappingJackson2SmileHttpMessageConverter} with a custom {@link ObjectMapper}
055         * (must be configured with a {@code SmileFactory} instance).
056         * You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
057         * @see Jackson2ObjectMapperBuilder#smile()
058         */
059        public MappingJackson2SmileHttpMessageConverter(ObjectMapper objectMapper) {
060                super(objectMapper, new MediaType("application", "x-jackson-smile"));
061                Assert.isInstanceOf(SmileFactory.class, objectMapper.getFactory(), "SmileFactory required");
062        }
063
064
065        /**
066         * {@inheritDoc}
067         * The {@code ObjectMapper} must be configured with a {@code SmileFactory} instance.
068         */
069        @Override
070        public void setObjectMapper(ObjectMapper objectMapper) {
071                Assert.isInstanceOf(SmileFactory.class, objectMapper.getFactory(), "SmileFactory required");
072                super.setObjectMapper(objectMapper);
073        }
074
075}