| 30 | import tools.jackson.databind.json.JsonMapper; |
| 31 | |
| 32 | public class Jackson3Decoder implements Decoder, JsonDecoder { |
| 33 | |
| 34 | private final JsonMapper mapper; |
| 35 | |
| 36 | public Jackson3Decoder() { |
| 37 | this(Collections.<JacksonModule>emptyList()); |
| 38 | } |
| 39 | |
| 40 | public Jackson3Decoder(Iterable<JacksonModule> modules) { |
| 41 | this( |
| 42 | JsonMapper.builder() |
| 43 | .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 44 | .addModules(modules) |
| 45 | .build()); |
| 46 | } |
| 47 | |
| 48 | public Jackson3Decoder(JsonMapper mapper) { |
| 49 | this.mapper = mapper; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Object decode(Response response, Type type) throws IOException { |
| 54 | if (response.status() == 404 || response.status() == 204) return Util.emptyValueOf(type); |
| 55 | if (response.body() == null) return null; |
| 56 | Reader reader = response.body().asReader(response.charset()); |
| 57 | if (!reader.markSupported()) { |
| 58 | reader = new BufferedReader(reader, 1); |
| 59 | } |
| 60 | try { |
| 61 | // Read the first byte to see if we have any data |
| 62 | reader.mark(1); |
| 63 | if (reader.read() == -1) { |
| 64 | return null; // Eagerly returning null avoids "No content to map due to end-of-input" |
| 65 | } |
| 66 | reader.reset(); |
| 67 | return mapper.readValue(reader, mapper.constructType(type)); |
| 68 | } catch (JacksonException e) { |
| 69 | if (e.getCause() != null && e.getCause() instanceof IOException) { |
| 70 | throw IOException.class.cast(e.getCause()); |
| 71 | } |
| 72 | throw e; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Object convert(Object object, Type type) { |
| 78 | return mapper.convertValue(object, mapper.constructType(type)); |
| 79 | } |
| 80 | } |
nothing calls this directly
no outgoing calls
no test coverage detected