SerializationUtils

Utility object for handling serialization of input data to JSON using Kotlin Serialization.

Functions

Link copied to clipboard
fun encodeDataToJsonElement(data: Any?, dataType: KType, json: Json? = null): JsonElement

Serializes the given data to a JsonElement using the specified data type. Throws SerializationException if serialization fails.

Link copied to clipboard
fun encodeDataToJsonElementOrDefault(data: Any?, dataType: KType, json: Json? = null, default: () -> JsonElement? = null): JsonElement

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

Link copied to clipboard
fun encodeDataToJsonElementOrNull(data: Any?, dataType: KType, json: Json? = null): JsonElement?

Serializes the given data to a JsonElement using the specified data type. Returns the serialized JsonElement if successful, or null if serialization fails.

Link copied to clipboard
fun encodeDataToString(data: Any?, dataType: KType, json: Json? = null): String

Serializes the given data to a string using the specified data type. Throws SerializationException if serialization fails.

Link copied to clipboard
fun encodeDataToStringOrDefault(data: Any?, dataType: KType, json: Json? = null, default: () -> String? = null): String

Serializes the given data to a string using the specified data type. If serialization fails, it falls back to data.toString().

Link copied to clipboard
fun encodeDataToStringOrNull(data: Any?, dataType: KType, json: Json? = null): String?

Serializes the given data to a string using the specified data type. Returns the serialized string if successful, or null if serialization fails.

Link copied to clipboard
fun parseDataToJsonElementOrDefault(data: String, json: Json? = null, default: () -> JsonElement? = null): JsonElement

Attempts to parse the given string into a JsonElement. If the parsing fails due to a SerializationException, it falls back to returning a JsonPrimitive wrapping the original string.