0

I am using Retrofit for networking in Android. I want to send some data to backend in one scenario but I want to send the same data albeit with a 1 key missing in another scenario. eg-

data: { "test" : "hello", "test2" :"hi"}

data: { "test" : "hello"}

The endpoint is the same. Only the data is different. I could do this by making different function calls and sending them different data but I thought of making a superclass and subclassing two POJO classes from it. Then depending upon the usecase I would just send the relevant subclass . But for that I would have to declare the uncommon data members in the subclass.

Will the Json parser pick the relevant members from the subclasses? Is this possible?

Vipin CP
  • 3,642
  • 3
  • 33
  • 55
Shitij Goyal
  • 191
  • 1
  • 8

1 Answers1

0

No need to use inheritance, you can do it with Gson or Jackson as is. If you're using Gson for example, you can create a TypeAdapter. It's easier than you might think.

An example of this is in another Stack Overflow answer: https://stackoverflow.com/a/18492010/781729

Community
  • 1
  • 1
r3flss ExlUtr
  • 720
  • 7
  • 17