How to annotate enum fields for deserialization using Jackson json

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

Jackson 1.6.2 REST web service/Apache Wink

How do I annotate an enum field so that Jackson deserializes it?

Inner class

public enum BooleanField {     BOOLEAN_TRUE        { public String value() { return "1";} },     BOOLEAN_FALSE       { public String value() { return "0";} }, 

Java Bean/Request object

BooleanField locked; public BooleanField getLocked() {return locked;} 

The Jackson docs state that it can do this via @JsonValue/@JsonCreator but provides no examples (how helpful!). I'm sure they just don't want too many people using their framework so they are keeping this secret.

Anyone willing to spill the (java)beans, as it were?

回答1:

If you are using Jackson 1.9, serialization would be done by:

public enum BooleanField {    BOOLEAN_TRUE("1")    ;     // either add @JsonValue here (if you don't need getter)    private final String value;     private BooleanField(String value) { this.value = value; }     // or here    @JsonValue public String value() { return value; } 

so change you need is to add method to Enum type itself, so all values have it. Not sure if it would work on subtype.

For @JsonCreator, having a static factory method would do it; so adding something like:

@JsonCreator public static BooleanField forValue(String v) { ... } 

Jackson 2.0 will actually support use of just @JsonValue for both, including deserialization.



回答2:

With Jackson 2.7.2 or newer

public enum BooleanField {     @JsonProperty("1")     BOOLEAN_TRUE,     @JsonProperty("0")     BOOLEAN_FALSE } 


回答3:

don't annotate them, just configure your ObjectMapper instance:

private ObjectMapper createObjectMapper() {     final ObjectMapper mapper = new ObjectMapper();     // enable toString method of enums to return the value to be mapped     mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);     mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);     return mapper; } 

and in your enum override the toString() method:

public enum SectionType { START("start"), MORE("more");      // the value which is used for matching     // the json node value with this enum     private final String value;      SectionType(final String type) {         value = type;     }      @Override     public String toString() {         return value;     } } 

You don't need any annotations or custom deserializers.



回答4:

Actually, according to the docs for JsonValue (Jackson 2.3.3):

NOTE: when use for Java enums, one additional feature is  * that value returned by annotated method is also considered to be the  * value to deserialize from, not just JSON String to serialize as.  * This is possible since set of Enum values is constant and it is possible  * to define mapping, but can not be done in general for POJO types; as such,  * this is not used for POJO deserialization.  

So for enums, your deserialization will not work using JsonCreator because JsonValue will be used for both serialization and deserialization. One way to do this for enums is using JsonSetter and JsonGetter.



回答5:

The following may work if the enumeration is an array or not. (Only for deserialization)

package com.stack.model;  import java.util.HashMap; import java.util.Map;  import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder;  import lombok.Data;  @Data @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) @JsonPropertyOrder({ "success", "my-enums" }) public class MyObjectJSON {      @JsonProperty("sucess")     private boolean success;      @JsonProperty("my-enums")     private MyEnum[] myEnums;       static enum MyEnum {         Enum1, Enum2, Enum3, Enum4, EnumN;          private static Map myEnumsMap = new HashMap(5);          static {             myEnumsMap.put("enum1-val", Enum1);             myEnumsMap.put("enum2-val", Enum2);             myEnumsMap.put("enum3-val", Enum3);             myEnumsMap.put("enum4-val", Enum4);             myEnumsMap.put("enumn-val", EnumN);         }          @JsonCreator         public static MyEnum forValue(String value) {             return myEnumsMap.get(value.toLowerCase());         }     } } 

To consider:

  1. The @Data annotation generates setters, getters, toString, etc.
  2. @JsonProperty("my-enums") private MyEnum[] myEnums, this is the way to annotate with jackson the field that is of type Enum ( It works if it is an array or not).

  3. { "sucess": true, "my-enums": ["enum1-val", "enum3-val"] }



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