a sample code based on showcase form example:
// not serializable class
class ObjectId(val id: Int) {
override fun toString(): String {
return "$id"
}
}
// custom serializer
object ObjectIdSerializer : KSerializer<ObjectId> {
override val descriptor: SerialDescriptor = SerialClassDescImpl("com.example.ObjectId")
override fun deserialize(decoder: Decoder): ObjectId {
val str = decoder.decodeString()
return ObjectId(str.toInt())
}
override fun serialize(encoder: Encoder, obj: ObjectId) {
encoder.encodeString(obj.id.toString())
}
}
// Form model
@Serializable
data class Form(
val text: String? = null,
val password: String? = null,
val password2: String? = null,
val textarea: String? = null,
val richtext: String? = null,
val date: Date? = null,
val time: Date? = null,
val checkbox: Boolean = false,
val radio: Boolean = false,
val simpleSelect: String? = null,
val select: String? = null,
val ajaxselect: String? = null,
val spinner: Double? = null,
val radiogroup: String? = null,
val upload: List<KFile>? = null,
@ContextualSerialization val objectId: ObjectId? = null
)
// FormPanel initialization
val formPanel = formPanel<Form>(customSerializers = mapOf(ObjectId::class to ObjectIdSerializer)) {
// Add custom field and String control to the form
addCustom(Form::objectId, Text(label = "ObjectId"))
// get form data
formPanel.getData()
formPanel.getDataJson()
// set form data
formPanel.setData(Form(objectId = ObjectId(14)))