Search code examples
androidrealm-mobile-platformrealm-listmoshi

Error using RealmList with Moshi


Caused by: java.lang.IllegalArgumentException: Platform java.lang.Class annotated [] requires explicit JsonAdapter to be registered

I get the above error when using a RealmList with Moshi.

public class MenuModel implements RealmModel
{
    @Json(name = "menugroups")
    private RealmList<MenuGroupModel> menugroups = null;
}

I have researched this a bit and found the following:

https://github.com/realm/realm-java/issues/2711

The solution there says I need to use a RealmListAdapter. I have copied the code to my project. I, however, have no idea how and where to implement this adapter.

How do I use it?


Solution

  • Go here: Regalia Moshi

    Copy both RealmListAdapter and RealmListAdapterJsonFactory to your project (or use the linked project). Add the adapter to your moshi like this:

    Moshi moshi = new Moshi.Builder()
    .add(new RealmListJsonAdapterFactory())
    .build();
    

    Further usage, if you're using Retrofit:

    Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.ENDPOINT)
                .addConverterFactory(MoshiConverterFactory.create(moshi))
                .build();