Roony
Roony

Reputation: 113

Serializer has not been found for type 'Any'

Im trying to serialize a Json file. But there's many variables with the type 'Any', and the @Serializable annotation is not working with this type. I've tried to do exactly like the answer in this question: Kotlin serialization: Serializer has not been found for type 'UUID' But the problem is that there is no "from string()" function for the type 'Any'. Is there a solution for this?

Edit: This is some of the data classes

    @Serializable
data class ApiResponse(
    val data:List<ApiResponseItem> = emptyList()
)


@Serializable
data class ApiResponseItem(
    @SerializedName("cards")
    val cards: List<Any>? = null,
    @SerializedName("country_id")
    val country_id: String? = null,
    @SerializedName("country_logo")
    val country_logo: String? = null,
    @SerializedName("country_name")
    val country_name: String? = null,
    @SerializedName("fk_stage_key")
    val fk_stage_key: String? = null,
    @SerializedName("goalscorer")
     val goalscorer: List<Any>? = null,
    @SerializedName("league_id")
    val league_id: String? = null,
    @SerializedName("league_logo")
    val league_logo: String? = null,
    @SerializedName("league_name")
    val league_name: String? = null,
    @SerializedName("league_year")
    val league_year: String? = null,
    @SerializedName("lineup")
    val lineup: Lineup? = null,
    @SerializedName("match_awayteam_extra_score")
    val match_awayteam_extra_score: String? = null,
    @SerializedName("match_awayteam_ft_score")
    val match_awayteam_ft_score: String? = null,
    @SerializedName("match_awayteam_halftime_score")
    val match_awayteam_halftime_score: String? = null,
    @SerializedName("match_awayteam_id")
    val match_awayteam_id: String? = null,
    @SerializedName("match_awayteam_name")
    val match_awayteam_name: String? = null,
    @SerializedName("match_awayteam_penalty_score")
    val match_awayteam_penalty_score: String? = null,
    @SerializedName("match_awayteam_score")
    val match_awayteam_score: String? = null,
    @SerializedName("match_awayteam_system")
    val match_awayteam_system: String? = null,
    @SerializedName("match_date")
    val match_date: String? = null,
    @SerializedName("match_hometeam_extra_score")
    val match_hometeam_extra_score: String? = null,
    @SerializedName("match_hometeam_ft_score")
    val match_hometeam_ft_score: String? = null,
    @SerializedName(" match_hometeam_halftime_score")
    val match_hometeam_halftime_score: String? = null,
    @SerializedName("match_hometeam_id")
    val match_hometeam_id: String? = null,
    @SerializedName("match_hometeam_name")
    val match_hometeam_name: String? = null,
    @SerializedName("match_hometeam_penalty_score")
    val match_hometeam_penalty_score: String? = null,
    @SerializedName("match_hometeam_score")
    val match_hometeam_score: String? = null,
    @SerializedName("match_hometeam_system")
    val match_hometeam_system: String? = null,
    @SerializedName("match_id")
    val match_id: String? = null,
    @SerializedName("match_live")
    val match_live: String? = null,
    @SerializedName("match_referee")
    val match_referee: String? = null,
    @SerializedName(" match_round")
    val match_round: String? = null,
    @SerializedName("match_stadium")
    val match_stadium: String? = null,
    @SerializedName("mmatch_status")
    val match_status: String? = null,
    @SerializedName("match_time")
    val match_time: String? = null,
    @SerializedName("stage_name")
    val stage_name: String? = null,
    @SerializedName("statistics_1half")
    val statistics_1half: List<Any>? = null,
    @SerializedName("substitutions")
    val substitutions: Substitutions? = null,
    @SerializedName("team_away_badge")
    val team_away_badge: String? = null,
    @SerializedName("team_home_badge")
    val team_home_badge: String? = null
)

Upvotes: 3

Views: 5703

Answers (1)

Ivo
Ivo

Reputation: 23277

Simply put, you can't make a class Serializable when it has fields of type Any. It's simply not possible because Any can quite literally be anything, including classes that are not Serializable. Try defining them more specific. I can assume that

val cards: List<Any>? = null,

is actually maybe

val cards: List<Card>? = null,

As long as you make sure that the types you put there are Serializable it should work

Upvotes: 1

Related Questions