Please note: Although this question seems similar to this one I am asking a slightly different question.
I am serializing/deserializing POJOs into JSON via Jackson.
I am trying to get instances of my UserStatus
enum to (de)serialize nicely and am attempting via:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
enum UserStatus {
Unregistered,
Activated,
Deactivated,
Locked
@JsonValue
String toValue() {
// TODO: ???
}
}
If my understanding of Jackson is correct, then my toValue()
method just need to figure out what value the current UserStatus
instance is, and convert it to a String. So UserStatus.Activated.toValue()
should gives us a String with a value of "Activated"
.
Main question: How do I accomplish this?
Ancillary question: Is this the right way to serialize/deserialize enums in Jackson-land?