001/*
002 * Copyright 2002-2020 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.json;
018
019import java.io.Reader;
020import java.io.Writer;
021import java.lang.reflect.ParameterizedType;
022import java.lang.reflect.Type;
023
024import com.google.gson.Gson;
025
026import org.springframework.lang.Nullable;
027import org.springframework.util.Assert;
028
029/**
030 * Implementation of {@link org.springframework.http.converter.HttpMessageConverter}
031 * that can read and write JSON using the
032 * <a href="https://code.google.com/p/google-gson/">Google Gson</a> library.
033 *
034 * <p>This converter can be used to bind to typed beans or untyped {@code HashMap}s.
035 * By default, it supports {@code application/json} and {@code application/*+json} with
036 * {@code UTF-8} character set.
037 *
038 * <p>Tested against Gson 2.8; compatible with Gson 2.0 and higher.
039 *
040 * @author Roy Clarkson
041 * @author Juergen Hoeller
042 * @since 4.1
043 * @see com.google.gson.Gson
044 * @see com.google.gson.GsonBuilder
045 * @see #setGson
046 */
047public class GsonHttpMessageConverter extends AbstractJsonHttpMessageConverter {
048
049        private Gson gson;
050
051
052        /**
053         * Construct a new {@code GsonHttpMessageConverter} with default configuration.
054         */
055        public GsonHttpMessageConverter() {
056                this.gson = new Gson();
057        }
058
059        /**
060         * Construct a new {@code GsonHttpMessageConverter} with the given delegate.
061         * @param gson the Gson instance to use
062         * @since 5.0
063         */
064        public GsonHttpMessageConverter(Gson gson) {
065                Assert.notNull(gson, "A Gson instance is required");
066                this.gson = gson;
067        }
068
069
070        /**
071         * Set the {@code Gson} instance to use.
072         * If not set, a default {@link Gson#Gson() Gson} instance will be used.
073         * <p>Setting a custom-configured {@code Gson} is one way to take further
074         * control of the JSON serialization process.
075         * @see #GsonHttpMessageConverter(Gson)
076         */
077        public void setGson(Gson gson) {
078                Assert.notNull(gson, "A Gson instance is required");
079                this.gson = gson;
080        }
081
082        /**
083         * Return the configured {@code Gson} instance for this converter.
084         */
085        public Gson getGson() {
086                return this.gson;
087        }
088
089
090        @Override
091        protected Object readInternal(Type resolvedType, Reader reader) throws Exception {
092                return getGson().fromJson(reader, resolvedType);
093        }
094
095        @Override
096        protected void writeInternal(Object object, @Nullable Type type, Writer writer) throws Exception {
097                // In Gson, toJson with a type argument will exclusively use that given type,
098                // ignoring the actual type of the object... which might be more specific,
099                // e.g. a subclass of the specified type which includes additional fields.
100                // As a consequence, we're only passing in parameterized type declarations
101                // which might contain extra generics that the object instance doesn't retain.
102                if (type instanceof ParameterizedType) {
103                        getGson().toJson(object, type, writer);
104                }
105                else {
106                        getGson().toJson(object, writer);
107                }
108        }
109
110}