3

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() 
MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
Akhil Latta
  • 1,603
  • 1
  • 22
  • 37
  • Are they different for the same endpoint or are the endpoints also different? What I'm trying to ask is, why not create different models? – Fred Sep 02 '19 at 10:57

0 Answers0