I'm facing an issue in new project. I'm connecting to API that could response two different data types in one response based on some server logic. I'm using Retrofit on Android and I was wondering if there's some "easy" way to handle that cases before retrofit object parse, eg. some kind of parser/serializer that would check what type has specific JSON field? I dunno.
Here are possible responses:
error response:
{
"ReturnCode": "error",
"ReturnCodeNumber": 444,
"ReturnMessage": "Invalid Request",
"ReturnData": ""
}
data response:
{
"ReturnCode": "ok",
"ReturnCodeNumber": 0,
"ReturnMessage": "success",
"ReturnData": [
{
}
]
}
Retrofit API request:
@FormUrlEncoded
@POST("url")
Observable<ApiResponse<List<Data>>> requestData()
API response class has exposed fields of above response and parameterized T for returnData.
So is it possible to somehow wrap it in some serializer class?