I have a Map<Duration, List<UUID>> and a custom duration module that'll convert Duration class into a string of format <number><time unit> ( e.g. Duration.ofNanos(501001001001) to 5mins 1sec 1ms 1us 1ns).And I am using them via a utility class defined as follows:
public class JacksonUtilities {
public static final ObjectMapper MAPPER = new ObjectMapper().configure(WRITE_DATES_AS_TIMESTAMPS, false)
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.registerModule(DurationModule.getInstance());
private JacksonUtilities() {
}
public static <S> String toJsonString(S source) {
try {
return MAPPER.writeValueAsString(source);
} catch (JsonProcessingException exception) {
throw new ConvertException(source, String.class, exception, false);
}
}
public static <T> T fromJsonString(String jsonString, Class<T> target) {
try {
return MAPPER.readValue(jsonString, target);
} catch (JsonProcessingException exception) {
throw new ConvertException(jsonString, target, exception, false);
}
}
}
Where DurationModule is the custom module that I have written. The issue I am facing while using JacksonUtilities::toJsonString is as follows: While using this for Map<UUID, Duration> the module is executed perfectly giving desired results. ( e.g. {"2042568e-1d20-455b-a151-6315f73394dc":"5mins 1sec 1ms 1us 1ns"} ), but while using the same function for Map<Duration, List<UUID>>, jackson seems to not use my module giving the following result {"PT5M1.001001001S":["2042568e-1d20-455b-a151-6315f73394dc"]}
I have also verified that the issue is not unique to my module as is happening with Jdk8Module from com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.16.1 as well.
how do I force jackson databind to use my custom module for map keys as well