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,263
  • 3
  • 30
  • 49
Shitij Goyal
  • 161
  • 1
  • 7

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
  • 650
  • 6
  • 16