|
33 | 33 | * Manages system types and is used to deserialize polymorphic types.
|
34 | 34 | */
|
35 | 35 | public class TypeManager {
|
36 |
| - private final ObjectMapper objectMapper; |
| 36 | + private final ObjectMapper defaultMapper; |
37 | 37 |
|
38 | 38 | /**
|
39 |
| - * Concurrent support is not needed since this map is only populated a boot, which is single-threaded |
| 39 | + * Concurrent support is not needed since this map is only populated a boot, which is single-threaded. |
40 | 40 | */
|
41 | 41 | private final Map<String, ObjectMapper> objectMappers = new HashMap<>();
|
42 | 42 |
|
43 | 43 | /**
|
44 |
| - * Constructor without params. |
| 44 | + * Default constructor. |
45 | 45 | */
|
46 | 46 | public TypeManager() {
|
47 |
| - objectMapper = new ObjectMapper(); |
48 |
| - objectMapper.registerModule(new JavaTimeModule()); // configure ISO 8601 time de/serialization |
49 |
| - objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); // serialize dates in ISO 8601 format |
50 |
| - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 47 | + defaultMapper = new ObjectMapper(); |
| 48 | + defaultMapper.registerModule(new JavaTimeModule()); // configure ISO 8601 time de/serialization |
| 49 | + defaultMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); // serialize dates in ISO 8601 format |
| 50 | + defaultMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 51 | + |
| 52 | + registerContext("default", defaultMapper); |
51 | 53 | }
|
52 | 54 |
|
53 | 55 | /**
|
54 | 56 | * Returns the object mapper for the default serialization context.
|
55 | 57 | */
|
56 | 58 | public ObjectMapper getMapper() {
|
57 |
| - return objectMapper; |
| 59 | + return defaultMapper; |
58 | 60 | }
|
59 | 61 |
|
60 | 62 | /**
|
61 | 63 | * Returns the object mapper for the given serialization context, creating one based on the default mapper if required.
|
62 | 64 | */
|
63 | 65 | @NotNull
|
64 | 66 | public ObjectMapper getMapper(String key) {
|
65 |
| - return objectMappers.computeIfAbsent(key, k -> objectMapper.copy()); |
| 67 | + return objectMappers.computeIfAbsent(key, k -> defaultMapper.copy()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Add custom mapper by key to list of object mappers. |
| 72 | + */ |
| 73 | + public void registerContext(String key, ObjectMapper mapper) { |
| 74 | + objectMappers.put(key, mapper); |
66 | 75 | }
|
67 | 76 |
|
68 | 77 | /**
|
69 | 78 | * Registers types with all contexts.
|
70 | 79 | */
|
71 | 80 | public void registerTypes(Class<?>... type) {
|
72 |
| - objectMapper.registerSubtypes(type); |
73 | 81 | objectMappers.values().forEach(m -> m.registerSubtypes(type));
|
74 | 82 | }
|
75 | 83 |
|
76 | 84 | /**
|
77 | 85 | * Registers types with all contexts.
|
78 | 86 | */
|
79 | 87 | public void registerTypes(NamedType... type) {
|
80 |
| - objectMapper.registerSubtypes(type); |
81 | 88 | objectMappers.values().forEach(m -> m.registerSubtypes(type));
|
82 | 89 | }
|
83 | 90 |
|
|
0 commit comments