Published on

Taming Unwieldy API Enums with Jackson

Authors

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:

  1. C which meant Current
  2. I which meant Suspended
  3. S 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
)