https://kotlinlang.org logo
#announcements
Title
# announcements
g

Guru

08/20/2019, 3:18 PM
Copy code
fun main(args: Array<String>) {

    val subt1: SubServiceType.SUBT1 = SubServiceType.SUBT1(SUBT1Type.SUBT11)

    val gson = Gson()
    val str = gson.toJson(subt1, SubServiceType::class.java)

    val strJson = "{\"mainServiceType\":\"SUBT21\"}"

    val objSubt2Type = gson.fromJson(strJson, ??) //<- PROBLEM IS HERE
}

enum class MainServiceType(value: Int) {
    MAIN(1),
    MAIN1(2)
}

interface I1{
    val value:Int
}

enum class SUBT1Type(override val value: Int) : I1{
    SUBT11(2),
    SUBT12(3)
}

enum class SUBT2Type(override val value: Int) : I1 {
    SUBT21(2),
    SUBT22(3)
}

sealed class SubServiceType(val mainServiceType: I1) {
    data class SUBT1(val subServiceType: SUBT1Type) : SubServiceType(subServiceType)
    data class SUBT2(val subServiceType: SUBT2Type) : SubServiceType(subServiceType)
}
I am having a problem in serializing this JSON string for sealed class. Any help please?
m

mbonnin

08/20/2019, 4:53 PM
Is it an option to use
moshi
or
kotlinx.serialization
? Gson is not playing super nice with kotlin
g

Guru

08/20/2019, 5:03 PM
Yea. I thought so.. thank you.
5 Views