Easy way to serialize/deserialize public enums?

早过忘川 提交于 2019-12-24 00:34:13

问题


I am having some trouble trying to serialize/deserialize some enum in Java. Here is my enum class:

FrequencyType.java:

@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public enum FrequencyType {
    NONE,
    HOURLY,
    DAILY,
    WEEKLY,
    MONTHLY;
}

Controller.java

@RequestMapping(method = RequestMethod.POST)
public Object createFrequency(@RequestBody FrequencyType frequencyType) throws Exception {
    ...
    frequencyManager.createFrequency(frequencyType);
    ...
}

I am trying to use the createFrequency method to take in a FrequencyType as json input. Here is an example of my input:

{
    "frequencyType": "HOURLY"
}

However, whenever I try to invoke this method via my REST API, I get an error saying:

"Could not read JSON: Can not deserialize instance of com.zuora.cds.api.schedule.FrequencyType out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@225337f8; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.zuora.cds.api.schedule.FrequencyType out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@225337f8; line: 1, column: 1]",

I have tried to add in the above JsonInclude and JsonIgnoreProperties annotationis but that doesn't seem to help. I also tried to add getters and setters like so:

FrequencyType getNone() {
    return FrequencyType.NONE;
}
FrequencyType getHourly() {
    return FrequencyType.HOURLY;
}
FrequencyType getDaily() {
    return FrequencyType.DAILY;
}
FrequencyType getWeekly() {
    return FrequencyType.WEEKLY;
}
FrequencyType getMonthly() {
    return FrequencyType.MONTHLY;
}
void setNone(FrequencyType frequencyType) {
    this.equals(frequencyType);
}
void setHourly(FrequencyType frequencyType) {
    this.equals(frequencyType);
}
void setDaily(FrequencyType frequencyType) {
    this.equals(frequencyType);
}
void setWeekly(FrequencyType frequencyType) {
    this.equals(frequencyType);
}
void setMonthly(FrequencyType frequencyType) {
    this.equals(frequencyType);
}

so that the POJO representation would hopefully transform the JSON into a FrequencyType but I end up getting the same error. Any help would be appreciated. Thanks!


回答1:


Enum types in Jackson are typically serialized and deserialized raw, as JSON strings.

You're trying to deserialize a JSON object as an enum

{
    "frequencyType": "HOURLY"
}

That's a JSON object with a single name-value pair with name frequencyType and a value that is a JSON string.

You'll need to create a POJO to wrap this object

class FrequencyTypeWrapper {
    private FrequencyType frequencyType;

    // getters and setters
}

Jackson will eventually support a @JsonWrapped annotation which you'll likely be able to add to your enum type, indicating that you want it to be wrapped with some name in a root object.

The alternative is to write and register your own JsonDeserializer for that enum type that does the appropriate conversion, ie. parses a JSON object, extracts a name-value pair, and converts the String value to an enum value.


If you're not deserializing anything else to JSON in your code, you can configure your ObjectMapper to unwrap the root value.

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

You'd have to configure the root name for your enum type as well

@JsonRootName("frequencyType")
enum FrequencyType {
    HOURLY /* more */;
}



回答2:


You can simply:

Add the following annotations:

-1.

 @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public enum FrequencyType {

-2. add the following annotation to to the setter:

@JsonDeserialize(using = EnumDeserializer.class)


来源:https://stackoverflow.com/questions/36943705/easy-way-to-serialize-deserialize-public-enums

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!