# Changing how an enum value is serialized

**URL:** <https://discuss.kotlinlang.org/t/changing-how-an-enum-value-is-serialized/16499>\
**Category:** Support\
**Created:** [February 26, 2020, 7:13am UTC](https://discuss.kotlinlang.org/t/changing-how-an-enum-value-is-serialized/16499 "2020-02-26T07:13:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian.monroe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ian.monroe/32/7279_2.png) [@ian.monroe](https://discuss.kotlinlang.org/u/ian.monroe)\
**Post date:** [February 26, 2020, 7:13am UTC](https://discuss.kotlinlang.org/t/changing-how-an-enum-value-is-serialized/16499/1 "2020-02-26T07:13:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [February 26, 2020, 8:28am UTC](https://discuss.kotlinlang.org/t/changing-how-an-enum-value-is-serialized/16499/2 "2020-02-26T08:28:15Z")

</div>

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

Reference : [SerialName for Enums](https://github.com/Kotlin/kotlinx.serialization/issues/31#issuecomment-559571106)

---

<div class="post-metadata">

**Author:** ![ian.monroe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ian.monroe/32/7279_2.png) [@ian.monroe](https://discuss.kotlinlang.org/u/ian.monroe)\
**Post date:** [February 26, 2020, 4:32pm UTC](https://discuss.kotlinlang.org/t/changing-how-an-enum-value-is-serialized/16499/3 "2020-02-26T16:32:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pihentagy](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@pihentagy](https://discuss.kotlinlang.org/u/pihentagy)\
**Post date:** [March 22, 2023, 3:40pm UTC](https://discuss.kotlinlang.org/t/changing-how-an-enum-value-is-serialized/16499/4 "2023-03-22T15:40:40Z")

</div>

Trying custom serialization in a spring boot application, and I cannot figure out the way how to handle custom enum representations.

```kt
enum class VoltageLevel(@get:JsonValue val s: String) {
    HIGH("h"),
    MEDIUM("m"),
    LOW("l")
}

```

I use it in the following controller:

```kt
@RestController
class MapController(private val mapElementRepository: MapElementRepository) {
    @GetMapping("/map-elements/view-window", produces = [MediaType.APPLICATION_JSON_VALUE])
    fun index(
// other parameters
        @RequestParam voltageLevels: Array<VoltageLevel>?
    ): MapElementResult {
// ...
    }
}

```

On the swagger UI, the values are ok (h, m and v), but when I fire a request, I get

> Could not resolve parameter [5] in public foobar.mapservice.MapElementResult foobar.mapservice.MapController.index(java.math.BigDecimal,java.math.BigDecimal,java.math.BigDecimal,java.math.BigDecimal,foobar.mapservice.NetworkType,foobar.mapservice.VoltageLevel,int): Failed to convert value of type ‘java.lang.String’ to required type ‘foobar.mapservice.VoltageLevel’; Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam foobar.mapservice.VoltageLevel] for value ‘h’

Same error if I try this way:

```kt
enum class VoltageLevel {
    @JsonProperty("h")
    HIGH,

    @JsonProperty("m")
    MEDIUM,

    @JsonProperty("l")
    LOW
}

```

Any clues?

Thanks
