Gson在序列化反序列化中的TypeAdapter

 1 package waf.json.adatpter;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import com.google.gson.TypeAdapter;
 9 import com.google.gson.internal.LinkedTreeMap;
10 import com.google.gson.stream.JsonReader;
11 import com.google.gson.stream.JsonToken;
12 import com.google.gson.stream.JsonWriter;
13 
14 public class CommonTypeAdapter extends TypeAdapter<Object>
15 {
16 
17     @Override
18     public Object read(JsonReader in) throws IOException
19     {
20         JsonToken token=in.peek();
21         switch(token)
22         {
23         case BEGIN_ARRAY:
24             List<Object> list=new ArrayList<Object>();
25             in.beginArray();
26             while(in.hasNext())
27             {
28                 list.add(read(in));
29             }
30             in.endArray();
31             return list;
32 
33         case BEGIN_OBJECT:
34             Map<String,Object> map=new LinkedTreeMap<String,Object>();
35             in.beginObject();
36             while(in.hasNext())
37             {
38                 map.put(in.nextName(),read(in));
39             }
40             in.endObject();
41             return map;
42 
43         case STRING:
44             return in.nextString();
45 
46         case NUMBER:
47             /**
48              * 改写数字的处理逻辑,将数字值分为整型与浮点型。
49              */
50             double dbNum=in.nextDouble();
51 
52             // 数字超过long的最大值,返回浮点类型
53             if(dbNum>Long.MAX_VALUE)
54             {
55                 return dbNum;
56             }
57 
58             // 判断数字是否为整数值
59             long lngNum=(long)dbNum;
60             if(dbNum==lngNum)
61             {
62                 return lngNum;
63             }
64             else
65             {
66                 return dbNum;
67             }
68 
69         case BOOLEAN:
70             return in.nextBoolean();
71 
72         case NULL:
73             in.nextNull();
74             return null;
75 
76         default:
77             throw new IllegalStateException();
78         }
79     }
80 
81     @Override
82     public void write(JsonWriter out,Object value) throws IOException
83     {
84         // 序列化无需实现
85     }
86 
87 }
CommonTypeAdapter
 1 package waf.json.adatpter;
 2 
 3 import java.lang.reflect.Type;
 4 import java.text.DateFormat;
 5 import java.text.SimpleDateFormat;
 6 import java.util.Date;
 7 
 8 import com.google.gson.JsonDeserializationContext;
 9 import com.google.gson.JsonDeserializer;
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonParseException;
12 
13 public class DateTypeAdapter implements JsonDeserializer<Date> /*,JsonSerializer<Date>*/
14 {
15 
16     @Override
17     public Date deserialize(JsonElement json,Type typeOfT,JsonDeserializationContext context) throws JsonParseException
18     {
19         Date date=null;
20         DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
21         try
22         {
23             date=df.parse(json.getAsString());
24         }
25         catch(Exception e)
26         {
27             date=null;
28         }
29         return date;
30     }
31     
32 }
DateTypeAdapter
 1 package waf.json.adatpter;
 2 
 3 import java.lang.reflect.Type;
 4 
 5 import com.google.gson.JsonDeserializationContext;
 6 import com.google.gson.JsonDeserializer;
 7 import com.google.gson.JsonElement;
 8 import com.google.gson.JsonParseException;
 9 
10 
11 public class IntegerTypeAdapter implements JsonDeserializer<Integer>
12 {
13 
14     @Override
15     public Integer deserialize(final JsonElement json,final Type typeOfT,final JsonDeserializationContext context) throws JsonParseException
16     {
17         if(json.isJsonNull()||json.getAsString().length()==0)
18         {
19             return null;
20         }
21         try
22         {
23             return json.getAsInt();
24         }
25         catch(NumberFormatException e)
26         {
27             return null;
28         }
29     }
30 
31 }
IntegerTypeAdapter
 1 package waf.json.adatpter;
 2 
 3 import java.lang.reflect.Type;
 4 
 5 import com.google.gson.JsonDeserializationContext;
 6 import com.google.gson.JsonDeserializer;
 7 import com.google.gson.JsonElement;
 8 import com.google.gson.JsonParseException;
 9 
10 
11 public class LongTypeAdapter implements JsonDeserializer<Long>
12 {
13 
14     @Override
15     public Long deserialize(final JsonElement json,final Type typeOfT,final JsonDeserializationContext context) throws JsonParseException
16     {
17         if(json.isJsonNull()||json.getAsString().length()==0)
18         {
19             return null;
20         }
21         try
22         {
23             return json.getAsLong();
24         }
25         catch(NumberFormatException e)
26         {
27             return null;
28         }
29     }
30 
31 }
LongTypeAdapter
 1 package waf.json.adatpter;
 2 
 3 import java.lang.reflect.Type;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 import java.util.Set;
 7 
 8 import com.google.gson.JsonDeserializationContext;
 9 import com.google.gson.JsonDeserializer;
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonObject;
12 import com.google.gson.JsonParseException;
13 
14 public class MapTypeAdapter1 implements JsonDeserializer<Map<String,Object>>
15 {
16 
17     @Override
18     public Map<String,Object> deserialize(JsonElement json,Type typeOfT,JsonDeserializationContext context) throws JsonParseException
19     {
20 
21         Map<String,Object> treeMap=new HashMap<String,Object>();
22         JsonObject jsonObject=json.getAsJsonObject();
23         Set<Map.Entry<String,JsonElement>> entrySet=jsonObject.entrySet();
24         for(Map.Entry<String,JsonElement> entry:entrySet)
25         {
26             treeMap.put(entry.getKey(),entry.getValue());
27         }
28         return treeMap;
29     }
30 
31 }
MapTypeAdapter

 

posted @ 2020-09-06 19:43  从程序员到CTO  阅读(222)  评论(0)    收藏  举报
交流加我QQ:39667545