|
| 1 | +/* |
| 2 | + * Copyright 2013 Netflix, 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 feign.jackson; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 19 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 20 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 21 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 22 | +import com.fasterxml.jackson.databind.Module; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 25 | +import dagger.Provides; |
| 26 | +import feign.Feign; |
| 27 | +import feign.codec.Decoder; |
| 28 | +import feign.codec.Encoder; |
| 29 | + |
| 30 | +import javax.inject.Singleton; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +/** |
| 35 | + * <h3>Custom serializers/deserializers</h3> |
| 36 | + * <br> |
| 37 | + * In order to specify custom json parsing, Jackson's {@code ObjectMapper} supports {@link JsonSerializer serializers} |
| 38 | + * and {@link JsonDeserializer deserializers}, which can be bundled together in {@link Module modules}. |
| 39 | + * <p/> |
| 40 | + * <br> |
| 41 | + * Here's an example of adding a custom module. |
| 42 | + * <p/> |
| 43 | + * <pre> |
| 44 | + * public class ObjectIdSerializer extends StdSerializer<ObjectId> { |
| 45 | + * public ObjectIdSerializer() { |
| 46 | + * super(ObjectId.class); |
| 47 | + * } |
| 48 | + * |
| 49 | + * @Override |
| 50 | + * public void serialize(ObjectId value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException { |
| 51 | + * jsonGenerator.writeString(value.toString()); |
| 52 | + * } |
| 53 | + * } |
| 54 | + * |
| 55 | + * public class ObjectIdDeserializer extends StdDeserializer<ObjectId> { |
| 56 | + * public ObjectIdDeserializer() { |
| 57 | + * super(ObjectId.class); |
| 58 | + * } |
| 59 | + * |
| 60 | + * @Override |
| 61 | + * public ObjectId deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException { |
| 62 | + * return ObjectId.massageToObjectId(jsonParser.getValueAsString()); |
| 63 | + * } |
| 64 | + * } |
| 65 | + * |
| 66 | + * public class ObjectIdModule extends SimpleModule { |
| 67 | + * public ObjectIdModule() { |
| 68 | + * // first deserializers |
| 69 | + * addDeserializer(ObjectId.class, new ObjectIdDeserializer()); |
| 70 | + * |
| 71 | + * // then serializers: |
| 72 | + * addSerializer(ObjectId.class, new ObjectIdSerializer()); |
| 73 | + * } |
| 74 | + * } |
| 75 | + * |
| 76 | + * @Provides(type = Provides.Type.SET) |
| 77 | + * Module objectIdModule() { |
| 78 | + * return new ObjectIdModule(); |
| 79 | + * } |
| 80 | + * </pre> |
| 81 | + */ |
| 82 | +@dagger.Module(injects = Feign.class, addsTo = Feign.Defaults.class) |
| 83 | +public final class JacksonModule { |
| 84 | + @Provides Encoder encoder(ObjectMapper mapper) { |
| 85 | + return new JacksonEncoder(mapper); |
| 86 | + } |
| 87 | + |
| 88 | + @Provides Decoder decoder(ObjectMapper mapper) { |
| 89 | + return new JacksonDecoder(mapper); |
| 90 | + } |
| 91 | + |
| 92 | + @Provides @Singleton ObjectMapper mapper(Set<Module> modules) { |
| 93 | + return new ObjectMapper() |
| 94 | + .setSerializationInclusion(JsonInclude.Include.NON_NULL) |
| 95 | + .configure(SerializationFeature.INDENT_OUTPUT, true) |
| 96 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 97 | + .registerModules(modules); |
| 98 | + } |
| 99 | + |
| 100 | + @Provides(type = Provides.Type.SET_VALUES) Set<Module> noDefaultModules() { |
| 101 | + return Collections.emptySet(); |
| 102 | + } |
| 103 | +} |
0 commit comments