9

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.

Keen Sage
  • 1,899
  • 5
  • 26
  • 44
  • I feel there is two parts to the question. First, how to inject a dependency. Could that be solved by registering this deserializer using ```ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(String.class, new CustomDeserializer()); mapper.registerModule(module);``` However, applying this deserializer selectively to some String fields, that is a different problem. How do you define which String fields in your class should this deserializer apply to? – Swaranga Sarma Feb 20 '19 at 06:34
  • The String that are encrypted will fail under the list of String fields that the deserializer would apply to. – Keen Sage Feb 20 '19 at 06:39
  • Can the decryption be done after the JSON de-serialization. You could create an intermediate model that can be parsed simply with Object mapper and that represents the encrypted data and then call some different API to decrypt the encrypted model. It might also be beneficial in some cases when you want to handle failures differently for JSON parsing exception or dependency failure exception – Swaranga Sarma Feb 20 '19 at 07:30
  • There are also use-cases where this applies to complex objects. Hence, I was trying to somehow get the decryption logic injected in deserialization. Otherwise, I will have to write a two layer of Transformation JSONStringWithEncryptedBlobs -> JSONStringWithPlainData -> JavaPojo – Keen Sage Feb 20 '19 at 09:41
  • It seems like you should use a wrapper type around String in particular places that you want to use this deserializer. That would allow you to bind to that wrapper type explicitly. `module.addDeserializer(StringWrapper.class, new CustomDeserializer(dependency))` – George L Feb 20 '19 at 17:31

3 Answers3

5

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.

Keen Sage
  • 1,899
  • 5
  • 26
  • 44
3

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.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
0

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.

Lars Juel Jensen
  • 1,643
  • 1
  • 22
  • 31