kamikat / moshi-jsonapi

JSON API v1.0 Specification in Moshi.
MIT License
156 stars 34 forks source link

Using getLinks() #42

Closed ioanafurtuna closed 7 years ago

ioanafurtuna commented 7 years ago

Hello!

I'm in the situation where I need to use the getLinks() method, but I got stuck.

Firstly, I'm having a problem while implemeting a GET request that should return a Document object, but I've found a workaround by using Retrofit's ResponseBody:

@GET("articles") Call<ResponseBody> getArticles();

If I use

@GET("articles") Call<Document> getArticles();

my app crashes with this error:

java.lang.IllegalArgumentException: Unable to create converter for class moe.banana.jsonapi2.Document

Disregarding that, my "links" object looks like this:

"links": { "link1": "http://<url_here>", "link2": "http://<url_here>" }

According to the documentation, I should use a JsonBuffer , but I am stuck while trying to deserialize the data inside it. Help, please!

Thanks!

kamikat commented 7 years ago

Maybe the retrofit instance is not correctly configured to use with moshi-jsonapi. Check the Retrofit integration guide we've just added to README.md.

As a Links object varies from case to case, a JsonBuffer is returned when calling getLinks(). Then you're expected to call JsonBuffer.get(JsonAdapter<T>) to deserialize the object within witch the JsonAdapter<T> parameter is a standard moshi adapter instance.

ioanafurtuna commented 7 years ago

Hello @kamikat , thanks for your reply. For my java.lang.IllegalArgumentException: Unable to create converter for class moe.banana.jsonapi2.Document error, I'm unable to find where the problem might be. My code looks like this:


 @Provides @Inject @Singleton
    Retrofit provideRetrofit(Moshi moshi, OkHttpClient httpClient, IdentificationService idService, AuthorizationService service) {

        final String apiBaseUrl = "<url>";
        return new Retrofit.Builder()
                .baseUrl(apiBaseUrl)
                .addConverterFactory(JsonApiConverterFactory.create(moshi))
                .client(httpClient
                        .newBuilder()
                        .addInterceptor( (Interceptor) service)
                        .build())
                .build();
    }

    @Provides @Singleton
    Moshi provideMoshiAdapter() {
        return new Moshi.Builder()
                .add(provideJsonApiAdapter())
                .add(new DateTimeAdapter())
                .add(new OtherAdapter())
                .build();
    }

    private JsonAdapter.Factory provideJsonApiAdapter()  {
        return ResourceAdapterFactory.builder()
                .add(Model1.class)
                .add(Model2.class)
                .add(Model3.class)
                .add(Model4.class)
                .add(Model5.class)
                .build();

    }

I'm using the JsonApiConverterFactory exactly as it's shown in this example.

Is there anything I should correct?

Thanks a lot

kamikat commented 7 years ago

Ahh... JsonApiConverterFactory does not handles Document correctly. And it is fixed now 😃