I am new to Moshi and looking for a way to setup a Moshi adapter with dagger 2 to automatically create a custom Object or List based on the JSON response.
The API can return either of the following 2 responses:
[{
"item_type": "xyz",
"items": [{
"name": "foo",
"age": 22
},
{
"name": "bar",
"age": 32
}
]
}]
or
[{
"name": "foo",
"age": 22
},
{
"name": "bar",
"age": 32
}]
I looked at Moshi Determine if JSON is array or single object for reference but in my use case the whole response JSON object has a different structure. Also, Since I am using dagger2 for dependency injection, I am not sure how and where to add the adapter as my Network module which providesMoshi is pretty generic and I won't need this custom adapter for other API's.
@Provides
@Singleton
fun provideMoshi(): Moshi = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
.build()