Last active
April 15, 2022 03:28
-
-
Save nobel6018/27cb81611b4e513e9887315d168adafe to your computer and use it in GitHub Desktop.
jackson set kotlin enum class as camelCase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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