Search code examples
androidkotlinandroid-room

How to apply TypeConverter of type "Any" from Model Response from API?


I am using https://www.themealdb.com/api.php as my API. Now, when I tried to generate a class from JSON, some fields are of type "Any". How can I apply a TypeConverter for that?

This is the generated Data class of Meal:

@Entity(
    tableName = "meals"
)
data class Meal(
    @PrimaryKey(autoGenerate = true)
    val id : Int? = null,

    val dateModified: Any,
    val idMeal: String,
    val strArea: String,
    val strCategory: String,
    val strCreativeCommonsConfirmed: Any,
    val strDrinkAlternate: Any,
    val strImageSource: Any,
    val strIngredient1: String,
    val strIngredient10: String,
    val strIngredient11: String,
    val strIngredient12: String,
    val strIngredient13: String,
    val strIngredient14: String,
    val strIngredient15: String,
    val strIngredient16: String,
    val strIngredient17: String,
    val strIngredient18: String,
    val strIngredient19: String,
    val strIngredient2: String,
    val strIngredient20: String,
    val strIngredient3: String,
    val strIngredient4: String,
    val strIngredient5: String,
    val strIngredient6: String,
    val strIngredient7: String,
    val strIngredient8: String,
    val strIngredient9: String,
    val strInstructions: String,
    val strMeal: String,
    val strMealThumb: String,
    val strMeasure1: String,
    val strMeasure10: String,
    val strMeasure11: String,
    val strMeasure12: String,
    val strMeasure13: String,
    val strMeasure14: String,
    val strMeasure15: String,
    val strMeasure16: String,
    val strMeasure17: String,
    val strMeasure18: String,
    val strMeasure19: String,
    val strMeasure2: String,
    val strMeasure20: String,
    val strMeasure3: String,
    val strMeasure4: String,
    val strMeasure5: String,
    val strMeasure6: String,
    val strMeasure7: String,
    val strMeasure8: String,
    val strMeasure9: String,
    val strSource: String,
    val strTags: Any,
    val strYoutube: String
)

Unlike other API, example (NewsAPI) it generated a custom class of type Source and then from there, I can provide a converter easily.


Solution

  • Any is a superclass in kotlin, it means each class in kotlin has Any as a parent class

    open class Any (Common source) (Native source)

    The root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/

    If you are familiar with Java, it is almost the same what is Object in Java.

    Usually autogenerators marks types as Any in Kotlin or Object in Java for fields where they have faced with issue to figure out the type. You could replace this fields by yourself

    e.g.

    val strImageSource: Any => val strImageSource: String 
    

    In case you still need a converter, lets say your date field you have received as a "24/01/2001", but want to save it as a long, you have to write the converter according to spec of your network library. In case you use Retrofit (a popular network/HTTP client), here is a documentation with details regarding custom converters