- Published on
Taming Unwieldy API Enums with Jackson
- Authors
- Name
- Yair Mark
- @yairmark
Today I was integrating with a 3rd party API and came across an enum that was simply letters but I'm many cases the letters did not line up with the actual meaning of the status making it very difficult for new devs to consume in future.
For example it had a status enum that had these values:
C
which meant CurrentI
which meant SuspendedS
which meant Success
Luckily this can easily be tamed with Jackson. So instead of defining this status field as a String or Char I made this enum which deals with the complexity of this upstream character name but uses more logical enum name for downstream users of the enum:
enum class DocumentStatus(@JsonValue val code:Char) {
CURRENT('C'),
SUSPENDED('I'),
SUCCESS('S');
}
And then to use it simply define the status type as this enum instead of a Char
or String
:
data class Document(
val name: String,
val status: DocumentStatus
)