|
15 | 15 | */
|
16 | 16 | package retrofit;
|
17 | 17 |
|
18 |
| -import com.google.gson.Gson; |
| 18 | +import com.google.gson.TypeAdapter; |
19 | 19 | import com.squareup.okhttp.MediaType;
|
20 | 20 | import com.squareup.okhttp.RequestBody;
|
21 | 21 | import com.squareup.okhttp.ResponseBody;
|
22 | 22 | import java.io.IOException;
|
23 |
| -import java.io.InputStream; |
24 |
| -import java.io.InputStreamReader; |
25 |
| -import java.lang.reflect.Type; |
| 23 | +import java.io.OutputStreamWriter; |
| 24 | +import java.io.Reader; |
| 25 | +import java.io.Writer; |
26 | 26 | import java.nio.charset.Charset;
|
| 27 | +import okio.Buffer; |
27 | 28 |
|
28 |
| -/** |
29 |
| - * A {@link Converter} which uses GSON for serialization and deserialization of entities. |
30 |
| - */ |
31 |
| -public class GsonConverter implements Converter { |
32 |
| - private final Gson gson; |
33 |
| - private final Charset charset; |
34 |
| - private final MediaType mediaType; |
35 |
| - |
36 |
| - /** |
37 |
| - * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and |
38 |
| - * decoding from JSON (when no charset is specified by a header) will use UTF-8. |
39 |
| - */ |
40 |
| - public GsonConverter() { |
41 |
| - this(new Gson()); |
42 |
| - } |
| 29 | +final class GsonConverter<T> implements Converter<T> { |
| 30 | + private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8"); |
| 31 | + private static final Charset UTF_8 = Charset.forName("UTF-8"); |
43 | 32 |
|
44 |
| - /** |
45 |
| - * Create an instance using the supplied {@link Gson} object for conversion. Encoding to JSON and |
46 |
| - * decoding from JSON (when no charset is specified by a header) will use UTF-8. |
47 |
| - */ |
48 |
| - public GsonConverter(Gson gson) { |
49 |
| - this(gson, Charset.forName("UTF-8")); |
50 |
| - } |
| 33 | + private final TypeAdapter<T> typeAdapter; |
51 | 34 |
|
52 |
| - /** |
53 |
| - * Create an instance using the supplied {@link Gson} object for conversion. Encoding to JSON and |
54 |
| - * decoding from JSON (when no charset is specified by a header) will use the specified charset. |
55 |
| - */ |
56 |
| - public GsonConverter(Gson gson, Charset charset) { |
57 |
| - if (gson == null) throw new NullPointerException("gson == null"); |
58 |
| - if (charset == null) throw new NullPointerException("charset == null"); |
59 |
| - this.gson = gson; |
60 |
| - this.charset = charset; |
61 |
| - this.mediaType = MediaType.parse("application/json; charset=" + charset.name()); |
| 35 | + GsonConverter(TypeAdapter<T> typeAdapter) { |
| 36 | + this.typeAdapter = typeAdapter; |
62 | 37 | }
|
63 | 38 |
|
64 |
| - @Override public Object fromBody(ResponseBody body, Type type) throws IOException { |
65 |
| - Charset charset = this.charset; |
66 |
| - if (body.contentType() != null) { |
67 |
| - charset = body.contentType().charset(charset); |
68 |
| - } |
69 |
| - |
70 |
| - InputStream is = body.byteStream(); |
| 39 | + @Override public T fromBody(ResponseBody body) throws IOException { |
| 40 | + Reader in = body.charStream(); |
71 | 41 | try {
|
72 |
| - return gson.fromJson(new InputStreamReader(is, charset), type); |
| 42 | + return typeAdapter.fromJson(in); |
73 | 43 | } finally {
|
74 | 44 | try {
|
75 |
| - is.close(); |
| 45 | + in.close(); |
76 | 46 | } catch (IOException ignored) {
|
77 | 47 | }
|
78 | 48 | }
|
79 | 49 | }
|
80 | 50 |
|
81 |
| - @Override public RequestBody toBody(Object object, Type type) { |
82 |
| - String json = gson.toJson(object, type); |
83 |
| - return RequestBody.create(mediaType, json); |
| 51 | + @Override public RequestBody toBody(T value) { |
| 52 | + Buffer buffer = new Buffer(); |
| 53 | + Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); |
| 54 | + try { |
| 55 | + typeAdapter.toJson(writer, value); |
| 56 | + writer.flush(); |
| 57 | + } catch (IOException e) { |
| 58 | + throw new AssertionError(e); // Writing to Buffer does no I/O. |
| 59 | + } |
| 60 | + return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); |
84 | 61 | }
|
85 | 62 | }
|
0 commit comments