How to inject dependency into Jackson Custom deserializer

后端 未结 3 524
北恋
北恋 2020-12-19 04:32

I want to enable a custom jackson deserializer of some fields of type String. The deserializer also needs to be injected with a guice based dependency bean. SampleCode below

相关标签:
3条回答
  • 2020-12-19 04:58

    Take a look on ContextualDeserializer interface. From documentation:

    Add-on interface that JsonDeserializers can implement to get a callback that can be used to create contextual (context-dependent) instances of deserializer to use for handling properties of supported type. This can be useful for deserializers that can be configured by annotations, or should otherwise have differing behavior depending on what kind of property is being deserialized.

    Let's assume you have simple decrypt interface and implementation structure.

    interface Dependency {
    
        String decrypt(String value);
    }
    
    class SomeDependency implements Dependency {
    
        public SomeDependency() {
            System.out.println("Create new SomeDependency!");
        }
    
        @Override
        public String decrypt(String value) {
            return value.replace('a', 'A');
        }
    }
    
    class DecryptModule extends AbstractModule {
    
        @Override
        protected void configure() {
            bind(Dependency.class).to(SomeDependency.class);
        }
    }
    

    You custom deserialiser could look like this:

    class DecryptDeserializer extends StdDeserializer<String> implements ContextualDeserializer {
    
        private Dependency dependency;
    
        public DecryptDeserializer() {
            super(String.class);
        }
    
        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
            return dependency.decrypt(p.getValueAsString());
        }
    
        @Override
        public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
            Injector injector = Guice.createInjector(new DecryptModule());
            DecryptDeserializer deserializer = new DecryptDeserializer();
            deserializer.dependency = injector.getInstance(Dependency.class);
    
            return deserializer;
        }
    }
    

    createContextual method is used to create new deserialiser instance. You have many options how to create it. You can even mix this solutions with Static Injection.

    0 讨论(0)
  • 2020-12-19 04:58

    Looks like there is another approach (Thanks to one of my colleague) using injectableValues on the objectMapper instance and then fetch the dependency through DeserializationContext ctxt. Following is the code.

    ObjectMapper guice module.

    public class MerchantConverterModule extends AbstractModule {
    
        @Override
        protected void configure() {
    
        }
    
        @Provides
        @Singleton
        public ObjectMapper objectMapper() {
    
            ObjectMapper objectMapper = new ObjectMapper();
    
            /**
             * Add dependency object to object mapper.
             */
            objectMapper.setInjectableValues(new InjectableValues
                .Std()
                .addValue("DependencyName", dependency));
    
            return objectMapper;
        }
    
    
    }
    

    Code of your custom deserializer

    public class CustomDeserializer extends StdDeserializer<String> {
    
        private SomeDependecy dependency;
    
        public StringDeserializer() {
            this(null);
        }
    
        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            return getDependency(ctxt).perform(p.getValueAsString());
        }
    
        private SomeDependency getDependency(DeserializationContext ctxt) {
            SomeDependency dependency = (SomeDependency) ctxt
                    .findInjectableValue("DependencyName", null, null);
    
            return dependency;
        }
    }
    

    findInjectableValue method is a final method, so you might need to tweak your unit test code to mock finals.

    NOTE: The drawback is that there is a tight coupling between the objectmapper and deserializer.

    0 讨论(0)
  • 2020-12-19 05:04

    I haven't used Guice that much, but I guess it is somewhat similar to Spring. So I will post this answer here in case you can use the same principles as I have, and that someone using Spring reads this.

    First I annotate my Jackson module as a Spring @Component:

    @Component
    public class FlowModule extends SimpleModule {
    
    @Autowired private SupplierItemDeserializer supplierItemDeserializer;
    

    This means I can get whatever I want @Autowired into my module. As you can see I have also done the same thing to my de- / serializers:

    @Component
    public class SupplierItemDeserializer extends JsonDeserializer<SupplierItem> {
    
    @Autowired
    private SupplierService supplierService;
    

    I have then created a service that deals with converting to and from JSON:

    @Service
    public class IOService {
    
    private ObjectMapper mapper;
    
    @Autowired
    private FlowModule flowModule;
    
    
    @PostConstruct
    public void initialize() {
        mapper = new ObjectMapper();
        mapper.registerModule(flowModule);
        mapper.registerModule(new JavaTimeModule());
    }
    

    The service contains an ObjectMapper with my module that has all the right wiring.

    0 讨论(0)
提交回复
热议问题