Skip to content

Instantly share code, notes, and snippets.

@nobel6018
Last active April 15, 2022 03:28
Show Gist options
  • Save nobel6018/27cb81611b4e513e9887315d168adafe to your computer and use it in GitHub Desktop.
Save nobel6018/27cb81611b4e513e9887315d168adafe to your computer and use it in GitHub Desktop.
jackson set kotlin enum class as camelCase
enum class TeaType {
GREEN_TEA, HERBAL_TEA
;
fun String.snakeToLowerCamelCase(): String {
val snakeRegex = "_[a-zA-Z]".toRegex()
return snakeRegex.replace(this.lowercase()) {
it.value.replace("_","")
.uppercase()
}
}
@JsonValue
fun jsonValue(): String {
return name.snakeToLowerCamelCase()
}
}
@Test
fun `test jackson enum class`() {
// given
val objectMapper = ObjectMapper()
class Order(
val teaType: TeaType,
val price: Int,
)
val order = Order(TeaType.GREEN_TEA, 3_000)
// when
val orderJsonString = objectMapper.writeValueAsString(order)
// then
assertThat(orderJsonString).isEqualTo("""
{"teaType":"greenTea","price":3000}
""".trimIndent())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment