EnumWithCaseConventionSerializer

open class EnumWithCaseConventionSerializer<E : Enum<E>>(enumName: String, entries: EnumEntries<E>, pgConvention: CaseConvention = CaseConvention.SNAKE_CASE_UPPER, kotlinConvention: CaseConvention = CaseConvention.PASCAL_CASE) : KSerializer<E>

A specialized serializer for mapping Kotlin Enums to their PostgreSQL representations.

This serializer is essential when your Enums use different naming conventions in Kotlin (e.g., PascalCase) and PostgreSQL (e.g., SNAKE_CASE_UPPER). It ensures that the value stored in the JSONB payload correctly correctly matches the database's expectations.

Key Features

  • Bidirectional Mapping: Automatically converts names during both serialization and deserialization.

  • Convention Support: Integrates with CaseConvention for flexible name transformations.

Usage Example

@Serializable(with = LegionStatusSerializer::class)
@PgEnum(pgConvention = CaseConvention.SNAKE_CASE_UPPER)
enum class LegionStatus { Garrisoned, OnMarch, InBattle, Victorious }

object LegionStatusSerializer : EnumWithCaseConventionSerializer<LegionStatus>(
enumName = "LegionStatus",
entries = LegionStatus.entries,
pgConvention = CaseConvention.SNAKE_CASE_UPPER,
kotlinConvention = CaseConvention.PASCAL_CASE
)

Parameters

E

The enum class to be serialized.

enumName

A unique identifier for the enum in the serialization descriptor.

entries

The list of all enum values (typically provided via EnumClass.entries).

pgConvention

The naming convention used in the database (default: SNAKE_CASE_UPPER).

kotlinConvention

The naming convention used in your Kotlin code (default: PASCAL_CASE).

Constructors

Link copied to clipboard
constructor(enumName: String, entries: EnumEntries<E>, pgConvention: CaseConvention = CaseConvention.SNAKE_CASE_UPPER, kotlinConvention: CaseConvention = CaseConvention.PASCAL_CASE)

Properties

Link copied to clipboard
open override val descriptor: SerialDescriptor

Functions

Link copied to clipboard
open override fun deserialize(decoder: Decoder): E
Link copied to clipboard
open override fun serialize(encoder: Encoder, value: E)