Skip to content

Instantly share code, notes, and snippets.

@Scuilion
Last active September 21, 2017 19:21
Show Gist options
  • Save Scuilion/036c53fd7fee2de89701a95822c0fb60 to your computer and use it in GitHub Desktop.
Save Scuilion/036c53fd7fee2de89701a95822c0fb60 to your computer and use it in GitHub Desktop.
Serialize/Deserialize values into an enum
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.HashMap;
import java.util.Map;
public enum DeviceScheduleFormat {
WEEKDAY("weekday"),
EVEN_ODD("even-odd"),
INTERVAL("interval");
private final String statusCode;
DeviceScheduleFormat(final String statusCode) {
this.statusCode = statusCode;
Holder.MAP.put(statusCode, this);
}
//For deserialization
@JsonCreator
public static DeviceScheduleFormat fromValue(final String value) {
return Holder.MAP.get(value);
}
//For serialization
@JsonValue
public String toValue() {
return this.statusCode;
}
@SuppressWarnings({"squid:S1118", "squid:S3008"})
private static class Holder {
static Map<String, DeviceScheduleFormat> MAP = new HashMap<>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment