GlobalComponentDataSerializer

@InterfacedService(acceptMultiple = false)
interface GlobalComponentDataSerializer(source)

Serializes and deserializes data from parameters annotated with @SerializableComponentData and @SerializableTimeoutData.

Default implementation

By default, a Jackson-based serializer with the Kotlin module is used.

Overriding the default instance

You can override the default instance by creating a service implementing this interface, in which you can use any serialization library you want.

Tip: You will generally need to get the type of the to-be-deserialized parameter, which you can get from the ParameterWrapper.

Here's an example with kotlinx.serialization:

@BService
class KotlinxComponentDataSerializer : GlobalComponentDataSerializer {

// Default instance, you can customize it later
private val json = Json

override fun deserialize(parameter: ParameterWrapper, data: SerializedComponentData): Any {
return json.decodeFromString(serializer(parameter.type), data.asString())!!
}

override fun serialize(parameter: ParameterWrapper, obj: Any): SerializedComponentData {
val json = json.encodeToString(serializer(parameter.type), obj)
return SerializedComponentData.fromString(json)
}
}

Functions

Link copied to clipboard
abstract fun deserialize(parameter: ParameterWrapper, data: SerializedComponentData): Any

Deserializes the data into an object compatible with the parameter.

Link copied to clipboard
abstract fun serialize(parameter: ParameterWrapper, obj: Any): SerializedComponentData

Serializes the given object into a SerializedComponentData.