encodeDataToJsonElementOrDefault

fun encodeDataToJsonElementOrDefault(data: Any?, dataType: KType, json: Json? = null, default: () -> JsonElement? = null): JsonElement(source)

Serializes the given data to a JsonElement using the specified data type. If serialization fails, falls back to a JsonPrimitive wrapping data.toString().

Return

A JsonElement representing the serialized data, or a JsonPrimitive containing data.toString() if serialization fails.

Parameters

data

The object to be serialized.

dataType

The type of the object used to find the appropriate serializer.

default

A lambda function that returns a fallback JsonElement if serialization fails.