|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Square, Inc. |
| 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 | +package retrofit2.converter.jackson3; |
| 17 | + |
| 18 | +import java.lang.annotation.Annotation; |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import okhttp3.MediaType; |
| 21 | +import okhttp3.RequestBody; |
| 22 | +import okhttp3.ResponseBody; |
| 23 | +import retrofit2.Call; |
| 24 | +import retrofit2.Converter; |
| 25 | +import retrofit2.Retrofit; |
| 26 | +import tools.jackson.databind.JavaType; |
| 27 | +import tools.jackson.databind.ObjectMapper; |
| 28 | +import tools.jackson.databind.ObjectReader; |
| 29 | +import tools.jackson.databind.ObjectWriter; |
| 30 | + |
| 31 | +/** |
| 32 | + * A {@linkplain Converter.Factory converter} which uses Jackson. |
| 33 | + * |
| 34 | + * <p>Because Jackson is so flexible in the types it supports, this converter assumes that it can |
| 35 | + * handle all types. If you are mixing JSON serialization with something else (such as protocol |
| 36 | + * buffers), you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this |
| 37 | + * instance} last to allow the other converters a chance to see their types. |
| 38 | + */ |
| 39 | +public final class JacksonConverterFactory extends Converter.Factory { |
| 40 | + private static final MediaType DEFAULT_MEDIA_TYPE = |
| 41 | + MediaType.get("application/json; charset=UTF-8"); |
| 42 | + |
| 43 | + /** Create an instance using a default {@link ObjectMapper} instance for conversion. */ |
| 44 | + public static JacksonConverterFactory create() { |
| 45 | + return new JacksonConverterFactory(new ObjectMapper(), DEFAULT_MEDIA_TYPE, false); |
| 46 | + } |
| 47 | + |
| 48 | + /** Create an instance using {@code mapper} for conversion. */ |
| 49 | + public static JacksonConverterFactory create(ObjectMapper mapper) { |
| 50 | + return create(mapper, DEFAULT_MEDIA_TYPE); |
| 51 | + } |
| 52 | + |
| 53 | + /** Create an instance using {@code mapper} and {@code mediaType} for conversion. */ |
| 54 | + @SuppressWarnings("ConstantConditions") // Guarding public API nullability. |
| 55 | + public static JacksonConverterFactory create(ObjectMapper mapper, MediaType mediaType) { |
| 56 | + if (mapper == null) throw new NullPointerException("mapper == null"); |
| 57 | + if (mediaType == null) throw new NullPointerException("mediaType == null"); |
| 58 | + return new JacksonConverterFactory(mapper, mediaType, false); |
| 59 | + } |
| 60 | + |
| 61 | + private final ObjectMapper mapper; |
| 62 | + private final MediaType mediaType; |
| 63 | + private final boolean streaming; |
| 64 | + |
| 65 | + private JacksonConverterFactory(ObjectMapper mapper, MediaType mediaType, boolean streaming) { |
| 66 | + this.mapper = mapper; |
| 67 | + this.mediaType = mediaType; |
| 68 | + this.streaming = streaming; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Return a new factory which streams serialization of request messages to bytes on the HTTP thread |
| 73 | + * This is either the calling thread for {@link Call#execute()}, or one of OkHttp's background |
| 74 | + * threads for {@link Call#enqueue}. Response bytes are always converted to message instances on |
| 75 | + * one of OkHttp's background threads. |
| 76 | + */ |
| 77 | + public JacksonConverterFactory withStreaming() { |
| 78 | + return new JacksonConverterFactory(mapper, mediaType, true); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Converter<ResponseBody, ?> responseBodyConverter( |
| 83 | + Type type, Annotation[] annotations, Retrofit retrofit) { |
| 84 | + JavaType javaType = mapper.getTypeFactory().constructType(type); |
| 85 | + ObjectReader reader = mapper.readerFor(javaType); |
| 86 | + return new JacksonResponseBodyConverter<>(reader); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Converter<?, RequestBody> requestBodyConverter( |
| 91 | + Type type, |
| 92 | + Annotation[] parameterAnnotations, |
| 93 | + Annotation[] methodAnnotations, |
| 94 | + Retrofit retrofit) { |
| 95 | + JavaType javaType = mapper.getTypeFactory().constructType(type); |
| 96 | + ObjectWriter writer = mapper.writerFor(javaType); |
| 97 | + return new JacksonRequestBodyConverter<>(writer, mediaType, streaming); |
| 98 | + } |
| 99 | +} |
0 commit comments