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

**URL:** <https://discuss.kotlinlang.org/t/error-compilation-none-of-the-following-functions-can-be-called-with-the-arguments-supplied-tojson-jsonelement-defined-in-com-google-gson-gson/7606>\
**Category:** Support\
**Created:** [April 28, 2018, 2:48pm UTC](https://discuss.kotlinlang.org/t/error-compilation-none-of-the-following-functions-can-be-called-with-the-arguments-supplied-tojson-jsonelement-defined-in-com-google-gson-gson/7606 "2018-04-28T14:48:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![poet\_peselnik](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@poet\_peselnik](https://discuss.kotlinlang.org/u/poet_peselnik)\
**Post date:** [April 28, 2018, 2:48pm UTC](https://discuss.kotlinlang.org/t/error-compilation-none-of-the-following-functions-can-be-called-with-the-arguments-supplied-tojson-jsonelement-defined-in-com-google-gson-gson/7606/1 "2018-04-28T14:48:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [April 28, 2018, 3:09pm UTC](https://discuss.kotlinlang.org/t/error-compilation-none-of-the-following-functions-can-be-called-with-the-arguments-supplied-tojson-jsonelement-defined-in-com-google-gson-gson/7606/2 "2018-04-28T15:09:15Z")

</div>

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

```kotlin
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.

> **[GitHub - Kotlin/kotlinx.serialization: Kotlin multiplatform / multi-format...](https://github.com/Kotlin/kotlinx.serialization)**
>
> Kotlin multiplatform / multi-format serialization - GitHub - Kotlin/kotlinx.serialization: Kotlin multiplatform / multi-format serialization

---

<div class="post-metadata">

**Author:** ![poet\_peselnik](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@poet\_peselnik](https://discuss.kotlinlang.org/u/poet_peselnik)\
**Post date:** [April 28, 2018, 4:47pm UTC](https://discuss.kotlinlang.org/t/error-compilation-none-of-the-following-functions-can-be-called-with-the-arguments-supplied-tojson-jsonelement-defined-in-com-google-gson-gson/7606/3 "2018-04-28T16:47:56Z")

</div>

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