0

I have an interface in java and I want to use that for json parsing. For example, consider the interface:

interface Student {
  String getName();
  int getId();
}

And with a json like:

{"id":1, "name"="jon"}

I want to parse that json using the interface alone. There are multiple concrete implementations of this class, and the implementing classes have a lot more fields than in json, so I don't want to use those classes. I know that I can achieve that by using dynamix proxies in java by defining my own invocation handlers. But, is there something inbuilt in jackson already for doing that?

Thanks in advance.

Aarkan
  • 3,811
  • 6
  • 40
  • 54
  • May be same problem as [How to add custom deserializer to interface using jackson](https://stackoverflow.com/questions/25387978/how-to-add-custom-deserializer-to-interface-using-jackson) – Thomas Fritsch Jun 08 '18 at 20:26
  • How come that is the same problem? The solution there needs changes in implementing classes while I was looking for a way to deserialize using interface without changing the implementing classes at all. Not sure why the question was downvoted. – Aarkan Jun 08 '18 at 20:39
  • I only said *may be* it's the same problem*. And by the way, I'm not the down-voter. – Thomas Fritsch Jun 08 '18 at 21:12

1 Answers1

1

Jackson has to be able to create objects from the JSON but an interface cannot be instantiated so this is an impossible problem(Besides some proxy hack). If you want to deserialize to a common object class just change the interface to be a class.

Deadron
  • 5,135
  • 1
  • 16
  • 27
  • This answer isn't entirely accurate as it can be possible to register an implementation for an interface to be used when deserializing. – Deadron Dec 10 '20 at 01:14