Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject dependency into Jackson Custom deserializer

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.

like image 659
Keen Sage Avatar asked Feb 20 '19 01:02

Keen Sage


People also ask

Does Jackson use the default constructor?

By default, Java provides a default constructor(if there's no parameterized constructor) which is used by Jackson to parse the response into POJO or bean classes. and the exception log.

What is JSON Deserialization in Java?

Deserialization is transforming the data from a file or stream back into an object to be used in your application. This can be binary data or structured data like JSON and XML. Deserialization is the opposite of serialization, which transforms objects into byte streams or structured text.


2 Answers

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.

like image 86
Keen Sage Avatar answered Oct 22 '22 21:10

Keen Sage


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.

like image 40
Michał Ziober Avatar answered Oct 22 '22 20:10

Michał Ziober