我在Kotlin中遇到一个枚举问题。问题似乎很愚蠢,但可能是我缺少一些代码片段来使其正常工作。这是我的json数据
"CustomerData": [
{
"token": "token_data1",
"role": "Admin"
},
{
"contactUUID": "token_data2",
"role": "User"
}
]
一个Kotlin数据类如下
@JsonInclude
enum class UserRole(val userRoleDesc: String) {
@SerializedName("Admin")
ADMIN("Admin"),
@SerializedName("User")
USER("User"),
@SerializedName("unknown")
UNKNOWN("unknown");
}
我正在使用以下枚举类
@JsonInclude(JsonInclude.Include.NON_EMPTY)
data class CustomerAssociate (
val contactUUID: String,
val role: Enum<UserRole>?
)
但是我遇到了错误
java.lang.RuntimeException: Unable to invoke no-args constructor for java.lang.Enum<T>. Registering an InstanceCreator with Gson for this type may fix this problem.
at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:228)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
有人可以帮我摆脱这个吗?在此先感谢所有人。
答案 0 :(得分:1)
问题在您的声明中。
@JsonInclude(JsonInclude.Include.NON_EMPTY)
data class CustomerAssociate (
val contactUUID: String,
val role: UserRole? // instead of your code 'val role: Enum<UserRole>?'
)