How to annotate enum fields for deserialization using Jackson json

前端 未结 6 1064
一个人的身影
一个人的身影 2020-12-02 19:59

I am using REST web service/Apache Wink with Jackson 1.6.2. How do I annotate an enum field so that Jackson deserializes it?

Inner class

public enum          


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 20:41

    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.

提交回复
热议问题