Gson系列1 --- Gson 序列化與反序列化 -- 陣列 / 集合序列化
阿新 • • 發佈:2019-01-29
1、簡述
> 陣列 / 集合序列化 採用陣列的形式 gson.fromJson(json, XXX[].class); 採用集合List的形式 gson.fromJson(json, new TypeToken<XXX>() {}.getType());
2.基本類
/** * 基本類 * @author sunRainAmazing */ @Data @NoArgsConstructor @AllArgsConstructor public class GsonEntity { private Integer id; private String name; privateDate birthday; }
3、測試類
package sun.rain.amazing.gson.quickstart; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import org.junit.Test; import sun.rain.amazing.gson.anno.coll.UserEntity; import sun.rain.amazing.gson.quickstart.arraylist.GsonEntity; importjava.util.*; /** * @author sunRainAmazing */ public class GsonArrayListTest { GsonEntity user0 = new GsonEntity(101,"tom",new Date()); GsonEntity user1 = new GsonEntity(101,"tom",new Date()); GsonEntity user2 = new GsonEntity(101,"tom",new Date()); private Gson gson = new Gson(); /***[{"id":101,"name":"tom","birthday":"Jul 5, 2018 3:52:48 PM"}, * {"id":101,"name":"tom","birthday":"Jul 5, 2018 3:52:48 PM"}, * {"id":101,"name":"tom","birthday":"Jul 5, 2018 3:52:48 PM"}] * [GsonEntity(id=101, name=tom, birthday=Thu Jul 05 15:52:48 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 15:52:48 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 15:52:48 CST 2018)] * [ * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * }, * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * }, * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * } * ] * [GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018)] * * */ @Test public void testArray(){ GsonEntity[] us = {user0,user1,user2}; // new Gson() 序列化 和 反序列化 String jsonArr = gson.toJson(us); System.out.println(jsonArr); // 反序列化 GsonEntity[] usersArr = gson.fromJson(jsonArr, GsonEntity[].class); System.out.println(Arrays.asList(usersArr)); // new GsonBuilder() 序列化 和 反序列化 gson = new GsonBuilder() .setPrettyPrinting() .setDateFormat("yyyy-MM-dd") .create(); jsonArr = gson.toJson(us); System.out.println(jsonArr); // 反序列化 usersArr = gson.fromJson(jsonArr, GsonEntity[].class); System.out.println(Arrays.asList(usersArr)); } /** * 注意:陣列的位元組碼檔案可以反射出具體的陣列中的物件型別, * 但是List<GsonEntity>不能直接用List<GsonEntity>.class 或List.class。 * 因為泛型編譯成位元組碼時是擦除型別的,List<GsonEntity>和List<SomeOne> * 在編譯成位元組碼後完全一樣,沒區別。 * * 泛型集合得用 new TypeToken(List<GsonEntity>){}.getType()作為反序列的第二個引數。 * * 但是 可以用陣列 來接收 List的型別 * * 見如下測試 * [{"id":101,"name":"tom","birthday":"Jul 5, 2018 4:02:20 PM"}, * {"id":101,"name":"tom","birthday":"Jul 5, 2018 4:02:20 PM"} * ,{"id":101,"name":"tom","birthday":"Jul 5, 2018 4:02:20 PM"}] * [GsonEntity(id=101, name=tom, birthday=Thu Jul 05 16:02:20 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 16:02:20 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 16:02:20 CST 2018)] * [ * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * }, * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * }, * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * } * ] * [GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018)] * */ @Test public void testListByArray(){ List<GsonEntity> list = new LinkedList<>(); list.add(user0); list.add(user1); list.add(user2); // new Gson() 序列化 和 反序列化 String jsonArr = gson.toJson(list); System.out.println(jsonArr); // 反序列化 GsonEntity[] usersArr = gson.fromJson(jsonArr, GsonEntity[].class); System.out.println(Arrays.asList(usersArr)); // new GsonBuilder() 序列化 和 反序列化 gson = new GsonBuilder() .setPrettyPrinting() .setDateFormat("yyyy-MM-dd") .create(); jsonArr = gson.toJson(list); System.out.println(jsonArr); // 反序列化 usersArr = gson.fromJson(jsonArr, GsonEntity[].class); System.out.println(Arrays.asList(usersArr)); } /** * 在新版本 gson 2.8.5中 * List 型別 會 預設轉為 陣列 * 並且數值 在反序列化時 也會 改變 * * [{"id":101,"name":"tom","birthday":"Jul 5, 2018 4:04:57 PM"}, * {"id":101,"name":"tom","birthday":"Jul 5, 2018 4:04:57 PM"}, * {"id":101,"name":"tom","birthday":"Jul 5, 2018 4:04:57 PM"}] * * {id=101.0, name=tom, birthday=Jul 5, 2018 4:04:57 PM} * * [[{id=101.0, name=tom, birthday=Jul 5, 2018 4:04:57 PM}, * {id=101.0, name=tom, birthday=Jul 5, 2018 4:04:57 PM}, * {id=101.0, name=tom, birthday=Jul 5, 2018 4:04:57 PM}]] * [ * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * }, * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * }, * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * } * ] * [[{id=101.0, name=tom, birthday=2018-07-05}, * {id=101.0, name=tom, birthday=2018-07-05}, * {id=101.0, name=tom, birthday=2018-07-05}]] */ @Test public void testList(){ List<GsonEntity> list = new ArrayList<>(); list.add(user0); list.add(user1); list.add(user2); // new Gson() 序列化 和 反序列化 String jsonArr = gson.toJson(list); System.out.println(jsonArr); // 反序列化 List<GsonEntity> usersList = gson.fromJson(jsonArr, List.class); System.out.println(usersList.get(1)); System.out.println(Arrays.asList(usersList)); // new GsonBuilder() 序列化 和 反序列化 gson = new GsonBuilder() .setPrettyPrinting() .setDateFormat("yyyy-MM-dd") .create(); jsonArr = gson.toJson(list); System.out.println(jsonArr); // 反序列化 usersList = gson.fromJson(jsonArr, List.class); System.out.println(Arrays.asList(usersList)); } /** * 新增反序列化引數 型別 * 採用 第二引數的形式 -- 進行解析 * [{"id":101,"name":"tom","birthday":"Jul 5, 2018 4:12:31 PM"}, * {"id":101,"name":"tom","birthday":"Jul 5, 2018 4:12:31 PM"}, * {"id":101,"name":"tom","birthday":"Jul 5, 2018 4:12:31 PM"}] * * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 16:12:31 CST 2018) * * [[GsonEntity(id=101, name=tom, birthday=Thu Jul 05 16:12:31 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 16:12:31 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 16:12:31 CST 2018)]] * [ * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * }, * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * }, * { * "id": 101, * "name": "tom", * "birthday": "2018-07-05" * } * ] * [[GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018), * GsonEntity(id=101, name=tom, birthday=Thu Jul 05 00:00:00 CST 2018)]] * */ @Test public void testListArgs(){ List<GsonEntity> list = new ArrayList<>(); list.add(user0); list.add(user1); list.add(user2); // new Gson() 序列化 和 反序列化 String jsonArr = gson.toJson(list); System.out.println(jsonArr); // 反序列化 List<GsonEntity> usersList = gson.fromJson(jsonArr, new TypeToken<List<GsonEntity>>(){}.getType()); System.out.println(usersList.get(1)); System.out.println(Arrays.asList(usersList)); // new GsonBuilder() 序列化 和 反序列化 gson = new GsonBuilder() .setPrettyPrinting() .setDateFormat("yyyy-MM-dd") .create(); jsonArr = gson.toJson(list); System.out.println(jsonArr); // 反序列化 usersList = gson.fromJson(jsonArr, new TypeToken<List<GsonEntity>>(){}.getType()); System.out.println(Arrays.asList(usersList)); } }