Home > OS >  How to decode json data with Reified, Generics, Interface and Kotlin?
How to decode json data with Reified, Generics, Interface and Kotlin?

Time:11-05

How to decode json data with Reified, Generics, Interface and Kotlin?

I created a project where i put the code and the instructions to run: https://github.com/paulocoutinhox/kotlin-gson-sample

But basically the code is:

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

interface Serializer {
    fun <T> decodeValue(data: String): T?
}

class JsonSerializer : Serializer {
    override fun <T> decodeValue(data: String): T? {
        try {
            val type = object : TypeToken<T>() {}.type
            val gson = Gson()
            return gson.fromJson<T>(data, type)
        } catch (e: Exception) {
            println("Error when parse: ${e.message}")
        }

        return null
    }
}

class Request<T>(val r: T)

inline fun <reified T> callSerializer(json: String): T? {
    val serializer = JsonSerializer()
    val decoded = serializer.decodeValue<Request<T>>(json)
    return decoded?.r
}

fun main() {
    val finalValue = callSerializer<Request<String>>("{\"r\": \"test\"}")
    println("Decoded data is: $finalValue")
}
  • The Request class has an inner value called r with generic type.
  • Im trying convert the json data above to the Request class and bind r from json to string type.

But im getting the error:

> Task :run FAILED
Exception in thread "main" java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class Request (com.google.gson.internal.LinkedTreeMap and Request are in unnamed module of loader 'app')
        at MainKt.main(Main.kt:36)
        at MainKt.main(Main.kt)

The gson library think that it is a LinkedTreeMap instead of the Request class.

How to solve this?

Thanks.

CodePudding user response:

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

interface Serializer {
    fun <T> decodeValue(data: String): Request<T>?
}

class JsonSerializer : Serializer {
    override fun <T> decodeValue(data: String): Request<T>? {
        try {
            val type = object : TypeToken<Request<T>>() {}.type
            val gson = Gson()
            return gson.fromJson<Request<T>>(data, type)
        } catch (e: Exception) {
            println("Error when parse: ${e.message}")
        }

        return null
    }
}

class Request<T>(val r: T)

inline fun <reified T> callSerializer(json: String): T? {
    val serializer = JsonSerializer()
    val decoded = serializer.decodeValue<T>(json)
    return decoded?.r
}

fun main() {
    println("Decoded data is: ${callSerializer<String>("{\"r\": \"test\"}")}")
}
  • Related