only classes are allowed on the left hand side of a class literal

前端 未结 1 387
Happy的楠姐
Happy的楠姐 2020-12-30 00:05

I know a lot of similar questions here in StackOverflow, but nothing solved mine.

I have a generic data class:

  data class ServiceCall(         


        
相关标签:
1条回答
  • 2020-12-30 00:44

    You can't use generics with class, as easily observable here:

    List<Int>::class.java
    

    It gives you the same error. To use the generic typ in GSON deserialization, do what is suggested here:

    https://stackoverflow.com/a/5554296/8073652

    EDIT:

    In Kotlin it looks like this:

    val type: Type = object : TypeToken<ServiceCall<SurveyListModel>>() {}.type 
    Gson().fromJson<ServiceCall<SurveyListModel>>(json, type).result
    

    Here's a small proof of concept, I've written:

     class Token : TypeToken<List<Int>>()
     val x: List<Int> = Gson().fromJson(Gson().toJson(arrayOf(1)), Token().type)
     println(x)
    
    0 讨论(0)
提交回复
热议问题