Using Dagger 2 to inject values when deserializing with Jackson

488 views Asked by At

When deserializing, Jackson will fetch the values of any properties of an object marked with @JacksonInject from a mapping supplied to the ObjectMapper instead of from the JSON. This mapping is specified by calling ObjectMapper.setInjectableValues() and providing an InjectableValues object which can look up the values to be injected on request.

It is fairly easy to create such an object with Guice (as jackson-module-guice does). First, when you create your object mapper, you inject the Injector and wrap it in an InjectableValues implementation which simply forwards requests to the Injector (the class of the thing to be injected and its annotations are available).

To make this work with Dagger 2, we would need to be able to take a Class object (and relevant annotations) and inject an instance at runtime. However, given Dagger 2's code-generation approach, this does not seem to be possible. Am I missing something?

0

There are 0 answers