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.


inline fun <T> encodeToString(value: T): String(source)

Serializes the value of type T into an equivalent JSON using serializer retrieved from the reified type parameter.

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(data))

Throws

if the given value cannot be serialized to JSON.