1

I'm receiving next json from FE:

{
  "type1":"A",
  "type2":"B"
}

enum Type1 {
   A,
   B
}

enum Type2 {
   A,
   B
}

And base on those two fields I want to map json to specific class. For example I have:

class Base {
   Type1 type1;
   Type2 type2;
}

class Ab extends Base {

}

Object should be Ab in case of type1 = A and type2 = B

class Ba extends Base {

}

Object should be Ba in case of type1 = B and type2 = a

For this case we have @JsonTypeInfo annotation but it works only with one field. Is there any solution for this problem?

Orest
  • 6,548
  • 10
  • 54
  • 84
  • 2
    You can use a custom resolver with[`@JsonTypeResolver`](https://fasterxml.github.io/jackson-databind/javadoc/2.4/com/fasterxml/jackson/databind/annotation/JsonTypeResolver.html) – Aaron Dec 01 '17 at 13:45
  • @Aaron hmm actually I can't get both fields to form an output object, maybe you have an example ? – Orest Dec 01 '17 at 14:54
  • I don't have an example handy, but maybe [this article](https://www.thomaskeller.biz/blog/2013/09/10/custom-polymorphic-type-handling-with-jackson/) helps? – Aaron Dec 01 '17 at 15:39
  • I've tried exactly from that article. There is no way to get both fields in resolver. – Orest Dec 01 '17 at 15:40
  • @OrestKyrylchuk i think you will have to use custom deserializer/serializer instead( – varren Dec 02 '17 at 07:31
  • @varren yep that would work, but my idea was to get around deserializer/serializer ( – Orest Dec 03 '17 at 15:48

0 Answers0