Error compilation: None of the following functions can be called with the arguments supplied. toJson(JsonElement!) defined in com.google.gson.Gson


#1

val gson = GsonBuilder().setPrettyPrinting().create()
val json: String = gson.toJson(token)

I need help. I have an error : None of the following functions can be called with the arguments supplied.
toJson(JsonElement!) defined in com.google.gson.Gson


#2

The problem is that you try to call a function expecting an java.lang.Object. When you use java this is no problem as Object is the superclass of any other class in java so you can just pass any object and java will cast it for you. Kotlin on the other hand does not automatically cast an object to java.lang.Object. This is the reason why you get the error.
I would suggest you add an extension method to Gson like this

fun Gson.toJson(src: Any) : String = toJson(src as java.lang.Object)

Also there is a Kotlin library for serialization. But I think it is still in development. Just thought I point that out.


#3

Thank you, I ll try to use it. Could you recomend a Kotlin library for API testing. Rest, streaming etc…