|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.converter.json; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStreamReader; |
| 21 | +import java.io.OutputStreamWriter; |
| 22 | +import java.io.Reader; |
| 23 | +import java.lang.reflect.Type; |
| 24 | +import java.nio.charset.Charset; |
| 25 | + |
| 26 | +import org.springframework.http.HttpHeaders; |
| 27 | +import org.springframework.http.HttpInputMessage; |
| 28 | +import org.springframework.http.HttpOutputMessage; |
| 29 | +import org.springframework.http.MediaType; |
| 30 | +import org.springframework.http.converter.AbstractHttpMessageConverter; |
| 31 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 32 | +import org.springframework.http.converter.HttpMessageNotWritableException; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +import com.google.gson.Gson; |
| 36 | +import com.google.gson.GsonBuilder; |
| 37 | +import com.google.gson.JsonIOException; |
| 38 | +import com.google.gson.JsonParseException; |
| 39 | +import com.google.gson.JsonSyntaxException; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Roy Clarkson |
| 43 | + * @since 1.0 |
| 44 | + */ |
| 45 | +public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> |
| 46 | +{ |
| 47 | + |
| 48 | + public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); |
| 49 | + |
| 50 | + private Gson gson; |
| 51 | + |
| 52 | + private Type type = null; |
| 53 | + |
| 54 | + private boolean prefixJson = false; |
| 55 | + |
| 56 | + /** |
| 57 | + * Construct a new {@code GsonHttpMessageConverter} with a default |
| 58 | + * {@link Gson#Gson() Gson}. |
| 59 | + */ |
| 60 | + public GsonHttpMessageConverter() |
| 61 | + { |
| 62 | + this(new Gson()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Construct a new {@code GsonHttpMessageConverter}. |
| 67 | + * |
| 68 | + * @param serializeNulls true to generate json for null values |
| 69 | + */ |
| 70 | + public GsonHttpMessageConverter(boolean serializeNulls) |
| 71 | + { |
| 72 | + this(serializeNulls ? new GsonBuilder().serializeNulls().create() : new Gson()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Construct a new {@code GsonHttpMessageConverter}. |
| 77 | + * |
| 78 | + * @param gson a customized {@link Gson#Gson() Gson} |
| 79 | + */ |
| 80 | + public GsonHttpMessageConverter(Gson gson) |
| 81 | + { |
| 82 | + super(new MediaType("application", "json", DEFAULT_CHARSET)); |
| 83 | + setGson(gson); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Sets the {@code Gson} for this view. If not set, a default |
| 88 | + * {@link Gson#Gson() Gson} is used. |
| 89 | + * <p> |
| 90 | + * Setting a custom-configured {@code Gson} is one way to take further control |
| 91 | + * of the JSON serialization process. |
| 92 | + * |
| 93 | + * @throws IllegalArgumentException if gson is null |
| 94 | + */ |
| 95 | + public void setGson(Gson gson) |
| 96 | + { |
| 97 | + Assert.notNull(gson, "'gson' must not be null"); |
| 98 | + this.gson = gson; |
| 99 | + } |
| 100 | + |
| 101 | + public void setType(Type type) |
| 102 | + { |
| 103 | + this.type = type; |
| 104 | + } |
| 105 | + |
| 106 | + public Type getType() |
| 107 | + { |
| 108 | + return type; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Indicates whether the JSON output by this view should be prefixed with |
| 113 | + * "{} &&". Default is false. |
| 114 | + * <p> |
| 115 | + * Prefixing the JSON string in this manner is used to help prevent JSON |
| 116 | + * Hijacking. The prefix renders the string syntactically invalid as a script |
| 117 | + * so that it cannot be hijacked. This prefix does not affect the evaluation |
| 118 | + * of JSON, but if JSON validation is performed on the string, the prefix |
| 119 | + * would need to be ignored. |
| 120 | + */ |
| 121 | + public void setPrefixJson(boolean prefixJson) |
| 122 | + { |
| 123 | + this.prefixJson = prefixJson; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean canRead(Class<?> clazz, MediaType mediaType) |
| 128 | + { |
| 129 | + return canRead(mediaType); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean canWrite(Class<?> clazz, MediaType mediaType) |
| 134 | + { |
| 135 | + return canWrite(mediaType); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + protected boolean supports(Class<?> clazz) |
| 140 | + { |
| 141 | + // should not be called, since we override canRead/Write instead |
| 142 | + throw new UnsupportedOperationException(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) |
| 147 | + throws IOException, HttpMessageNotReadableException |
| 148 | + { |
| 149 | + |
| 150 | + Reader json = new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders())); |
| 151 | + |
| 152 | + try |
| 153 | + { |
| 154 | + Type typeOfT = getType(); |
| 155 | + if (typeOfT != null) |
| 156 | + { |
| 157 | + return this.gson.fromJson(json, typeOfT); |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + return this.gson.fromJson(json, clazz); |
| 162 | + } |
| 163 | + } |
| 164 | + catch (JsonSyntaxException ex) |
| 165 | + { |
| 166 | + throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); |
| 167 | + } |
| 168 | + catch (JsonIOException ex) |
| 169 | + { |
| 170 | + throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); |
| 171 | + } |
| 172 | + catch (JsonParseException ex) |
| 173 | + { |
| 174 | + throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + protected void writeInternal(Object o, HttpOutputMessage outputMessage) |
| 180 | + throws IOException, HttpMessageNotWritableException |
| 181 | + { |
| 182 | + |
| 183 | + OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), getCharset(outputMessage.getHeaders())); |
| 184 | + |
| 185 | + try |
| 186 | + { |
| 187 | + if (this.prefixJson) |
| 188 | + { |
| 189 | + writer.append("{} && "); |
| 190 | + } |
| 191 | + Type typeOfSrc = getType(); |
| 192 | + if (typeOfSrc != null) |
| 193 | + { |
| 194 | + this.gson.toJson(o, typeOfSrc, writer); |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + this.gson.toJson(o, writer); |
| 199 | + } |
| 200 | + writer.close(); |
| 201 | + } |
| 202 | + catch (JsonIOException ex) |
| 203 | + { |
| 204 | + throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // helpers |
| 209 | + |
| 210 | + private Charset getCharset(HttpHeaders headers) |
| 211 | + { |
| 212 | + if (headers != null && headers.getContentType() != null |
| 213 | + && headers.getContentType().getCharSet() != null) |
| 214 | + { |
| 215 | + return headers.getContentType().getCharSet(); |
| 216 | + } |
| 217 | + return DEFAULT_CHARSET; |
| 218 | + } |
| 219 | + |
| 220 | +} |
0 commit comments