How to inject dependency into Jackson Custom deserializer

*爱你&永不变心* 提交于 2019-12-02 03:54:26

问题


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:

public class CustomDeserializer extends StdDeserializer<String> {

    private SomeDependecy dependency;

    public StringDeserializer() {
        this(null);
    }

    public StringDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return dependency.perform(p.getValueAsString());
    }
}

I cannot register a module based on Class type as it is generic (String.class, Complex Datatype( but not every one require a custome deserializer)). Is there a way to achieve the above without using static methods?

PS: I did search net but could not find a cleaner solution without using statics . All the suggestions where around using Some static method to get context and bean.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/54777586/how-to-inject-dependency-into-jackson-custom-deserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!