10

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 {
okhobb
  • 758
  • 8
  • 22

3 Answers3

17

Adding the import below works for me.

import kotlinx.serialization.decodeFromString

Kotlin Official Document

JS84
  • 221
  • 3
  • 4
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 16 '22 at 09:15
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31298912) – gremur Mar 21 '22 at 13:38
  • 2
    Thanks JS84. This answer solved the question for me. I had a "DeserializationStrategy required" error, and including this import allowed me to send a String, as expected. I think the included link is also helpful. Note: I did not need to include any OptIn or Experimental flags. I assume this is now old stuff. – Ernie Thomason Jun 05 '22 at 05:13
7

That method is currently part of an experimental API in SerialFormat.kt:

It is an extension function with the following interface:

@OptIn(ExperimentalSerializationApi::class)
public inline fun <reified T> StringFormat.encodeToString(value: T): String

You need to enable experimental APIs to be able to use it.

ByteWelder
  • 5,464
  • 1
  • 38
  • 45
  • Do you mind providing more details? I added this to project level build.gradle: ```tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions.freeCompilerArgs += ["-Xuse-experimental=org.jetbrains.kotlinx.serialization.ExperimentalSerializationApi"] }``` and added `@OptIn(ExperimentalSerializationApi::class)` to the function calling the API, but it still doesn't work. Thanks. – Bao Lei Jan 17 '21 at 03:41
  • Hello Bao were you able to solve the issue? – Ivan Feb 10 '21 at 16:44
3

To use this you need to import import kotlinx.serialization.decodeFromString. But because this is an experimental API, you also need to mark your class or method with @ExperimentalSerializationApi. But to enable experimental switches you also need to add an compiler by languageSettings.optIn("kotlin.RequiresOptIn").

build.gradle.kts

plugins {
    kotlin("jvm") version "1.5.31"
    kotlin("plugin.serialization") version "1.5.31"
}

kotlin.sourceSets.all {
    languageSettings.optIn("kotlin.RequiresOptIn")
}

test.kts

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialFormat
import kotlinx.serialization.json.Json
import kotlinx.serialization.decodeFromString

@Serializable
data class Box(val s: String = "")

@OptIn(ExperimentalSerializationApi::class)
fun readJson(){
    val deserialized = Json.decodeFromString<Box>("""{"s" : "Hallo"}""")
}
Tobse
  • 1,176
  • 1
  • 9
  • 22
  • 1
    In my case only `kotlinx.serialization.json.Json` and `kotlinx.serialization.decodeFromString` were required. No need to use `@OptIn` or those two other imports. And what comes to plugins part: `plugins { id 'com.android.application';id 'kotlin-android';id 'kotlinx-serialization' apply true }` – Scre Oct 12 '21 at 22:00