toJsonElement

inline fun <T> T.toJsonElement(): JsonElement

Converts this value to a JsonElement tree using kotlinx.serialization.

A String receiver is serialized as a JSON string, so "42".toJsonElement() inside generic code is the string "42". To parse JSON text, use parseJson.

Return

the JsonElement representation

Type Parameters

T

the type to serialize (must be @Serializable)


fun String.toJsonElement(verbose: Boolean = false): JsonElement

Deprecated

Parses JSON text, unlike the generic toJsonElement, which serializes a String as a JSON string. Use parseJson.

Replace with

parseJson(verbose)

Parses this String as JSON text into a JsonElement; renamed parseJson.

The name shadowed the generic toJsonElement, so the same call parsed "42" into the number 42 on a String but serialized it as the string "42" in generic code.