jackson对枚举类型的序列化

2. Controlling the Enum Representation

Let’s define the following Enum:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public enum Distance {
     KILOMETER( "km" , 1000 ),
     MILE( "miles" , 1609.34 ),
     METER( "meters" , 1 ),
     INCH( "inches" , 0.0254 ),
     CENTIMETER( "cm" , 0.01 ),
     MILLIMETER( "mm" , 0.001 );
 
     private String unit;
     private final double meters;
 
     private Distance(String unit, double meters) {
         this .unit = unit;
         this .meters = meters;
     }
 
     // standard getters and setters
}

3. Default Enum Representation

By default, Jackson will represent Java Enums as simple String – for example:

1
new ObjectMapper().writeValueAsString(Distance.MILE);

Will result in:

1
"MILE"

What we would like to get when marshaling this ENUM to a JSON Object is to give something like:

1
{ "unit" : "miles" , "meters" :1609.34}

4. Enum as Json Object

Starting with Jackson 2.1.2 – the is now a configuration option that can handle this kind of representation – viathe @JsonFormat annotation, at the Enum level:

1
2
@JsonFormat (shape = JsonFormat.Shape.OBJECT)
public enum Distance { ... }

This will lead to the correct result when serializing this enum for Distance.MILE:

1
{ "unit" : "miles" , "meters" :1609.34}

5. Enums and @JsonValue

Yet another simple way of controlling the marshaling output for an enum is using the @JsonValue annotation on a getter:

1
2
3
4
5
6
7
8
public enum Distance {
     ...
  
     @JsonValue
     public String getMeters() {
         return meters;
     }
}

What we’re saying here is that getMeters() is the actual representation of this enum. So the result of serializing:

1
1609.34

6. Custom Serializer for Enum

Before Jackson 2.1.2, or if even more customization is required for the enum – we can use a custom Jackson serializer – first we’ll need to define it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class DistanceSerializer extends StdSerializer {
     
     public DistanceSerializer() {
         super (Distance. class );
     }
 
     public DistanceSerializer(Class t) {
         super (t);
     }
 
     public void serialize(Distance distance, JsonGenerator generator,
       SerializerProvider provider)
       throws IOException, JsonProcessingException {
         generator.writeStartObject();
         generator.writeFieldName( "name" );
         generator.writeString(distance.name());
         generator.writeFieldName( "unit" );
         generator.writeString(distance.getUnit());
         generator.writeFieldName( "meters" );
         generator.writeNumber(distance.getMeters());
         generator.writeEndObject();
     }
}

We will now tie together the serializer and the class it applies to:

1
2
@JsonSerialize (using = DistanceSerializer. class )
public enum TypeEnum { ... }

The result:

1
{ "name" : "MILE" , "unit" : "miles" , "meters" :1609.34}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值