In this question I've seen an interesting approach for injecting objects into custom deserializers for the Jackson mapping framework (I'm currently on version 2.10.x).
Essentially, one registers a dependency MyService in the ObjectMapper
jsonMapper.setInjectableValues(new InjectableValues
.Std()
.addValue(MyService.class.getName(), myServiceInstance));
and then in a class that extends StdDeserializer
it can be accessed through the DeserializationContext
which has a findInjectableValue
method.
Now, I hope the library provides a symmetric approach for serialisation, but honestly could not find it. Specifically, if you have a class that extends StdSerializer
, you will need to implement a method serialize(ProjectSerializable value, JsonGenerator jsonGenerator, SerializerProvider provider)
which doesn't seems to have a class with similar features of DeserializationContext
.
So, how can one achieve the same "injection" with a custom serializer without resorting to ugly solutions based on static access to instance providers or other untestable things.