Changing how an enum value is serialized

I use Kotlon class enums with Spring/Hibernate and 99.9% of the time they do what I want when these values are serialized into a JSON. The 0.1% case is what I’m dealing with now, I need a leading number character, which Kotlin (like all languages I know of) doesn’t allow in an enum.

The “name” property is final and can’t be overriden. Is there some other way to set what the name string is? I can override toString() fine but apparently that’s not what the JSON serializer is using.

Add @Serializable annotation to the enum and add @SerialName("<JsonName>") annotation to each enum value as needed.

Reference : SerialName for Enums

1 Like

kotlinx-serialization looks like an alternative to the current Jackson-based stuff I’m using now, but @SerialName pointed me in the right direction. In my project just adding @JsonProperty to the enum entry makes it work.