EnumWithCaseConventionSerializer
Writes an enum constant into JSON under the label PostgreSQL knows it by, rather than under its Kotlin name.
registerEnum teaches the driver that Praetor is PRAETOR in an enum column. A jsonb payload never reaches that: kotlinx.serialization writes the constant's own name, so the same value is PRAETOR in one place and Praetor in the other, and a query filtering on payload->>'office' finds neither reliably.
On the JVM this is already answered without writing anything: the client reads the labels off the driver's registry, so a registered enum - named at registerEnum, or found by a scan through @PgEnumType - is written correctly wherever @Contextual asks for it. This is for the side that has no registry to read: a frontend sharing the class, or an enum that only ever appears inside JSON. The conventions default to what registerEnum defaults to, so an enum that took the defaults there takes them here too, and @Serializable(with = …) binds tighter than a contextual module - so naming this on the class makes both ends agree by construction rather than by each deriving its own.
@Serializable(with = MagistratureSerializer::class)
@PgEnumType(pgConvention = CaseConvention.SNAKE_CASE_UPPER)
enum class Magistrature { Quaestor, Aedile, Praetor, Consul }
object MagistratureSerializer : EnumWithCaseConventionSerializer<Magistrature>(
enumName = "Magistrature",
entries = Magistrature.entries
)Parameters
What to call it in the descriptor - the class's own name is the obvious choice, and it has to be distinct from every other serializer's in the same format.
Its constants, which is YourEnum.entries.
How the labels are written in PostgreSQL.
How the constants are written in Kotlin.
Type Parameters
The enum being written.