我是靠谱客的博主 优秀小蝴蝶,最近开发中收集的这篇文章主要介绍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}

最后

以上就是优秀小蝴蝶为你收集整理的jackson对枚举类型的序列化的全部内容,希望文章能够帮你解决jackson对枚举类型的序列化所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部