【转】 Json转换利器Gson之实例五-注册TypeAdapter及处理Enum类型

原文链接: https://my.oschina.net/airship/blog/3072615

枚举类型给我们的程序带来了好处,如何用Gson来实现与Json的互转呢?请看本文.

本文重点掌握如何自己写一个TypeAdapter及注册TypeAdapter和处理Enum类型.

实体类:

1

2

3

public enum PackageState {

    PLAY, UPDATE, UPDATING, DOWNLOAD, DOWNLOADING,

}

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

public class PackageItem {

    private String name;

    private PackageState state;

    private String size;

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public PackageState getState() {

        return state;

    }

 

    public void setState(PackageState state) {

        this.state = state;

    }

 

    public String getSize() {

        return size;

    }

 

    public void setSize(String size) {

        this.size = size;

    }

 

    @Override

    public String toString() {

        return "PackageItem [name=" + name + ", size=" + size + ", state="

                + state + "]";

    }

}

 自己写一个转换器实现JsonSerializer接口和jsonDeserializer接口:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

mport java.lang.reflect.Type;

 

import com.google.gson.JsonDeserializationContext;

import com.google.gson.JsonDeserializer;

import com.google.gson.JsonElement;

import com.google.gson.JsonParseException;

import com.google.gson.JsonPrimitive;

import com.google.gson.JsonSerializationContext;

import com.google.gson.JsonSerializer;

 

public class EnumSerializer implements JsonSerializer,

        JsonDeserializer {

 

    // 对象转为Json时调用,实现JsonSerializer接口

    @Override

    public JsonElement serialize(PackageState state, Type arg1,

            JsonSerializationContext arg2) {

        return new JsonPrimitive(state.ordinal());

    }

 

    // json转为对象时调用,实现JsonDeserializer接口

    @Override

    public PackageState deserialize(JsonElement json, Type typeOfT,

            JsonDeserializationContext context) throws JsonParseException {

        if (json.getAsInt() < PackageState.values().length)

            return PackageState.values()[json.getAsInt()];

        return null;

    }

 

}

 测试类:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

 

public class GsonTest6 {

 

    public static void main(String[] args) {

        GsonBuilder gsonBuilder = new GsonBuilder();

        gsonBuilder.registerTypeAdapter(PackageState.class,

                new EnumSerializer());

        Gson gson = gsonBuilder.create();

        PackageItem item = new PackageItem();

        item.setName("item_name");

        item.setSize("500M");

        item.setState(PackageState.UPDATING);// 这个 state是枚举值

 

        String s = gson.toJson(item);

        System.out.println(s);

 

        System.out.println("--------------------------------");

 

        PackageItem retItem = gson.fromJson(s, PackageItem.class);

        System.out.println(retItem);

    }

}

 输出结果(结果中已经将state的对应枚举类型转为了int类型):

1

2

3

{"name":"item_name","state":2,"size":"500M"}

--------------------------------

PackageItem [name=item_name, size=500M, state=UPDATING]

  

分类: JSP

标签: Enum, Gson, json, TypeAdapter, 实例

转载于:https://my.oschina.net/airship/blog/3072615

你可能感兴趣的:(【转】 Json转换利器Gson之实例五-注册TypeAdapter及处理Enum类型)