| 30 | import tools.jackson.databind.json.JsonMapper; |
| 31 | |
| 32 | public class Jackson3Encoder implements Encoder, JsonEncoder { |
| 33 | |
| 34 | private final JsonMapper mapper; |
| 35 | |
| 36 | public Jackson3Encoder() { |
| 37 | this(Collections.<JacksonModule>emptyList()); |
| 38 | } |
| 39 | |
| 40 | public Jackson3Encoder(Iterable<JacksonModule> modules) { |
| 41 | this( |
| 42 | JsonMapper.builder() |
| 43 | .changeDefaultPropertyInclusion( |
| 44 | incl -> incl.withValueInclusion(JsonInclude.Include.NON_NULL)) |
| 45 | .enable(SerializationFeature.INDENT_OUTPUT) |
| 46 | .addModules(modules) |
| 47 | .build()); |
| 48 | } |
| 49 | |
| 50 | public Jackson3Encoder(JsonMapper mapper) { |
| 51 | this.mapper = mapper; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void encode(Object object, Type bodyType, RequestTemplate template) { |
| 56 | try { |
| 57 | JavaType javaType = mapper.getTypeFactory().constructType(bodyType); |
| 58 | template.body(mapper.writerFor(javaType).writeValueAsBytes(object), Util.UTF_8); |
| 59 | } catch (JacksonException e) { |
| 60 | throw new EncodeException(e.getMessage(), e); |
| 61 | } |
| 62 | } |
| 63 | } |
nothing calls this directly
no outgoing calls
no test coverage detected