Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Kotlin's Json.decodeFromString work without a first DeserializationStrategy argument?

Tags:

json

kotlin

I'm very new to Kotlin and am curious what magic allows this code to work:

From https://github.com/Kotlin/kotlinx.serialization/blob/e2e764a132c8eebd31208120774baf9a71ec23c7/formats/json/commonTest/src/kotlinx/serialization/SerializerForNullableTypeTest.kt

@Serializable
data class Box(val s: StringHolder?)

val deserialized = Json.decodeFromString<Box>(string)

When the function definition seems to require an initial argument before the encoded JSON string.

https://github.com/Kotlin/kotlinx.serialization/blob/e2e764a132c8eebd31208120774baf9a71ec23c7/formats/json/commonMain/src/kotlinx/serialization/json/Json.kt

public final override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T {
like image 439
okhobb Avatar asked Feb 17 '26 06:02

okhobb


1 Answers

Adding the import below works for me.

import kotlinx.serialization.decodeFromString

Kotlin Official Document

like image 154
JS84 Avatar answered Feb 20 '26 00:02

JS84



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!