I came across one issue with some of the response we getting is not straight to parse and convert it to POJO. The format of response I am getting is as below
[
"list",
[
{
"@type": "com.exampe.model.ModelName",
"number": 1,
"name": "Test Name",
"url": "/test/url/",
"type": "f"
}
]
]
I want to ignore that "list" and parse a POJO in List of object ModelName. I am using Retrofit and Moshi Convertor but I am not sure how I can achieve this. Is there any way that I can intercept the response before it passed to Moshi Convertor or any different approach that I can go for.
Retrofit Snippet
private fun getRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl(BuildConfig.API_URL)
.addConverterFactory(MoshiConverterFactory.create())
.client(getHTTPClient())
.build()
}
Retrofit
offers custom converters (see official documentation),moshi
also should offer something similar.
I do not have experience in using moshi
, but I have checked documentation and source code - it looks it is possible.
Moshi
offers custom adapters which should do things you need. Take a look on PolymorphicJsonAdapterFactory, it has methods fromJson()
and toJson()
which allows you manually parse json
elements in which way you like.
Even more. PolymorphicJsonAdapterFactory
looks as an option you need for this.
A JsonAdapter factory for objects that include type information in the JSON. When decoding JSON. Moshi uses this type information to determine which class to decode to. When encoding Moshi uses. the object’s class to determine what type information to include.