encodeToString

override fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String(source)

Serializes the value into an equivalent JSON using the given serializer. This method is recommended to be used with an explicit serializer (e.g. the custom or third-party one), otherwise the encodeToString(value: T) version might be preferred as the most concise one.

Example of usage:

@Serializable
class Project(val name: String, val language: String)

val data = Project("kotlinx.serialization", "Kotlin")

// Prints {"name":"kotlinx.serialization","language":"Kotlin"}
println(Json.encodeToString(Project.serializer(), data))
// The same as Json.encodeToString<T>(value: T) overload
println(Json.encodeToString(data))

Throws

if the given value cannot be serialized to JSON.