|
1 | 1 | package com.maxmind.minfraud; |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.annotation.JsonInclude; |
| 4 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
4 | 5 | import com.fasterxml.jackson.databind.MapperFeature; |
5 | 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
6 | 7 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 8 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
7 | 9 | import com.fasterxml.jackson.databind.util.StdDateFormat; |
8 | 10 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
9 | 11 |
|
10 | 12 | import java.io.IOException; |
11 | 13 |
|
12 | 14 | public abstract class AbstractModel { |
| 15 | + private final static ObjectMapper mapper = JsonMapper.builder() |
| 16 | + .addModule(new JavaTimeModule()) |
| 17 | + .defaultDateFormat(new StdDateFormat().withColonInTimeZone(true)) |
| 18 | + .enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) |
| 19 | + .disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS) |
| 20 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 21 | + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 22 | + .serializationInclusion(JsonInclude.Include.NON_NULL) |
| 23 | + .serializationInclusion(JsonInclude.Include.NON_EMPTY) |
| 24 | + .build(); |
13 | 25 |
|
14 | 26 | /** |
15 | 27 | * @return JSON representation of this object. |
16 | 28 | * @throws IOException if there is an error serializing the object to JSON. |
17 | 29 | */ |
18 | 30 | public final String toJson() throws IOException { |
19 | | - ObjectMapper mapper = new ObjectMapper(); |
20 | | - mapper.registerModule(new JavaTimeModule()); |
21 | | - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
22 | | - mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); |
23 | | - mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); |
24 | | - mapper.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS); |
25 | | - mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true)); |
26 | | - mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
27 | | - |
28 | 31 | return mapper.writeValueAsString(this); |
29 | 32 | } |
30 | 33 |
|
|
0 commit comments