Search code examples
androidenumsgsonkotlinretrofit2

How use Kotlin enum with Retrofit?


How can I parse JSON to model with enum?

Here is my enum class:

enum class VehicleEnumEntity(val value: String) {
   CAR("vehicle"),
   MOTORCYCLE("motorcycle"),
   VAN("van"),
   MOTORHOME("motorhome"),
   OTHER("other")
}

and I need to parse type into an enum

"vehicle": { "data": { "type": "vehicle", "id": "F9dubDYLYN" } }

EDIT

I have tried standard way, just pass my enum to POJO and it always null


Solution

  • enum class VehicleEnumEntity(val value: String) {
       @SerializedName("vehicle")
       CAR("vehicle"),
    
       @SerializedName("motorcycle")
       MOTORCYCLE("motorcycle"),
    
       @SerializedName("van")
       VAN("van"),
    
       @SerializedName("motorhome")
       MOTORHOME("motorhome"),
    
       @SerializedName("other")
       OTHER("other")
    }
    

    Source