serializer

open override fun serializer(): KSerializer<DpOffset>

Returns a KSerializer for this field's value type, or null if serialization is not supported.

This method is used to enable persistence of field values. When a serializer is provided, the preview state can be saved and restored across sessions, allowing users to maintain their configured preview settings.

Default behavior

By default, this method returns null, indicating that the field does not support serialization. Built-in primitive fields (like me.tbsten.compose.preview.lab.field.StringField, me.tbsten.compose.preview.lab.field.IntField, me.tbsten.compose.preview.lab.field.BooleanField) provide serializers automatically.

Custom implementation

When creating a custom field with a serializable value type, override this method:

@Serializable
data class MyConfig(val name: String, val enabled: Boolean)

class MyConfigField(label: String, initialValue: MyConfig) :
MutablePreviewLabField<MyConfig>(label, initialValue) {

override fun serializer(): KSerializer<MyConfig> = MyConfig.serializer()

@Composable
override fun Content() { /* ... */}
}

Using withSerializer

For existing fields, use me.tbsten.compose.preview.lab.field.withSerializer to add serialization support without creating a new class:

@Serializable
enum class Theme { Light, Dark, System }

val theme = fieldValue {
SelectableField(
label = "Theme",
choices = Theme.entries,
choiceLabel = { it.name },
).withSerializer(Theme.serializer())
}

Return

A KSerializer for the value type, or null if serialization is not supported

See also