0

I have a map like so:

@JsonSerialize(contentUsing = MyClassCustomJacksonSerializer.class) 
@JsonDeserialize(contentUsing = MyClassCustomJacksonDeserializer.class)
Map<Integer, Map<String, MyClass>> mapA;

I get this exception:

java.util.HashMap cannot be cast to com.whatever.MyClass

How can I register a custom deserializer for the nested object MyClass? "contentUsing" attempts to apply it to the nested map rather than the content of the nested map, that makes sense as the annotation is on the root map, but I see no way to specify it for the nested map?

griffinjm
  • 493
  • 2
  • 10
  • 1
    I'd say put the annotation on `MyClass` or use a mixin if you can't change `MyClass`. You always want to apply that custom (de)serializer to MyClass instances, right? – Thomas Oct 23 '20 at 15:22
  • Yeah I should have mentioned it's not really my class, it's from an external library, a mixin is done at the global level? I could also just create register the ser/deser at a global level I guess? ```simpleModule.addSerializer(MyClass.class, new MyClassCustomJacksonSerializer()); simpleModule.addDeserializer(MyClass.class, new MyClassCustomJacksonDeserializer());``` – griffinjm Oct 23 '20 at 15:24
  • Yes I always want to apply to all the MyClass instances – griffinjm Oct 23 '20 at 15:27
  • 1
    Yes, mixins would be applied to the object mapper. Have a look here to get started: https://dzone.com/articles/jackson-mixin-to-the-rescue – Thomas Oct 23 '20 at 15:40
  • 1
    Take a look at `MixIn` feature. [Dynamic addition of fasterxml Annotation?](https://stackoverflow.com/questions/25290915/dynamic-addition-of-fasterxml-annotation/25306786#25306786), [Jackson conditional @JsonUnwrapped](https://stackoverflow.com/questions/25425419/jackson-conditional-jsonunwrapped/25435990#25435990), [Jackson parse json with unwraping root, but without ability to set @JsonRootName](https://stackoverflow.com/questions/19568867/jackson-parse-json-with-unwraping-root-but-without-ability-to-set-jsonrootname). You can use also a new `Module`. – Michał Ziober Oct 23 '20 at 15:46
  • @jgriffin, have you found an answer? Do you need extra help? Were links helpful? – Michał Ziober Oct 28 '20 at 23:00
  • @MichałZiober they helped some, although I am not sure of the advantage of the mixin vs just configuring the ser/deser in a module on the mapper. That's what I ended up doing `SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(MyClass.class, new MyClassCustomJacksonSerializer()); simpleModule.addDeserializer(MyClass.class, new MyClassCustomJacksonSerializer()); mapper.registerModule(simpleModule);` – griffinjm Nov 03 '20 at 23:12
  • 1
    In this case, `SimpleModule` is better because is straightforward. In case, you would like to attach annotation with serialiser or deserialiser to `getter`/`setter` and you do not have an access to a code, you could do that in runtime using `MixIn` feature. `SimpleModule` register serialiser and deserialiser in global scope. `MixIn` can attach it only for given `getter`/`setter`. – Michał Ziober Nov 04 '20 at 01:19

0 Answers0