Moshi PolymorphicJsonAdapter can not make it to work

Viewed 31

I am quite new with android and Moshi, I am trying to make PolymorphicJsonAdapter to work

Here is my setup for now:

Retrofit:

    enum class TileContentType {
        @Json(name = "MediaTile")
        mediaTile,
        @Json(name = "QuestionTile")
        questionTile,
        @Json(name = "ConversationStarterTile")
        conversationStarterTile
    }
    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient, BASE_URL: String): Retrofit {
        val moshi = Moshi.Builder()
            .add(
                PolymorphicJsonAdapterFactory.of(Tile::class.java, "type")
                    .withSubtype(MediaTile::class.java, TileContentType.mediaTile.name)
                    .withSubtype(QuestionTile::class.java, TileContentType.questionTile.name)
                    .withSubtype(ConversationStarterTile::class.java, TileContentType.conversationStarterTile.name)
            )
            .add(KotlinJsonAdapterFactory())
            .build()
       return  Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .client(okHttpClient)
            .build()
    }
}

Base Tile and Tile Response:

open class Tile(@Json(name="type") open val type: String, @Json(name = "id") open val id: String)
data class TileResponse(@Json(name="token") val token: String?, @Json(name="tiles")val tiles: List<Tile>)

Post tile:

data class Author(val id: String, val title: AuthorTitle, val avatar: Avatar)

data class AuthorTitle(val text: String, val actions: List<AuthorTitleActions>?)

data class AuthorTitleActions(val id: String, val text: String, val action: String)

data class Avatar(
    val color: Color?,
    val title: String?,
    val id: String,
    val badge: Badge?,
    val image: String?,
    val action: String?,
)

data class Badge(val id: String, val url: String?)

data class Comment(
    val id: String?,
    val postId: String,
    val author: CommentAuthor,
    val createdDate: Date?,
    val text: String?,
    val noOfLikes: Int?,
    val isLiked: Boolean?,
    val longPressAction: List<CommentAction>,
    val parentId: String?,
    val viewIdentifier: String?,
)

data class CommentAction(
    val command: String,
    val title: String,
    val destructive: Boolean?,
    val id: String,
)

data class DeeplinkAction(val title: String, val action: String)

data class CommentAuthor(
    val userId: String,
    val displayName: String,
    val username: String?,
    val avatar: Avatar,
)

data class TileImage(val url: String, val id: String)

data class PostReactions(
    val topReactions: List<Reaction>,
    val myReaction: String?,
    val profiles: List<ReactionProfile>
)

data class Reaction(val type: String, val id: String, val title: String)
data class ReactionProfile(val id: String, val name: String)

data class PostText(var text: String?, var isTranslated: Boolean?, val originalText: String?)
data class Tag(val id: String, val title: String, val action: String)

interface PostTile {
    @Json(name= "id") val id: String
    val author: Author
    val createdDate: String
    val text: PostText?
    val topComment: Comment?
    val seeAllCommentsAction: DeeplinkAction?
    val allComments: List<Comment?>?
    val tags: List<Tag>?
}

And here is for example MediaTile

data class MediaTile(
    @Json(name= "id") override val id: String,
    override val type: String,
    override val author: Author,
    override val createdDate: String,
    override var text: PostText?,
    override val topComment: Comment?,
    override val seeAllCommentsAction: DeeplinkAction?,
    override val allComments: List<Comment?>?,
    override val tags: List<Tag>?,
    val images: List<TileImage>?,
    val reactions: PostReactions?,
) : PostTile, Tile(type, id)

When I try to fetch data I always get same error: Accessing hidden method Ljava/lang/invoke/MethodHandles$Lookup;->(Ljava/lang/Class;I)V (unsupported, reflection, allowed)

Any help maybe?

0 Answers
Related