|
9 | 9 | import com.fasterxml.jackson.databind.BeanDescription; |
10 | 10 | import com.fasterxml.jackson.databind.JavaType; |
11 | 11 | import com.fasterxml.jackson.databind.JsonSerializer; |
12 | | -import com.fasterxml.jackson.databind.Module; |
13 | 12 | import com.fasterxml.jackson.databind.ObjectMapper; |
14 | 13 | import com.fasterxml.jackson.databind.SerializationConfig; |
15 | 14 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
|
18 | 17 | import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
19 | 18 |
|
20 | 19 | import io.quarkus.jackson.ObjectMapperCustomizer; |
21 | | -import io.quarkus.resteasy.reactive.jackson.runtime.ResteasyReactiveServerJacksonRecorder; |
22 | 20 |
|
23 | 21 | @Singleton |
24 | 22 | public class GeneratedSerializersRegister implements ObjectMapperCustomizer { |
25 | 23 |
|
| 24 | + private static final SimpleModule mappingModule = new SimpleModule(); |
| 25 | + private static final ExactSerializers serializers = new ExactSerializers(); |
| 26 | + |
| 27 | + static { |
| 28 | + // Use a custom SimpleSerializers to use a json serializer only if it has been generated for that |
| 29 | + // exact class and not one of its sublclasses. This is already the default behaviour for deserializers. |
| 30 | + mappingModule.setSerializers(serializers); |
| 31 | + } |
| 32 | + |
26 | 33 | @Override |
27 | 34 | public void customize(ObjectMapper objectMapper) { |
28 | | - objectMapper.registerModule(MappingModuleHolder.mappingModule); |
| 35 | + objectMapper.registerModule(mappingModule); |
29 | 36 | } |
30 | 37 |
|
31 | | - static class MappingModuleHolder { |
32 | | - static final Module mappingModule = createMappingModule(); |
33 | | - |
34 | | - private static Module createMappingModule() { |
35 | | - SimpleModule module = new SimpleModule(); |
36 | | - // Use a custom SimpleSerializers to use a json serializer only if it has been generated for that |
37 | | - // exact class and not one of its sublclasses. This is already the default behaviour for deserializers. |
38 | | - ExactSerializers serializers = new ExactSerializers(); |
39 | | - |
40 | | - for (Class<? extends StdSerializer> serClass : ResteasyReactiveServerJacksonRecorder.getGeneratedSerializers()) { |
41 | | - try { |
42 | | - StdSerializer serializer = serClass.getConstructor().newInstance(); |
43 | | - serializers.addExactSerializer(serializer.handledType(), serializer); |
44 | | - } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
45 | | - | NoSuchMethodException e) { |
46 | | - throw new RuntimeException(e); |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - module.setSerializers(serializers); |
51 | | - |
52 | | - for (Class<? extends StdDeserializer> deserClass : ResteasyReactiveServerJacksonRecorder |
53 | | - .getGeneratedDeserializers()) { |
54 | | - try { |
55 | | - StdDeserializer deserializer = deserClass.getConstructor().newInstance(); |
56 | | - module.addDeserializer(deserializer.handledType(), deserializer); |
57 | | - } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
58 | | - | NoSuchMethodException e) { |
59 | | - throw new RuntimeException(e); |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - return module; |
| 38 | + public static void addSerializer(Class<? extends StdSerializer> serClass) { |
| 39 | + try { |
| 40 | + StdSerializer serializer = serClass.getConstructor().newInstance(); |
| 41 | + serializers.addExactSerializer(serializer.handledType(), serializer); |
| 42 | + } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
| 43 | + | NoSuchMethodException e) { |
| 44 | + throw new RuntimeException(e); |
64 | 45 | } |
| 46 | + } |
65 | 47 |
|
| 48 | + public static void addDeserializer(Class<? extends StdDeserializer> deserClass) { |
| 49 | + try { |
| 50 | + StdDeserializer deserializer = deserClass.getConstructor().newInstance(); |
| 51 | + mappingModule.addDeserializer(deserializer.handledType(), deserializer); |
| 52 | + } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
| 53 | + | NoSuchMethodException e) { |
| 54 | + throw new RuntimeException(e); |
| 55 | + } |
66 | 56 | } |
67 | 57 |
|
68 | 58 | public static class ExactSerializers extends SimpleSerializers { |
|
0 commit comments