0

I know a similar question already exists (Jackson - Deserializing to a Different Type) but the answer there does not help me. The old JSON files are already in production and cannot be changed.

I have a regular application with old POJOs. Now I have new POJOs and currently to support the old files I am deserializing to the old POJOs and converting manually to the new ones. I would like to write the conversion directly into a Jackson deserializer but I am having trouble. In the past I have writen a simple Jackson module for serializing and deserializing certain types and I have some basic knowledge of Jackson.

I have something like:

public class MyModule extends com.fasterxml.jackson.databind.module.SimpleModule

  public MyModule() {
    super("MyModule", Version.unknownVersion());

    addDeserializer(OldPojo.class, new JsonDeserializer<NewPojo>() {
      @Override
      public NewPojo deserialize(JsonParser p, DeserializationContext ctxt) {
        // convert and return new pojo
      }
    }

    // others deserializers ...
  }
}

Would this approach be possible? And what am I doing wrong? Lots of thanks.

Miguel
  • 11
  • 3
  • What is the difference between `OldPojo` and `NewPojo`? What is the difference between `Old JSON payload` and `New JSON payload`? – Michał Ziober Apr 06 '20 at 11:39
  • Many differences. For example some a citation POJO may have now a number of authors instead of a single author that used to be a string. New POJOs may also have more or less properties or use new 3rd party libs for local dates. – Miguel Apr 06 '20 at 11:54
  • it will work. what type of error you get? – GolamMazid Sajib Apr 06 '20 at 12:36
  • I finally got it working! I just created a few deserializers for the main POJOs and treated the JsonNodes from there. I followed the second answer from https://stackoverflow.com/questions/18313323/how-do-i-call-the-default-deserializer-from-a-custom-deserializer-in-jackson. One problem was to use the original serializer for the new POJOs and I just made an original mapper. I'd like to learn how to use the default serializer instead. Solution at: https://github.com/SiLeBAT/FSK-Lab/blob/emf_jackson/de.bund.bfr.knime.fsklab.metadata.model/src/metadata/EmfMetadataModule.java – Miguel Apr 07 '20 at 16:25

1 Answers1

0

The documentation for `SimpleModule.addDeserializer() gives the signature for that method like this:

public <T> SimpleModule addDeserializer(Class<T> type, JsonDeserializer<? extends T> deser)

where <T> is the return type of JsonDeserializer.deserialize() (ok, <? extends T> is that type …).

So in your case, you must be able to case a NewPojo instance to an OldPojo reference:

NewPojo o = new NewPojo();
OldPojo r = (OldPojo) o;

If that works without a ClassCastException, you approach may be successful …

You said that you cannot change the JSON files – but do you have to keep the OldPojo class as well? Why does

…
addDeserializer( **NewPojo.class**, new JsonDeserializer<NewPojo>() )
{
    …

do not work for you?

tquadrat
  • 3,033
  • 1
  • 16
  • 29
  • No. I cannot cast NewPojo (o) to OldPojo since they are different classes. NewPojo is not extending OldPojo. Regarding addDeserializer, I thought T is the delegating type. Only T objects would be picked up (NewPojo). Anyway, I am currently trying to edit the old POJO classes with a JsonDeserialize(converter = SomeConverter.class) and I am using one converter per POJO as in https://www.logicbig.com/tutorials/misc/jackson/json-serialize-deserialize-converter.html. I have read that converters can be used directly with StdDelegatingDeserializer but I cannot find a simple example. – Miguel Apr 06 '20 at 11:07