1

I am using Jackson to deep-copy an object, like so:

   Map<String,Object> props = mapper.convertValue(prototype, Map.class); 
   Command command = mapper.convertValue(props, Command.class);

However, since Command is an interface without @JsonTypeInfo attached, this copy fails with error: Can not construct instance of Command.. abstract type.

I don't want to annotate the interface. I want to provide type information at the conversation level.

How do I do that?


I thought this would work:

mapper.enableDefaultTyping();

Whereby type info would automatically be added for both de/serialization, but I get:

Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.util.Map

So the Map is not getting the type info it needs.

mtyson
  • 8,196
  • 16
  • 66
  • 106
  • @RobertMoskal - This isn't about excluding fields, its about providing the type information programmatically for `convertValue()`. – mtyson Oct 08 '16 at 15:54
  • Sorry about that. Seems like the filter technique should work no? – Robert Moskal Oct 08 '16 at 15:55
  • @RobertMoskal No worries... I'm not familiar with the filter technique. – mtyson Oct 08 '16 at 15:57
  • It's a duplicate of the reverse problem: http://stackoverflow.com/questions/5489532/jackson-json-library-how-to-instantiate-a-class-that-contains-abstract-fields – Robert Moskal Oct 08 '16 at 16:07
  • @RobertMoskal Those solutions all require TypeInfo be set on the interface or classes... I'm wanting to do it programmatically at the conversion. – mtyson Oct 08 '16 at 18:59
  • 1
    @mtyson you can set TypeInfo in a mixin for Command class for the given object mapper: objectMapper.addMix(...).convertValue(...) – Alexey Gavrilov Oct 12 '16 at 17:20

0 Answers0