Alex
Alex

Reputation: 1228

Deserialize a JSON class that has interface properties without using annotations

I want to deserialize a JSON representation of an object that contains an interface as one of its properties. The class of the object looks something Like this:

class A {
    B b;
}

Interface B {
   //some content
}
Class C implements B {
}
Class D implements B {

}

I thought jackson is the best to do this.

The simplest way is to put some annotations above interface B as mentioned in the solution here . But the problem here is I do not have access to change any of those classes or interface. So I cannot put annotations in class A. Is there another way to achieve the same?

Upvotes: 2

Views: 844

Answers (1)

Patrick Chan
Patrick Chan

Reputation: 1029

Fast dirty workaround:

You can deserialize the class C first and set as a class A's attribute

Formal method:

This is a kind of deserialization strategy which you may want to determine whether it's a class C or class D by the content of JSON, which is custom deserialization

Upvotes: 1

Related Questions