Last active
September 21, 2017 19:21
-
-
Save Scuilion/036c53fd7fee2de89701a95822c0fb60 to your computer and use it in GitHub Desktop.
Serialize/Deserialize values into an enum
This file contains 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
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